gpt4 book ai didi

android - 在 RecyclerView 中检测到不一致,如何在滚动时更改 RecyclerView 的内容

转载 作者:IT老高 更新时间:2023-10-28 13:25:11 25 4
gpt4 key购买 nike

我正在使用 RecyclerView显示项目的名称。我的行包含单个 TextView .项目名称存储在 List<String> mItemList .

更改RecyclerView 的内容, 我替换 mItemList 中的字符串并调用notifyDataSetChanged () 上 RecyclerViewAdapter .

但是如果我尝试更改 mItemList 的内容当 RecyclerView 滚动时,有时它给了我 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588

如果大小为 mItemList,则会发生这种情况比以前少了。那么更改 RecyclerView 内容的正确方法是什么? ?这是 RecyclerView 中的错误吗? ?

这是异常的完整堆栈跟踪:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 157(offset:157).state:588
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3300)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1093)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:956)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:2715)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)

AdapterView 代码:

private static class FileListAdapter extends RecyclerView.Adapter<FileHolder> {
private final Context mContext;
private final SparseBooleanArray mSelectedArray;
private final List<String> mList;

FileListAdapter(Context context, List<String> list, SparseBooleanArray selectedArray) {
mList = list;
mContext = context;
mSelectedArray = selectedArray;
}


@Override
public FileHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.file_list_item, viewGroup, false);

TextView tv = (TextView) view
.findViewById(R.id.file_name_text);
Typeface font = Typeface.createFromAsset(viewGroup.getContext().getAssets(),
viewGroup.getContext().getString(R.string.roboto_regular));
tv.setTypeface(font);

return new FileHolder(view, tv);
}

@Override
public void onBindViewHolder(FileHolder fileHolder, final int i) {

String name = mList.get(i);

// highlight view if selected
setSelected(fileHolder.itemView, mSelectedArray.get(i));

// Set text
fileHolder.mTextView.setText(name);
}

@Override
public int getItemCount() {
return mList.size();
}
}

private static class FileHolder extends RecyclerView.ViewHolder {

public final TextView mTextView;

public FileHolder(View itemView, TextView tv) {
super(itemView);
mTextView = tv;
}
}

最佳答案

编辑:该错误现已修复,如果您仍然遇到相同的异常,请确保您仅从主线程更新您的适配器数据源并调用适当的适配器通知方法之后。

老答案: 好像是RecyclerView的一个bug,据报herehere .希望它会在下一个版本中得到修复。

关于android - 在 RecyclerView 中检测到不一致,如何在滚动时更改 RecyclerView 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com