我一直在使用 ViewPager 使用选项卡布局一段时间,但它已被弃用。我按照官方文档迁移到 ViewPager2,一切正常,但每个片段都出现以下错误:
无效 ID 0x00000001 无效 ID 0x00000001 无效 ID 0x00000001
每次我在片段之间滑动时,这些都会增加。没有崩溃,但每次我重新打开活动时,数字都会增加到 Invalid ID 0x00000002 等等。
我迷失了解决这个错误的解决方案,但该应用程序仍然适用于新的 ViewPager2。有什么想法可以解决错误垃圾邮件吗?
活动代码:
//Responsible for adding the 4 tabs: NewLoot, ActiveLoot, SharedLoot, FinishedLoot
private void setupViewPager(){
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager(),getLifecycle());
adapter.addFragment(new NewLootFragment());//index 0
adapter.addFragment(new ActiveLootFragment());//index 1
adapter.addFragment(new FinishedLootFragment());//index 2
ViewPager2 viewPager = (ViewPager2) findViewById(R.id.container);
viewPager.setAdapter(adapter);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//tab.setText("OBJECT " + (position + 1));
}
}
).attach();
tabLayout.getTabAt(0).setIcon(R.drawable.ic_search);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_arrow);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_action_name);
tabLayout.getTabAt(0).getIcon().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.view.getTab().getIcon().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_IN);
} @Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.view.getTab().getIcon().setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
} @Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
SectionPagerAdapter 代码:
//Class that stores fragments for tabs
public class SectionsPagerAdapter extends FragmentStateAdapter {
private static String TAG = "SectionsPagerAdapter";
private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
public SectionsPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle){
super(fragmentManager, lifecycle);
}
//public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
// super(fragmentManager, lifecycle);
//}
public void addFragment(Fragment fragment){
mFragmentList.add(fragment);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemCount() {
return mFragmentList.size();
}
}
布局活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Loot.LootActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top Section (Toolbar) -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout0">
<include
android:id="@+id/topViewBarHelper"
layout="@layout/snippet_top_profilebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<!-- Top Section (Toolbar) -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout1"
android:layout_below="@+id/relLayout0">
<include layout="@layout/layout_top_tabs"/>
</RelativeLayout>
<!-- Middle Section (Body) -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout2"
android:layout_below="@+id/relLayout1">
<include layout="@layout/layout_center_viewpager"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout3">
<!-- Bottom Section (Navigation) -->
<include layout="@layout/layout_bottom_navigation_view"/>
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
布局中心查看器:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr">
</androidx.viewpager2.widget.ViewPager2>
</RelativeLayout>
</merge>
布局顶部选项卡:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/AppBarLayout">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tab_layout"
android:background="@drawable/white_grey_border_bottom"
app:tabIndicatorColor="@color/red"
app:tabIndicatorHeight="4dp">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
</merge>