개발/안드로이드
android - Tapbar ( fragment )
웅'jk
2023. 2. 15. 15:23
1. implement
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
2. ui
// activation_main
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
app:itemIconSize="30dp"
app:labelVisibilityMode="selected"
app:menu="@menu/bottom_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomNavigationView"
app:defaultNavHost="true"
app:navGraph="@navigation/my_nav" />
//navigation 폴더에 my_nav
<navigation 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:id="@+id/my_nav"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.rrc0777.tabbar.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" />
<fragment
android:id="@+id/secondFragment"
android:name="com.rrc0777.tabbar.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
<fragment
android:id="@+id/thirdFragment"
android:name="com.rrc0777.tabbar.ThirdFragment"
android:label="fragment_third"
tools:layout="@layout/fragment_third" />
</navigation>
3. java code
// main
// 멤버변수
BottomNavigationView navigationView;
Fragment firstFragment;
Fragment secondFragment;
Fragment thirdFragment;
// onCreate
navigationView = findViewById(R.id.bottomNavigationView);
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
thirdFragment = new ThirdFragment();
navigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
Fragment fragment;
if(itemId == R.id.firstFragment){
fragment = firstFragment;
}else if(itemId == R.id.secondFragment){
fragment = secondFragment;
}else{
fragment = thirdFragment;
}
return loadFragment(fragment);
}
});
//
}
// loadfragment 메소드
private boolean loadFragment(Fragment fragment){
if(fragment != null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment,fragment)
.commit();
return true;
}
else{
return false;
}
}
// FirstFlagment 는 다음과 같이 되어있다.
public class FirstFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FirstFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FirstFragment.
*/
// TODO: Rename and change types and number of parameters
public static FirstFragment newInstance(String param1, String param2) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
// 화면 관련 logic 은 onCreateView 에서 처리한다.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return new inflater.inflate(R.layout.fragment_first, container, false);
}