[연습8] - 선택된 항목의 상세정보를 보여주는 DetailsFragment 추가하기

  1. 연습7에서 수행한 프로젝트를 바탕으로 진행
  2. 안드로이드 스튜디오에서 File > New > Fragment > Fragment(Blank) 를 이용하여 DetailsFragment를 생성한다.
  3. Configure Component 대화창에서 아래와 같이 설정후, Finish 버튼을 클릭

  4. 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>
  5. 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;
        }
    }
  6. 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();
        }
    }
  7. 실행결과