【问题标题】:Select item in adapter in recyclerview adapter android在recyclerview适配器android中选择适配器中的项目
【发布时间】: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


    【解决方案1】:

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

    示例:

    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();在 onClickListener 中。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多