recyclerview do do diepager2 tab上的显示更新列表更改
在我的Android应用程序中,我使用TakerAut和ViewPager2使用RecyClerview显示不同类别的列表。当我进行添加和删除时,然后调用notifyDataSetchanged
时,列表在我仍在当前选项卡上时会更改。但是,当我选择另一个选项卡并返回时,列表显示创建片段时初始化的原始项目。
在createFragment
fragmentStatapter
适配器类的函数中,我将片段用启动数据项arrayList启动,如下所示。
public class DataListAdapter extends FragmentStateAdapter {
private DataListFragment[] fragments;
//
// other code
//
@Override
public Fragment createFragment(int position) {
ArrayList<DataItem> dataItems;
//The actual code returns a different data item based on conditions
dataItems = dbAdapter.getDataItems();
DataListFragment frag = new DataListFragment(dataItems);
fragments[position] = frag;
return frag;
}
}
在datalistfragment中,我...
DataListFragment(ArrayList<DataItem> dataItems) {
this.dataItems.addAll(dataItems);
}
为了确保选择选项卡时我拥有最新的arraylist,我在活动中写下了以下代码...
dlBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
dlAdapter.refreshFragmentDataItems(tab.getPosition());
}
});
来自Adapter的相关代码:
public void refreshFragmentDataItems(int position) {
ArrayList<DataItem> dataItems;
dataItems = dbAdapter.getRecentDataItems(null);
fragments[position].setDataItems(dataItems);
}
最后是setDataitems()代码:
public void setDataItems(ArrayList<DataItem> dataItems) {
this.dataItems.clear();
this.dataItems.addAll(dataItems);
recyclerAdapter.notifyDataSetChanged();
}
当我调试代码时,即使不执行refreshfragmentDataitems()
函数,我注意到fragments [position] .dataitems
variable具有考虑到任何添加的添加和从阵列列表中删除项目后的最新项目列表。
尽管如此,它仍继续显示原始列表。我试图移动notifydatasetchanged()呼叫无济于事。在某些情况下,它将成为一个永远的循环。
在我选择另一个选项卡时我的回收器视图中的列表中没有更新的内容时,会感谢任何输入。
编辑:在进一步诊断时,我发现问题是由于搜索功能引起的。
我有一个QueryTextListener,并在OnqueryTextChange中评论了代码解决了问题。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager inm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
dlAdapter.getFragment(dlBinding.tabs.getSelectedTabPosition()).filterAdapter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
//comment below line to fix issue
dladapter.getFragment(dlbinding.tabs.getSelectedTabPosition())。filterAdapter(newText); 返回true; } });
这是过滤回收库适配器的功能。
public void filterAdapter(String query) {
recyclerAdapter.getFilter().filter(query);
}
当我在上述两个函数中调试所有步骤时,手表窗格中的项目计数反映了实际计数,但是当调试步骤完成时,它仍然显示出较早的删除项目。
In my android app, I am using a TabLayout and ViewPager2 to show different categories of lists using RecyclerView. When I do additions and deletions and then call notifyDataSetChanged
, the list changes while I am still on the current tab. However, when I select another tab and return, the list shows the original items that were initialised while creating the fragment.
In the createFragment
function of the FragmentStateAdapter
adapter class, I initialise a fragment with the starting data item arraylist as follows.
public class DataListAdapter extends FragmentStateAdapter {
private DataListFragment[] fragments;
//
// other code
//
@Override
public Fragment createFragment(int position) {
ArrayList<DataItem> dataItems;
//The actual code returns a different data item based on conditions
dataItems = dbAdapter.getDataItems();
DataListFragment frag = new DataListFragment(dataItems);
fragments[position] = frag;
return frag;
}
}
In the DataListFragment I have...
DataListFragment(ArrayList<DataItem> dataItems) {
this.dataItems.addAll(dataItems);
}
In order to ensure I have the latest ArrayList when a tab is selected, I wrote the following code in my activity...
dlBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
dlAdapter.refreshFragmentDataItems(tab.getPosition());
}
});
Relevant code from adapter:
public void refreshFragmentDataItems(int position) {
ArrayList<DataItem> dataItems;
dataItems = dbAdapter.getRecentDataItems(null);
fragments[position].setDataItems(dataItems);
}
And finally the setDataItems()
code from the Fragment class:
public void setDataItems(ArrayList<DataItem> dataItems) {
this.dataItems.clear();
this.dataItems.addAll(dataItems);
recyclerAdapter.notifyDataSetChanged();
}
When I debug the code, even without executing the refreshFragmentDataItems()
function, I noticed that the fragments[position].dataItems
variable had the most recent list of items after factoring in any additions and removal of items from the ArrayList.
Nevertheless it continues to display the original list. I tried to move the notifyDataSetChanged() call around to no avail. In some cases it was going into a forever loop.
Would appreciate any inputs in figuring out why the list in my recycler views are not updating when I select another tab.
EDIT: On further diagnosis, I found the issue is caused due to the search feature.
I have a QueryTextListener, and commenting out the code in OnQueryTextChange fixes the problem.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager inm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
dlAdapter.getFragment(dlBinding.tabs.getSelectedTabPosition()).filterAdapter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
//comment below line to fix issue
dlAdapter.getFragment(dlBinding.tabs.getSelectedTabPosition()).filterAdapter(newText);
return true;
}
});
This is the function that filters the RecyclerView adapter.
public void filterAdapter(String query) {
recyclerAdapter.getFilter().filter(query);
}
When I debug all the steps in the above two functions, the item count in the watch pane reflects the actual count, but when the debugging steps are finished, it still shows an earlier deleted item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
供将来使用,如果您在同一活动中的片段之间切换,则可以按以下方式执行:
在您的
onstop()/ondestroyview()/onpause()/onpause()
什么更合适的情况下,请执行此操作:在您的
onstart()/oncreateview()/onResume()
无论回调更合适的情况下,执行此操作:这样,您就可以成功地将RecyClerview的状态保持在可穿衣的状态,并随时恢复它。
别忘了添加:
For future use, If you are switching between Fragments within the same activity, you can do as follows:
In your
onStop()/onDestroyView()/onPause()
whatever callback is more appropriate, do this:And In your
onStart()/onCreateView()/onResume()
whatever callback is more appropriate, do this:This way you can successfully keep your recyclerView's state in a parcelable and restore it whenever you want.
Don't forget to add: