Three ways to implement single selection in RecyclerView

Today, I will share with you three ways to implement single selection in RecyclerView. All three ways require an identification bit, mPosition

If you have a single choice, it is recommended that you do not use checkBox (checkBox is displayed differently in different versions of android). It is ideal to use an ImageView instead.

In the Adapter, first initialize our identification bit int mPosition=-1 ;

The first way is to refresh the list, use notifyDataSetChanged();

Paste the code below

if (mPosition == position) {
    myviewholders.mIvCheck.setImageResource(R.mipmap.icon_no);
mPosition = -1;
} else if (mPosition != position) {
    myviewholders.mIvCheck.setImageResource(R.mipmap.icon);
mPosition = position;
notifyDataSetChanged();
}            

This method of implementation is simple and rude, but it takes up a lot of memory. Every time the selection is changed, it will go from onBindViewHolder to a screen entry several times.

The second way is to refresh a single Item event, use notifyItemChanged(positio)

code show as below

if ( mPosition == position ) {
     myList .get( mPosition ).setCheck( false ) ;
     notifyItemChanged( position ) ;
 mPosition = - 1 ;
 } else if ( mPosition != position && mPosition != - 1 ) {
     // cancel first The check state of the previous item notifyItemChanged( mPosition ) ;
 //Set the check state of the new item
 mPosition = position ; notifyItemChanged( position    
            
    );
} else if (mPosition == -1) {
    mPosition = position;
myList.get(position).setCheck(true);
notifyItemChanged(position);
}        

This method does not take up much memory and only refreshes a single Item

The above two ways to achieve RecyclerView single selection have a defect, that is, if there is a picture in the Item, there will be a flicker of switching pictures

Let's start zooming in

The third implementation only changes the select button in a single Item setImageResource() is    highly recommended

code show as below

if (mPosition == position) {
    Myviewholders vhNew = (Myviewholders) myRecycle.findViewHolderForLayoutPosition(mPosition);
    vhNew.mIvCheck.setImageResource(R.mipmap.icon_no);
mPosition = -1;
} else if (mPosition != -1) {    
    Myviewholders couponVH = (Myviewholders) myRecycle .findViewHolderForLayoutPosition( mPosition ) ;
     if ( couponVH != null ) { //still in the screen
         couponVH.mIvCheck.setImageResource (R.mipmap.icon_no ) ; }
 else { //
         some extreme cases, holder Cached in Recycler's cacheView,
         //The ViewHolder cannot be obtained at this time, but the onBindViewHolder method will not be called back. So add an exception handler
 notifyItemChanged( mPosition ) ;
 }
     //Set the check state of the new Item
 mPosition = position ;
 Myviewholders vhNew = (Myviewholders)                        myRecycle.findViewHolderForLayoutPosition(mPosition);
    vhNew.mIvCheck.setImageResource(R.mipmap.icon);
} else if (mPosition == -1) {
    Myviewholders couponVH = (Myviewholders) myRecycle .findViewHolderForLayoutPosition( position ) ;
 //Set the check state of the new item
 mPosition = position ;
 couponVH. mIvCheck .setImageResource(R.mipmap. icon ) ;
 }            

这种实现方式唯一的缺陷就是要在创建Adapter的时候要传入RecyclerView的对象,用来获取到单个Item的findViewHolderForLayoutPosition(position),有了单个Item的实例我们就可以操作item的View了,此方法可能很多人没有见过吧,因为用的确实比较少,代码量也比较多;但这确实是RecyclerView的高级阶段才用的到的哦

切换单选标记的ImageView的代码也贴出来啦,要是有需要源码的可以留言哦

myviewholders.mIvCheck.setImageResource(R.mipmap.icon_no);
//先重置一下选择标记为未选择
if (mPosition == position) {
    myviewholders.mIvCheck.setImageResource(R.mipmap.icon);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324609&siteId=291194637