实现RecycleView动态使列表item可以点击或不可点击切换

效果

这里讲的是第二个button跳转的Activity,这里和上一篇不同之处在于可以item点击、item子控件点击

image

继承BaseAdapter

同样也要继承BaseAdapter

public class ADUadapter extends BaseAdapter<ChatModel> {
    public Context context;
    protected ADUholder holder;

    public ADUadapter(Context context) {
        super();
        this.context = context;
    }

    @Override
    protected void setItemChildListener(BaseViewHolder viewHolder) {
        super.setItemChildListener(viewHolder);
        if (viewHolder instanceof ADUholder){
            ADUholder adUholder = (ADUholder) viewHolder;
            baseViewHolderHelper.setItemChildClickListener(adUholder.imageView);
        }
    }

    @Override
    protected BaseViewHolder addViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        return new ADUholder(view);
    }

    @Override
    protected void fillData(RecyclerView.ViewHolder holder, final int position, final ChatModel item) {
        this.holder = (ADUholder) holder;
        this.holder.title.setText(item.name);
        this.holder.name.setText(item.mMsg);
    }
}

重写setItemChildListener()

这里可以实现item子控件的点击效果,我们仅仅需要baseViewHolderHelper.setItemChildClickListener(adUholder.imageView);这里的参数就是我们让该控件实现点击效果。

在Activity中实现GIF中的效果

实现item点击效果

adUadapter.setOnItemClickListener(new BaseViewHolder.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(UpdataItemActivity.this, "position: " + position, Toast.LENGTH_SHORT).show();
            }
        });

实现item长按效果

adUadapter.setOnLongItemClickListener(new BaseViewHolder.OnLongItemClickListener() {
            @Override
            public boolean onLongItemClick(View view, int position) {
                String title = adUadapter.getItem(position).name;
                Toast.makeText(UpdataItemActivity.this, title + ",position: " + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

实现item子控件点击效果

adUadapter.setOnChildItemClickListener(new BaseViewHolderHelper.OnChildItemClickListener() {
            @Override
            public void onChildItemClick(View view, int position) {
                String title = adUadapter.getItem(position).name;
                Toast.makeText(UpdataItemActivity.this, title + ",你点了图片,position: " + position, Toast.LENGTH_SHORT).show();
            }
        });

实现item点击或不可点击切换

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.radioButton) {
                    adUadapter.getViewHolder().setItemable(true, true);
                } else if (i == R.id.radioButton2) {
                    adUadapter.getViewHolder().setItemable(false, false);
                }
            }
        });

好了,在此附上github源码,喜欢的请start、fork。 https://github.com/DyncKathline/TestRecyclerView

转载请注明出处,谢谢!

posted @ 2016-09-11 21:30  DyncKathline  阅读(5103)  评论(0编辑  收藏  举报