单击任何RecyclerView项时,其给出错误

时间:2019-07-19 07:43:03

标签: android android-recyclerview recycler-adapter

我已在 RecyclerView 中添加了onClick侦听器, onBindViewHolder ,我切换了复选框的选定状态,但它引发了以下错误废弃或附加的视图可能无法回收< / p>

ViewHolder

中只有一个复选框
override fun onBindViewHolder(holder: BookingSlotListItemViewHolder, position: Int) { 
    holder.timeSlot.setOnClickListener {  view: View? -> 
    holder.timeSlot.toggle()
  }
}

抛出此错误

E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.debugapp.debug, PID: 24546
        java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true androidx.recyclerview.widget.RecyclerView{3bf1da4 VFED..... ........ 32,625-1016,767 #7f0a003b app:id/booking_slot_list}, adapter:com.debugapp.v2.bookings.BookingSlotListAdapter@43fc70d, layout:androidx.recyclerview.widget.LinearLayoutManager@edbe6c2, context:com.debugapp.v2.BookingActivity@6ec8476
            at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:6159)
            at androidx.recyclerview.widget.RecyclerView.removeAnimatingView(RecyclerView.java:1451)
            at androidx.recyclerview.widget.RecyclerView$ItemAnimatorRestoreListener.onAnimationFinished(RecyclerView.java:12454)
            at androidx.recyclerview.widget.RecyclerView$ItemAnimator.dispatchAnimationFinished(RecyclerView.java:12954)
            at androidx.recyclerview.widget.SimpleItemAnimator.dispatchMoveFinished(SimpleItemAnimator.java:292)
            at androidx.recyclerview.widget.DefaultItemAnimator.animateMove(DefaultItemAnimator.java:264)
            at androidx.recyclerview.widget.DefaultItemAnimator.animateChange(DefaultItemAnimator.java:324)
            at androidx.recyclerview.widget.SimpleItemAnimator.animateChange(SimpleItemAnimator.java:161)
            at androidx.recyclerview.widget.RecyclerView.animateChange(RecyclerView.java:4186)
            at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3984)
            at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3652)
            at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
            at android.view.View.layout(View.java:20831)
            at android.view.ViewGroup.layout(ViewGroup.java:6203)
            at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
            at android.view.View.layout(View.java:20831)
            at android.view.ViewGroup.layout(ViewGroup.java:6203)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
            at android.view.View.layout(View.java:20831)
            at android.view.ViewGroup.layout(ViewGroup.java:6203)
            at androidx.recyclerview.widget.RecyclerView$LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:9322)
            at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1615)
            at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
            at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
            at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
            at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
            at android.view.View.measure(View.java:23355)
            at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
            at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
            at android.view.View.measure(View.java:23355)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
            at android.view.View.measure(View.java:23355)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
            at android.view.View.measure(View.java:23355)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.view.View.measure(View.java:23355)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)

3 个答案:

答案 0 :(得分:1)

尝试使用

override fun onBindViewHolder(holder: BookingSlotListItemViewHolder, position: Int) { 
    holder.timeSlot.setOnClickListener {  view: View? -> 
        if (view is CheckBox) {
            view.toggle()
        }
    }
}

相反。

答案 1 :(得分:1)

使用setOnCheckListener代替setOnClickListener

或尝试

mGridLayoutManager.setItemPrefetchEnabled(false);

答案 2 :(得分:0)

尝试在ViewHolder的init函数中设置OnClickListener。