你们可以帮助我吗
这是我添加片段的代码
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (!IsScrolling) {
Fragment fragment = new ProductInfoFragment();
Bundle bundle = new Bundle();
bundle.putString("prodID", mItems.get(position).getproductID());
bundle.putString("catName", catName);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(R.id.fragmentContainer, fragment, "ProuctInfoFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
mItems.clear();
}
}
})
);
但是当我从添加的片段中按回来时,我收到了此错误并且我的应用程序崩溃了。
FATAL EXCEPTION: main Process: com.tiseno.poplook, PID: 2617
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 5(offset:5).state:9
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4401)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3807)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5514)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
为什么会这样?请帮忙。我正在使用两个片段。产品列表fragent和产品详细信息片段
答案 0 :(得分:28)
您在未通知mItems.clear();
适配器的情况下呼叫RecyclerView
数据中的某些内容已更改。
每次更改适配器使用的数据后,您需要调用adapter.notifyDataSetChanged();
请参阅this。
答案 1 :(得分:4)
我遇到了同样的问题,当您清除列表时,您必须通知适配器,因为每次更改列表并且适配器包含旧列表时,您必须在添加新列表之前清除列表后通知适配器适配器中的项目
在您的课程开始时创建数据成员的实例
ArrayList<HashMap<String,String>> yourarraylist =new ArrayList<>();
Youradaptername instance;
通知适配器
yourarraylist .clear();
instance.notifyDataSetChanged();
答案 2 :(得分:0)
问题可能与适配器有关。如果在适配器构造函数中使用setHasStableIds(true);
,则需要确保使用stable ids。
确保在适配器中正确覆盖getItemId
。
应该是:
@Override
public long getItemId(int position) {
return yourList == null ? 0 : yourList.get(position).getId();
}
不是:
@Override
public long getItemId(int position) {
return position;
}