지난번에 BottomNavigationView를 만들었다면 화면과 연결을 해보자
한 액티비티에 BottomNavigationView 와 FrameLayout을 넣고 FrameLayout에 들어갈 여러개의 Fragment를
연결해주려고 한다.
먼저 activity_main.xml에 FrameLayout을 추가한다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/bottomNavigationView"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
추가했다면 frameLayout에 Fragment를 추가해주어야 한다.
나는 기존 BottomNavigationView에 5가지 아이템을 넣었으니 5개의 프래그먼트를 추가하겠다.
(각각 PlayListFragment, SongFragment, ArtistFragment, AlbumsFragment, FolderFragment)
layout-New-Fragment-Fragment(Blank)로 추가해주었다.
추가 후에 MainActivity로 가서 BottomNavigationView 아이템을 클릭 했을 때 화면이 변경되게 해보자.
package hyerim.my.musicplayer;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
private Fragment playListFragment, songFragment, artistFragment, albumsFragment, folderFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frameLayout = findViewById(R.id.frameLayout);
BottomNavigationView bottomView = findViewById(R.id.bottomNavigationView);
bottomView.setOnNavigationItemSelectedListener(listener);
playListFragment = new PlayListFragment();
songFragment = new SongFragment();
artistFragment = new ArtistFragment();
albumsFragment = new AlbumsFragment();
folderFragment = new FolderFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, playListFragment).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, playListFragment).commit();
return true;
case R.id.menu2:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, songFragment).commit();
return true;
case R.id.menu3:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, artistFragment).commit();
return true;
case R.id.menu4:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, albumsFragment).commit();
return true;
case R.id.menu5:
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, folderFragment).commit();
return true;
}
return false;
}
};
}
각 프래그먼트를 선언해주고, 각 Fragment변수명에 맞는 클래스를 연결해주었다.
첫 화면은 맨 처음 자동 아이템을 선택해주기 때문에 PlayListFragment로 지정해주고
그 후에 setOnNavigationItemSelectedListener를 통해 화면전환이 되게끔 순서대로 지정해주었다.
다 작성 후 실행해보면
하단 메뉴에서 playList를 눌렀을 땐 해당 프래그먼트가, Song 메뉴를 눌렀을 땐 그에 해당되는 화면이 보여지게 되고
그 외의 메뉴들도 마찬가지로 해당 화면이 보여질 것이다.
'소프트웨어 > 안드로이드[Java]' 카테고리의 다른 글
[Android] Collapsing Toolbar Layout 텍스트 안나올 때 (0) | 2021.04.20 |
---|---|
[Android] android.database.CursorIndexOutOfBoundsException (0) | 2021.04.06 |
[Android] BottomNavigationView 구현하기 (0) | 2021.04.01 |
[Android] 버튼 커스텀 하기 - Ripple Effect 방법 (0) | 2020.12.29 |
[Android]Thread에서 UI바꾸기 (0) | 2020.12.18 |