-
안드로이드 프래그먼트 활용하기Mobile App/Android 2021. 5. 2. 18:57
MainActivity.java
public class MainActivity extends AppCompatActivity { private FragmentManager fragmentManager; private Fragment_Main fragment_main; private Fragment_MyPage fragment_my_page; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); fragment_main = new Fragment_Main(); fragment_my_page = new Fragment_MyPage(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.frameLayout, fragment_my_page).commitNowAllowingStateLoss(); } public void MenuClickHandler(View v){ fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); switch (v.getId()){ case R.id.btn_main: fragmentTransaction.replace(R.id.frameLayout, fragment_main).commitNowAllowingStateLoss(); break; case R.id.btn_my_page: fragmentTransaction.replace(R.id.frameLayout, fragment_my_page).commitNowAllowingStateLoss(); break; } } }
activity_main.xml
<FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@id/linearLayout" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> </FrameLayout> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"> <Button android:id="@+id/btn_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MenuClickHandler" android:layout_weight="1" android:text="Main" /> <Button android:id="@+id/btn_my_page" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="MenuClickHandler" android:layout_weight="1" android:text="MyPage" /> </LinearLayout>
각 Fragment Java
public class Fragment_이름 extends Fragment { @Nullable @Override public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ){ return inflater.inflate(R.layout.fragment_이름, container, false); } }
테스트용 Fragment Layout xml
<?xml version="1.0" encoding="utf-8"?> <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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="This" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="is" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="a" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Main Page" /> </LinearLayout> </LinearLayout>
결과물 'Mobile App > Android' 카테고리의 다른 글
안드로이드 웹뷰 뒤로가기 버튼 이벤트 만들기 (0) 2021.05.12 안드로이드 웹뷰 사용 및 설정, SSL 무시 (0) 2021.05.12 안드로이드 하단 메뉴바 활용하기 (0) 2021.05.02 안드로이드 프래그먼트 활용하기 (0) 2021.05.02 안드로이드 스플래시 이미지 구현하기 (0) 2021.04.29 안드로이트 타이틀바/상태바 없애기 (노치 대응) (0) 2021.04.29