在recyclerview adapter android中选择adapter中的项
我想在适配器中为“回收器”视图中的列表创建选择器。我需要改变背景时,选择项目,但当选择其他项目旧的选择应该是明确的。 这是我的适配器:在recyclerview adapter android中选择adapter中的项,android,android-recyclerview,Android,Android Recyclerview,我想在适配器中为“回收器”视图中的列表创建选择器。我需要改变背景时,选择项目,但当选择其他项目旧的选择应该是明确的。 这是我的适配器: public class BTDevicesAdapter extends RecyclerView.Adapter<BTDevicesAdapter.BaseHolder>{ private ArrayList<BluetoothDevice> devices; private Context ctx; pri
public class BTDevicesAdapter extends RecyclerView.Adapter<BTDevicesAdapter.BaseHolder>{
private ArrayList<BluetoothDevice> devices;
private Context ctx;
private BluetoothPreferences bluetoothPreferences = null;
public BTDevicesAdapter(ArrayList<BluetoothDevice> devices, Context ctx) {
this.devices = devices;
this.ctx = ctx;
bluetoothPreferences = new BluetoothPreferences(ctx);
}
@Override
public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ElementHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_bt_device, parent, false));
}
@Override
public void onBindViewHolder(final BaseHolder holder, final int position) {
final BluetoothDevice device = devices.get(position);
holder.bindItem(position, device);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setBackgroundColor(ctx.getResources().getColor(R.color.cardview_shadow_start_color));
bluetoothPreferences.setBluetoothName(device.getName());
bluetoothPreferences.setBluetoothAddress(device.getAddress());
}
});
}
@Override
public int getItemCount() {
return devices.size();
}
public static abstract class BaseHolder extends RecyclerView.ViewHolder {
public BaseHolder(View itemView) {
super(itemView);
}
public abstract void bindItem(int position, BluetoothDevice device);
}
public static class ElementHolder extends BaseHolder {
@InjectView(R.id.btDeviceName)
TextView name;
@InjectView(R.id.btDeviceAddress) TextView address;
public ElementHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}
@Override
public void bindItem(int position, BluetoothDevice device) {
name.setText(device.getName());
address.setText(device.getAddress());
}
}
}
公共类BTDeviceAdapter扩展了RecyclerView.Adapter{
专用阵列列表设备;
私有上下文ctx;
私有BluetoothPreferences BluetoothPreferences=null;
公共BTDeviceAdapter(ArrayList设备,上下文ctx){
这个。设备=设备;
this.ctx=ctx;
bluetoothPreferences=新的bluetoothPreferences(ctx);
}
@凌驾
public BaseHolder onCreateViewHolder(视图组父级,int-viewType){
返回新的ElementHolder(LayoutInflater.from(parent.getContext())
.充气(R.layout.item_list_bt_device,父项,false));
}
@凌驾
public void onBindViewHolder(最终BaseHolder,最终int位置){
最终蓝牙设备=设备。获取(位置);
夹持器。夹持件(位置、装置);
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
view.setBackgroundColor(ctx.getResources().getColor(R.color.cardview\u shadow\u start\u color));
bluetoothPreferences.setBluetoothName(device.getName());
bluetoothPreferences.setBluetoothAddress(device.getAddress());
}
});
}
@凌驾
public int getItemCount(){
返回设备。size();
}
公共静态抽象类BaseHolder扩展了RecyclerView.ViewHolder{
公共基地持有者(查看项目视图){
超级(项目视图);
}
公共抽象项(int位置,蓝牙设备);
}
公共静态类ElementHolder扩展了BaseHolder{
@InjectView(R.id.btDeviceName)
文本视图名称;
@InjectView(R.id.btDeviceAddress)文本视图地址;
公共元素持有者(视图项视图){
超级(项目视图);
ButterKnife.inject(这个,itemView);
}
@凌驾
公共void绑定项(int位置,Bluetooth设备){
name.setText(device.getName());
address.setText(device.getAddress());
}
}
}
正如你们所看到的,我改变了被选中的背景,但我不知道旧的选择有多清晰。有什么想法吗?您需要为每个案例设置背景,只需验证该行是否为选中项,他们就会更改其背景颜色 样本:
private int clickedPosition;
@Override
public void onBindViewHolder(final BaseHolder holder, final int position) {
final BluetoothDevice device = devices.get(position);
holder.bindItem(position, device);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//set the position
clickedPosition = position;
bluetoothPreferences.setBluetoothName(device.getName());
bluetoothPreferences.setBluetoothAddress(device.getAddress());
//notify the data has changed
notifyDataSetChanged();
}
});
}
绑定数据时:
@Override
public void bindItem(int position, BluetoothDevice device) {
name.setText(device.getName());
address.setText(device.getAddress());
//view is the holder view param when is create
//you need store or get access
if(position==clickedPosition){
view.setBackgroundColor(ctx.getResources().getColor(R.color.cardview_shadow_start_color));
}else{
view.setBackgroundColor(ctx.getResources().getColor(R.color.default_color));
}
}
您的解决方案很好,但需要调用notifyDataSetChanged();在我的听众中。谢谢!