* Main ----------------------------------------------------------
***** xml
***** java
* Fragment1 ----------------------------------------------------------
***** fragment1.xml
***** java
* Fragment2 ----------------------------------------------------------
***** fragment2.xml
***** java
ref :
http://yoo454.tistory.com/entry/Fragment-%EA%B0%84%EB%8B%A8-%EC%98%88%EC%A0%9C
http://novafactory.net/archives/1837
***** xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment1" android:id="@+id/button1" android:layout_weight="1" android:onClick="selectFrag" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment2" android:id="@+id/button2" android:layout_weight="1" android:onClick="selectFrag" /> </LinearLayout> <fragment android:id="@+id/fragment_place" android:name="[Fragment1 class]" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/fragment1" /> </LinearLayout>
***** java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void selectFrag(View view){ Fragment fr; if(view == findViewById(R.id.button1)){ fr = new Fragment1(); }else { fr = new Fragment2(); } FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.replace(R.id.fragment_place, fr); fragmentTransaction.commit(); } }
* Fragment1 ----------------------------------------------------------
***** fragment1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff9b9b"> </LinearLayout>
***** java
public class Fragment1 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.fragment1, container, false); } }
* Fragment2 ----------------------------------------------------------
***** fragment2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#7dd163"> </LinearLayout>
***** java
public class Fragment2 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.fragment2, container, false); } }
ref :
http://yoo454.tistory.com/entry/Fragment-%EA%B0%84%EB%8B%A8-%EC%98%88%EC%A0%9C
http://novafactory.net/archives/1837
댓글
댓글 쓰기