在我的MainActivity
中,我有一个BottomNavigationView
,其中包含5个不同的片段。其中之一是HomeFragment
。在HomeFragment
中,我有一个TabLayout
和Viewpager
,其中还有3个片段(家庭,世界,消息)。
我可以使用什么代码,以便当我在BottomNavigationView中单击主页(项目)时,选项卡中的内容将完全替换屏幕,并且您只能根据所使用的内容来查看选项卡中的内容。
问题是TabLayout
中的片段保留在HomeFragment
中的BottomNavigationView
后面,所以当我滚动时,您会看到TabLayout中三个选项卡中的片段(取决于您所使用的选项卡)背景。
我该如何解决?
HomeFragment.java
public class HomeFragment extends Fragment {
private ProgressBar mProgressBar;
private PostAdapter mPostAdapter;
private List<Post> mPostLists;
private Activity mActivity;
private FirebaseAuth mFirebaseAuth;
private List<String> mFollowingList;
private DrawerLayout mDrawer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
mFirebaseAuth = FirebaseAuth.getInstance();
mDrawer = mActivity.findViewById(R.id.drawerLayout);
mProgressBar = v.findViewById(R.id.progress_circular);
TabLayout tabLayout = v.findViewById(R.id.tab_layout);
ViewPager viewPager = v.findViewById(R.id.viewpager);
if (getFragmentManager() != null) {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
viewPagerAdapter.addFragment(new HomeFragment(), "Home");
viewPagerAdapter.addFragment(new WorldFragment(), "World");
viewPagerAdapter.addFragment(new MessagesFragment(), "Messages");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
RecyclerView recyclerView = v.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
mPostLists = new ArrayList<>();
mPostAdapter = new PostAdapter(getContext(), mPostLists);
recyclerView.setAdapter(mPostAdapter);
return v;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof Activity) {
mActivity = (Activity) context;
}
}
static class ViewPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mFragments;
private ArrayList<String> mTitles;
ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
this.mTitles = new ArrayList<>();
}
@NonNull
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mTitles.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
}
MainActivity.java
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
mSelectedFragment = new HomeFragment();
break;
case R.id.nav_search:
mSelectedFragment = new SearchFragment();
break;
case R.id.nav_notifications:
mSelectedFragment = new NotificationsFragment();
break;
case R.id.nav_profile:
SharedPreferences.Editor editor = getSharedPreferences("PREFS", MODE_PRIVATE).edit();
editor.putString("profileid", FirebaseAuth.getInstance().getCurrentUser().getUid());
editor.apply();
mSelectedFragment = new ProfileFragment();
break;
case R.id.nav_save:
mSelectedFragment = new SaveFragment();
break;
}
if (mSelectedFragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mSelectedFragment).commit();
}
return true;
}
};
}
尝试一下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".fragment.BlankFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_home_fragment"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:attr/windowBackground"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/events_logo_main_activity"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:contentDescription="@string/logo"
android:src="@drawable/events_logo_black_max_size" />
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginEnd="11dp"
android:contentDescription="@string/camera"
android:src="@drawable/ic_camera_create_events_home_fragment_black" />
<ImageView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:contentDescription="@string/options"
android:src="@drawable/ic_three_bars_settings_home_fragment_black" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appBar1"
android:background="?android:attr/windowBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:windowBackground"
app:tabIndicatorColor="@color/colorPrimaryAqua50"
app:tabSelectedTextColor="@color/colorPrimaryAqua50"
app:tabTextColor="@color/colorPrimaryDark" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/appBar2"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewpager">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>