on
Android App Study_22 (하단 선택바 (bottomNavigation, Fragment 전환))
Android App Study_22 (하단 선택바 (bottomNavigation, Fragment 전환))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
package com.example.snsappexample; import ... public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; private Fragment1 fragment1; private Fragment2 fragment2; private Fragment3 fragment3; private Fragment4 fragment4; private Fragment5 fragment5; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigationView = findViewById(R.id.bottomNavi); fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); fragment4 = new Fragment4(); fragment5 = new Fragment5(); setFragment(R.id.action_airplane); //제일 처음 띄울 fragment를 설정한다. bottomNavigationView.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) { setFragment(item.getItemId()); //item의 id를 인자로 한다. return true ; //true의 의미 = bottomMenu의 애니메이션을 적용시긴다. } }); } private void setFragment( int n){ fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); switch (n) { //item의 id에 따라서 fragment를 띄우는 각각의 코드를 실행시킨다. case R.id.action_airplane : fragmentTransaction. replace (R.id.main_frame, fragment1); fragmentTransaction.commit(); break ; case R.id.action_Bus : fragmentTransaction. replace (R.id.main_frame, fragment2); fragmentTransaction.commit(); break ; case R.id.action_BlueTooth : fragmentTransaction. replace (R.id.main_frame, fragment3); fragmentTransaction.commit(); break ; case R.id.action_create : fragmentTransaction. replace (R.id.main_frame, fragment4); fragmentTransaction.commit(); break ; case R.id.action_setting : fragmentTransaction. replace (R.id.main_frame, fragment5); fragmentTransaction.commit(); break ; } } } Colored by Color Scripter
from http://yunseong.tistory.com/45 by ccl(A) rewrite - 2021-09-17 01:01:18