gpt4 book ai didi

android - 在recyclerview适配器android中选择适配器中的项目

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:08 24 4
gpt4 key购买 nike

我想在回收器 View 中为我的列表在适配器中创建选择器。 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());


}
}
}

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

最佳答案

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

示例:

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

绑定(bind)数据时:

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

关于android - 在recyclerview适配器android中选择适配器中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31573054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com