파이티스

안드로이드 스튜디오 BottomNavigationView 하단탭 만들기 (화면전환) - 2 본문

Android/APP 만들기

안드로이드 스튜디오 BottomNavigationView 하단탭 만들기 (화면전환) - 2

파이티스 2023. 8. 18. 10:15

 

지난시간 MainActivity에 Fragment를 5개 만들어서 하단 탭바를 생성해보았다. 자세한 내용을 아래 링크를 참고하자!

 

 

https://pytis.tistory.com/14

 

안드로이드 스튜디오 BottomNavigationView 하단탭 만들기 - 1

Fragment 란? 하나의 액티비티 안에 여러 화면을 구현하는 것이다. 예를들어 카카오톡에서 하단의 탭을 누르면 해당화면으로 전환되는것을 말한다. 액티비티위에 Fragment가 존재하므로 Activity 전체

pytis.tistory.com

 

이번시간에는 각각의 아이콘을 눌렀을때 해당 플래그먼트로 화면전환하는 로직 구현을 해보겠다.

멤버변수 만들기

 

BottomNavigationView 앱 하단에 탐색메뉴를 만들기 위한 뷰를 선언, 5개의 Fragment 멤버변수 만들기

 

public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;

    Fragment firstFragment;
    Fragment secondFragment;
    Fragment thirdFragment;
    Fragment fourthFragment;
    Fragment fifthFragment;

 

onCreate() 메서드가 호출

 

로직을 구현하고 마지막에 loadFragment() 함수 실행한다.

 

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setTitle("AI챗");

        bottomNavigationView = findViewById(R.id.bottomNavigationView);

        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();
        thirdFragment = new ThirdFragment();
        fourthFragment = new FourthFragment();
        fifthFragment = new FifthFragment();

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                int itemId = item.getItemId();
                Fragment fragment = null;

                if(itemId == R.id.firstFragment){
                    fragment = firstFragment;
                    getSupportActionBar().setTitle("홈");

                }else if(itemId == R.id.secondFragment){
                    fragment = secondFragment;
                    getSupportActionBar().setTitle("찾기");

                }else if(itemId == R.id.thirdFragment){
                    fragment = thirdFragment;
                    getSupportActionBar().setTitle("AI챗");

                }else if(itemId == R.id.fourthFragment){
                    fragment = fourthFragment;
                    getSupportActionBar().setTitle("약정보");

                }else if(itemId == R.id.fifthFragment) {
                    fragment = fifthFragment;
                    getSupportActionBar().setTitle("내관리");

                }

                return loadFragment(fragment) ;
            }
        });

    }

 

loadFragment() 함수 정의

 

이 함수는 프래그먼트를 로드하고 화면에 표시하는 역할

 

    boolean loadFragment(Fragment fragment){
        if(fragment != null){
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment, fragment)
                    .commit();
            return true;
        }else{
            return false;
        }
    }

 

완성