RecyclerView - 动态地向适配器添加行

时间:2017-09-25 11:05:33

标签: android android-layout android-recyclerview swiperefreshlayout

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

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);

我尝试动态添加row(listview项)。每行的高度可以不同。我通过重载onCreateViewHolder,onBindViewHolder,getCount和getItemViewType方法来实现一个适配器类。

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

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

 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)

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

1 个答案:

答案 0 :(得分:0)

在您的item的活动中附加itemlist,然后只需致电mAdapter.notifyDatasetChanged();即可。