我的RecyclerView
包含单选按钮。当我单击一个时,我的以下实现应取消选择所有其他实现(notifyDataSetChanged()
应该重新绑定所有行)。令人惊讶的是,onClick
永远不会被触发:
public class NameAdapter extends RecyclerView.Adapter<NameAdapter.NameHolder> {
private List<String> myNames;
private int selectedPosition = -1;
public NameAdapter(List<String> names) {
myNames = names;
}
@Override public NameHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.name_item, parent, false);
return new NameHolder(view);
}
@Override public void onBindViewHolder(NameHolder holder, int position) {
String name = myNames.get(position);
holder.name.setText(name);
holder.radioButton.setChecked(position == selectedPosition);
}
@Override public int getItemCount() {
return myNames.size();
}
public class NameHolder extends RecyclerView.ViewHolder {
private TextView name;
private RadioButton radioButton;
public NameHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
radioButton = (RadioButton) itemView.findViewById(R.id.radio_button);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override public void onClick(View v) {
selectedPosition = getAdapterPosition();
notifyDataSetChanged();
}
};
itemView.setOnClickListener(clickListener);
}
}
}
答案 0 :(得分:0)
使用以下方法之一触发点击事件。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}