回收器视图 - 动态向适配器添加行



我创建了一个 swıpe 视图布局并将回收器视图添加到其中。我的代码如下:

swipeRefreshLayout = new SwipeRefreshLayout(activity);
theme = new ContextThemeWrapper(activity, R.style.ScrollBarRecyclerView);
recyclerView = new RecyclerView(theme);
linearLayoutManager = new LinearLayoutManager(activity);
recyclerView.setLayoutManager(linearLayoutManager);
swipeRefreshLayout.addView(recyclerView);
recyclerView.setAdapter(dataAdapter);

我尝试动态添加行(列表视图项目)。每行的高度可能不同。我通过重载 onCreateViewHolder、onBindViewHolder、getCount 和 getItemViewType 方法来实现适配器类。

public void addItem() {
// add item to items
dataAdapter.notifyItemInserted(items.length-1);
}

它适用于 3-4 个项目。当需要滚动以显示更多行时,应用程序将崩溃。日志猫的输出在这里:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{126d708 position=4 id=-1, oldPos=3, pLpos:3 scrap [attachedScrap] tmpDetached no parent}
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5046)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5177)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3330)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3186)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1595)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4560)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.view.Choreographer.doCallbacks(Choreographer.java:683)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.view.Choreographer.doFrame(Choreographer.java:616)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:751)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:154)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6077)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
09-25 13:54:23.406 12444 12444 E AndroidRuntime:    at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

如何将项目添加到动态上的可滚动回收器视图?

在您的活动中itemlist附加一个item,然后只需调用mAdapter.notifyDatasetChanged();仅此而已。

最新更新