-
前言
简单的实现Recyclerview选中效果,开发中很常见,比如点击item,后面加上选择按钮,亦或者类似CheckBox效果,可以用很少几行代码实现
-
实现
直接贴代码,布局很简单,一个文字,后面一个选择按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_state"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:textColor="@color/state_visibility"
android:textSize="18sp"
tools:text="@string/state" />
<ImageView
android:id="@+id/iv_selected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="invisible"
android:src="@drawable/icon_selected" />
</LinearLayout>```
实现选择功能,在Adapter中
/**
- Author : ddz
- Creation time : 2017/3/1 17:08
- Fix time : 2017/3/1 17:08
*/
public class CheckStateAdapter extends RecyclerView.Adapter<CheckStateAdapter.StateHolder> {
private Context context;
private String[] arrayState;
private int selectedPosition = -5; //默认一个参数
public CheckStateAdapter(@NonNull String[] arrayState, Context context) {
this.context = context;
this.arrayState = arrayState;
}
@Override
public StateHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new StateHolder(LayoutInflater.from(context).inflate(R.layout.state_item, parent, false));
}
@Override
public void onBindViewHolder(StateHolder holder, int position) {
if (null == arrayState) return;
holder.itemView.setSelected(selectedPosition == position);
if (selectedPosition == position) {
holder.ivSelected.setVisibility(View.VISIBLE);
} else {
holder.ivSelected.setVisibility(View.INVISIBLE);
}
holder.tvState.setText(arrayState[position]);
holder.itemView.setOnClickListener(view -> {
onItemClickListener.OnItemClick(view, holder, holder.getAdapterPosition());
selectedPosition = position; //选择的position赋值给参数,
notifyItemChanged(selectedPosition);//刷新当前点击item
});
}
@Override
public int getItemCount() {
return arrayState.length == 0 ? 0 : arrayState.length;
}
public void updateData(String[] airPortModels) {
if (null != arrayState) {
selectedPosition = -5;
arrayState = null;
arrayState = airPortModels;
notifyDataSetChanged();
}
}
public class StateHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv_state)
TextView tvState;
@BindView(R.id.iv_selected)
ImageView ivSelected;
public StateHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
private OnItemClickListener onItemClickListener;
public interface OnItemClickListener { //定义接口,实现Recyclerview点击事件
void OnItemClick(View view, StateHolder holder, int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) { //实现点击
this.onItemClickListener = onItemClickListener;
}
}
item点击时,就可以简单实现点击选中效果
网友评论
顺便说一下, 我的需求比较简单, 只需要记录并更新一个选中状态. 所以我在更新的时候没有使用 notifyDataSetChanged() , 感觉太重了. 于是改进了一下.
/** 外部更新选中的Item
* @param position
*/
public void updateSelectedPosition(int position){
int temp = selectedPosition;
selectedPosition = position;
notifyItemChanged(temp);
notifyItemChanged(selectedPosition);
}
selectedPosition = -5;
不是两行吗?