【发布时间】:2015-07-23 05:15:21
【问题描述】:
我想在适配器中为回收站视图中的列表创建选择器。 I need to change background when select item, but when choose other item old selection should be clear. 这是我的适配器:
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());
}
}
}
如您所见,我更改了选定的背景,但我不知道旧的选择有多清晰。有什么想法吗?
【问题讨论】:
标签: android android-recyclerview