Configure Component 대화창에서 아래와 같이 설정후, Finish 버튼을 클릭
fragment_details.xml 파일을 열고, TextView 위젯을 포함한 레이아웃 정의
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/textview"/>
</ScrollView>
</LinearLayout>
DetailsFragment.java
public class DetailsFragment extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "index";
private int mIndex;
public DetailsFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param index selected position in the ListView.
* @return A new instance of fragment DetailsFragment.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment fragment = new DetailsFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, index);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mIndex = getArguments().getInt(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView tv = (TextView)view.findViewById(R.id.textview);
if (mIndex >=0)
tv.setText(Shakespeare.DIALOGUE[mIndex]);
return view;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
implements TitlesFragment.OnTitleSelectedListener{
//... 기존과 동일
public void onTitleSelected(int i) {
//Toast.makeText(getApplicationContext(),"position="+i,Toast.LENGTH_SHORT).show();
DetailsFragment detailsFragment = DetailsFragment.newInstance(i);
getSupportFragmentManager().beginTransaction().replace(R.id.details, detailsFragment).commit();
}
}
실행결과