最近先来无事把以前项目中用到的比较常用的功能抽取出来整理成一个demo,以后用到的时候直接拿来用,也希望能帮到其他人。整理的可能不太好,还望各位指正。
RecyclerView单选
- Adapter类
public class SingleAdapter extends RecyclerView.Adapter {
private List<String> datas;
private int selected = -1;
public SingleAdapter(List<String> datas) {
this.datas = datas;
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
{
this.mOnItemClickLitener = mOnItemClickLitener;
}
public void setSelection(int position){
this.selected = position;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new SingleViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof SingleViewHolder){
final SingleViewHolder viewHolder = (SingleViewHolder) holder;
String name = datas.get(position);
viewHolder.mTvName.setText(name);
if(selected == position){
viewHolder.mCheckBox.setChecked(true);
viewHolder.itemView.setSelected(true);
} else {
viewHolder.mCheckBox.setChecked(false);
viewHolder.itemView.setSelected(false);
}
if (mOnItemClickLitener != null)
{
viewHolder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mOnItemClickLitener.onItemClick(viewHolder.itemView, viewHolder.getAdapterPosition());
}
});
}
}
}
@Override
public int getItemCount() {
return datas.size();
}
class SingleViewHolder extends RecyclerView.ViewHolder{
TextView mTvName;
CheckBox mCheckBox;