-
前言
簡單的實現(xiàn)Recyclerview選中效果杠输,開發(fā)中很常見,比如點擊item歪玲,后面加上選擇按鈕迁央,亦或者類似CheckBox效果,可以用很少幾行代碼實現(xiàn)
-
實現(xiàn)
直接貼代碼滥崩,布局很簡單岖圈,一個文字,后面一個選擇按鈕
<?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>```
實現(xiàn)選擇功能钙皮,在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; //默認(rèn)一個參數(shù)
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賦值給參數(shù)蜂科,
notifyItemChanged(selectedPosition);//刷新當(dāng)前點擊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 { //定義接口,實現(xiàn)Recyclerview點擊事件
void OnItemClick(View view, StateHolder holder, int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) { //實現(xiàn)點擊
this.onItemClickListener = onItemClickListener;
}
}
item點擊時短条,就可以簡單實現(xiàn)點擊選中效果