RecyclerView.Adapter通过位置获取ItemView

时间:2016-12-15 10:15:17

标签: android-recyclerview android-adapter

在我的适配器中,我致电${view}以获取LayoutManager.ChildAt(position),但我获得的视图不是匹配的itemview,当我拨打itemview时,应用崩溃了:

notifyItemChanged(position)

谢谢,这是我在适配器中的代码:

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.paicaifu.riches, PID: 8502
Java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{adb21588 position=0 id=-1, oldPos=-1, pLpos:-1}
at Android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:654)
at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
at android.support.v7.widget.RecyclerView.addAnimatingView(RecyclerView.java:1107)
at android.support.v7.widget.RecyclerView.animateChange(RecyclerView.java:3270)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3088)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2917)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
…
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 

我只想查找最后一项并更改它的状态(打开或关闭),当项目超出屏幕时,notifyItemChanged(int position)将出现问题。

1 个答案:

答案 0 :(得分:4)

这可能有点太晚了,但是当你没有在onCreateViewHolder中创建新视图而是返回已经膨胀的缓存视图时,通常会发生崩溃“调用附着在未分离的子节点上”并附上。