本篇文章主要记录RecycleView实现单选功能
应用场景:需获取不定量的销售人员数据,实现单选一个销售
使用RecycleView首先得引入
implementation 'com.android.support:recyclerview-v7:28.0.0'
RecycleView布局
<?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="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycle_view">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
RecycleView_item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:background="@drawable/select_false_button"
android:button="@null"
android:id="@+id/recycle_text"/>
</LinearLayout>
适配器adapter
public class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.MyViewHolder>
{
private List<String> mData;
private Context mContext;
private int defItem = -1;//默认值
private OnItemListener onItemListener;
public BaseAdapter(List<String> data, Context context)
{
mData = data;
mContext = context;
}
public void setOnItemListener(OnItemListener onItemListener)
{
this.onItemListener = onItemListener;
}
//设置点击事件
public interface OnItemListener
{
void onClick(View v, int pos);
}
//获取点击的位置
public void setDefSelect(int position)
{
this.defItem = position;
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position)
{
MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(mContext)
.inflate(R.layout.recycle_item,viewGroup,false));
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder barberListViewHolder,final int position)
{
barberListViewHolder.mTextView.setText(mData.get(position));
if (defItem != -1)
{
/*点的位置跟点击的textview位置一样设置点击后的不同样式*/
if (defItem == position)
{
/*设置选中的样式*/
barberListViewHolder.mTextView.setBackground(mContext.getResources().getDrawable(R.drawable.select_true_button));
barberListViewHolder.mTextView.setTextColor(Color.parseColor("#FFFFFF"));
} else {
/*其他的变为未选择状态
*设置未选中的样式
*/
barberListViewHolder.mTextView.setBackground(mContext.getResources().getDrawable(R.drawable.select_false_button));
barberListViewHolder.mTextView.setTextColor(Color.parseColor("#000000"));
}
}
}
@Override
public int getItemCount()
{
return mData.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
TextView mTextView;
public MyViewHolder(@NonNull View itemView)
{
super(itemView);
mTextView = itemView.findViewById(R.id.recycle_text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemListener != null) {
onItemListener.onClick(v, getLayoutPosition());
}
}
});
}
}
}
drawable选中和未选中的样式
选中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorOrange"/>
<corners android:radius="30dp"/>
</shape>
未选中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorGray" />
<corners android:radius="30dp"/>
</shape>
Activity操作
public class MainActivity extends AppCompatActivity {
@BindView(R.id.recycle_view)
RecyclerView mRecyclerView;
private List<String> mData;
private BaseAdapter mBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
initView(this);
}
/*初始化数据*/
public void initData()
{
mData = new ArrayList<>();
for (int i = 0;i < 10;i++)
{
mData.add("" + i);
}
}
public void initView(final Context context)
{
mBaseAdapter = new BaseAdapter(mData,this);
/*设置RecycleView布局*/
mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
/*设置RecycleView适配器*/
mRecyclerView.setAdapter(mBaseAdapter);
mBaseAdapter.setOnItemListener(new BaseAdapter.OnItemListener()
{
@Override
public void onClick(View v, int pos)
{
/*点击事件*/
mBaseAdapter.setDefSelect(pos);
}
});
}
}
至此可完成实现单选功能
如有错误请指出