Select recyclerview 适配器中适配器中的项目 android

Select item in adapter in recyclerview adapter android

我想在回收站视图中为我的列表创建 selector in 适配器。 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());


            }
        }
    }

如您所见,我更改了 selected 的背景,但我不知道旧的 selection 有多清晰。有什么想法吗?

你需要为每个案例设置背景,只要验证该行是否是选中的项目,他们就会改变他的背景颜色。

样本:

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));
            }
        }