简体   繁体   中英

Disable click on RecyclerView inside a SwipeRefreshLayout

I implemented a SwipeRefreshLayout using a RecyclerView and I need that my adapter items are disabled during the OnRefreshListener .

I tried the following approach, but the click occurs normally:

mRecyclerView.setEnabled(false);
mRecyclerView.setClickable(false);

Use logic we had with ListAdapter . This will disable adapter items, instead their parent.

public interface RecyclerViewItemEnabler{
  public boolean isAllItemsEnabled();
  public boolean getItemEnabled(int position);
}

And implementation should look like this:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements RecyclerViewItemEnabler{

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.itemView.setEnabled(isAllItemsEnabled());
        //or do this in onBindViewHolder()
    }
    @Override
    public boolean isAllItemsEnabled(){ return mAllEnabled; }

    @Override
    public boolean getItemEnabled(int position){
       return true;
    }
    public void setAllItemsEnabled(boolean enable){
      mAllEnabled = enable;
      notifyItemRangeChanged(0, getItemCount());
    }

}

Usage: mRecylerAdapter.setAllItemsEnabled(!mSwipeRefreshLayout.isRefreshing());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM