如何解决Recylerview 在按下时附加旧结果
我正在尝试创建一个带有底部导航视图的应用程序,但它看起来像是在堆积碎片。让我给你看清楚的图片。
[![再次点击后退按钮时黑屏][3]][3]
代码如下: 主页.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/bottomfrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStart="0dp"
app:fabAlignmentMode="center">
<com.rawat.soccermatch.CustomBottomNav
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemRippleColor="@android:color/transparent"
app:labelVisibilityMode="labeled"
app:itemIconTint="@color/enable_disable"
app:itemTextColor="@color/enable_disable"
app:menu="@menu/bottom_nav_menu"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:contentDescription="@string/title_create"
android:src="@drawable/ic_plusn"
app:tint="#fff"
app:layout_anchor="@id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Home.java
bt = findViewById(R.id.bottomNavigationView);
loadFragment(new HomeFrag()); // at first home fragment will be loaded
bt.setonNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment mfragment;
int item_id=item.getItemId();
if(item_id==R.id.navigation_home){
mfragment = new HomeFrag();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_Feed){
mfragment = new Feed();
loadFragment(mfragment);
}
else if(item_id==R.id.navigation_create){
mfragment = new creatematch();
loadFragment(mfragment);
}
else if (item_id==R.id.navigation_following){
mfragment = new Following();
loadFragment(mfragment);
}
else {
mfragment = new Account();
loadFragment(mfragment);
}
return true;
}
});
ft = findViewById(R.id.fab);
ft.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ft.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
ft.setRippleColor(Color.GREEN);
Fragment fragment = new creatematch();
bt.getMenu().findItem(R.id.navigation_create).setChecked(true);
loadFragment(fragment);
}
});
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottomfrag,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
我也应该发布 HomFrag 代码吗?
我现在已经编辑了这个问题,我理解的问题是当我点击返回我的 Home Fragment 从它离开的地方恢复时,但是 recylerview 值正在重置并将新结果附加到旧的。并且波纹颜色从跟随到回家没有变化。
解决方法
试着用这个函数代替你的
public void loadFragment(Fragment fragment) {
try {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
manager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.addToBackStack(null);
ft.replace(R.id.bottomfrag,fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
,
您正在将该片段添加到后台堆栈,因此当您单击返回分离它时,您需要确保您的片段未添加到后台堆栈
,覆盖 onBackPressed() 方法。在其中,您可以检查当前打开了哪个部分以及在这种情况下您想做什么(什么都不做/关闭应用程序/导航到另一个部分)。
,在底部导航活动中,您必须设置默认片段是选择您想要的片段,例如在公共主页片段中始终显示为默认片段,因此将 homefragment 设置为默认值。
在创建时设置这些代码
bottomNavigationView.setSelectedItemId(R.id.nav_home);
检查背压
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。