Android Studio中如何在Fragment内实现导航栏
在已有导航栏加载的Fragment内实现子导航菜单的解决方案
我曾在一个由外部导航栏加载的Fragment中,遇到了内部导航菜单的实现问题。最终验证最简单的方案是在该Fragment中使用TabLayout + ViewPager2组合来实现子页面切换,以下是完整实现代码:
主Fragment代码
public class HomeFragment extends Fragment { private FragmentHomeBinding binding; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = FragmentHomeBinding.inflate(inflater, container, false); View root = binding.getRoot(); TabLayout tabLayout = root.findViewById(R.id.tabLayout); ViewPager2 viewPager2 = root.findViewById(R.id.viewPagerHomeScreen); ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this); viewPager2.setAdapter(viewPagerAdapter); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager2.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); return root; } @Override public void onDestroyView() { super.onDestroyView(); binding = null; } private class ViewPagerAdapter extends FragmentStateAdapter { public ViewPagerAdapter(@NonNull Fragment fragment) { super(fragment); } @NonNull @Override public Fragment createFragment(int position) { switch (position){ case 0 : return new HomeScreenMembership(); case 1 : return new HomeScreenCreatedClubs(); default : return new HomeScreenMembership(); } } @Override public int getItemCount() { return 2; } }
主Fragment布局XML
<?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" tools:context=".ui.home.HomeFragment"> <com.google.android.material.tabs.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/membership_tab" /> <com.google.android.material.tabs.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/created_clubs_tab" /> </com.google.android.material.tabs.TabLayout> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewPagerHomeScreen" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/tabLayout" /> </androidx.constraintlayout.widget.ConstraintLayout>
Tab对应的Fragment示例(其余Tab页面实现逻辑类似)
package com.freedommuskrats.clubbub.ui.home; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.freedommuskrats.clubbub.R; public class HomeScreenCreatedClubs extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home_screen_created_clubs, container, false); } }
对应Fragment的布局XML
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".ui.home.HomeScreenCreatedClubs"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Created" /> </FrameLayout>
内容的提问来源于Stack Exchange,提问作者brando f
相关产品推荐
相关产品推荐

