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

效果

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

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

继承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

转载请注明出处,谢谢!

上一篇:Myeclipse 2014 javascript 添加 jquery 代码提示


下一篇:白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连续7-电话问卷调查