删除项目或将项目添加到RecyclerView元素时出现问题,问题不是添加或删除自身时发生,所以我解释了这种情况。
我有一个回收站和一个向其中添加元素的按钮,如果用户按一个视图,它将显示一个包含删除按钮和更多内容的对话框。我可以在“回收者”视图上添加和/或删除项目,并使用可以更新视图notifyDataSetChanged()
,但是当我按“后退”按钮(仅通过此操作,不能以其他任何方式)时,视图会再次出现(在删除的情况下)或消失(如果我进行了添加),则将回收站设置为更改前的样子,并且我不希望出现这种情况,因为我创建的delete和add方法不仅会添加/删除视图,而且还会关联数据库中的数据同样,因此,如果删除后按视图,则应用程序崩溃,因为不再有此类数据,但视图仍然存在。
所有这些内容都在一个导航抽屉可访问的片段中,如果我浏览其他片段并再次返回该片段,则后按不再会影响回收器,并且所有这些“鬼影”视图都会消失,因为这些数据已从数据库中重新加载。我需要一个som帮助来避免这种情况,我知道可以删除撤消操作,但是我不想,我只是想删除数据。
更新:这正是发生的情况,回收器视图中的项目消失/重新出现是由按下后退按钮引起的
UPDATE 2: I've solved the problem by overriding the onBackPressed method using this documentation from google, when back pressed what I do is to navigate to first fragment in the navigation, this is not what I wanted but solved the problem temporarily (as a patch), I'm still looking for answer.
UPDATE 3: Posted my own answer with the fix of all this problem, not using onBackPressed, it worked fine, and I realize some curious behavior
As you may notice my app has a navigation drawer, when trying to fix the problem by overridig the onBackPressed() in fragment, I tried to get back to the first fragment in my navigation drawer, when doing so I realize an error that I had not realized before:
NavigationUI.onNavDestinationSelected(menuItem,navController);
This line was duplicated in the NavigationItemSelectedListener:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
int id=menuItem.getItemId();
//this line indicates the action of changing fragment using navController reference
NavigationUI.onNavDestinationSelected(menuItem,navController);
if (id==R.id.nav_cuanto){
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {drawer.closeDrawer(GravityCompat.START);}
},300);
}
if(id==R.id.nav_reportes){
// NavigationUI.onNavDestinationSelected(menuItem,navController); -> this line caused the hole problem
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {drawer.closeDrawer(GravityCompat.START);}
},300);
}
if(id==R.id.nav_about){
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
drawer.closeDrawer(GravityCompat.START);
}
},300);
}
return true;
}
});
I deleted the line and everything worked just fine, as expected, this particular method creates a new instance of the fragment you are trying to navigate to, but the problem is that when you try to navigate to the fragment you already are in, it creates a new instance a navigate to it, and this new instance is on the over the old one, so when back pressed the app do not return to previous view, but to the same fragment with the old view (out of date, of couse), I couldn't imagine this behavior, this is highly not imaginable, and it's not in the docs so I almost die trying to understand. So WARNING: if someone is trying to use
NavigationUI.onNavDestinationSelected(menuItem,navController);
请勿在同一通话中多次使用它,如果是这样,在最佳情况下,您需要按两次:-(
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句