我已经创建了回收 View 。回收 View 包含复选框。如果我单击回收 View 中的第一个项目,则复选框被选中,然后我单击第二个项目意味着第一个和第二个项目复选框被选中。它工作正常,但我如果我单击第二个项目复选框,我想取消选中第一个选中的项目。`
public static class AddressAdapter1 extends RecyclerView.Adapter<AddressAdapter1.MyViewHolder> {
private int lastSelectedPosition = -1;
private final Context mcontext;
private AdapterView.OnItemClickListener onItemClickListener;
private AdapterView.OnItemLongClickListener onItemLongClickListener;
public ArrayList<Child> movieItems;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView address1,landmark1,building1,others_text;
public ImageView home,work,others,edit;
public CheckBox checkbox;
public MyViewHolder(View view) {
super(view);
address1 = (TextView) view.findViewById(R.id.address);
landmark1 = (TextView) view.findViewById(R.id.landmark);
building1 = (TextView) view.findViewById(R.id.building);
others_text = (TextView) view.findViewById(R.id.others_text);
others = (ImageView) view.findViewById(R.id.others);
home = (ImageView) view.findViewById(R.id.home);
work = (ImageView) view.findViewById(R.id.work);
edit = (ImageView) view.findViewById(R.id.edit);
checkbox = (CheckBox) view.findViewById(R.id.checkbox);
}
}
public AddressAdapter1(Context mcontext,ArrayList<Child> movieItems ) {
this.mcontext = mcontext;
this.movieItems = movieItems;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Child m = movieItems.get(position);
holder.edit.setVisibility(View.GONE);
if (m.getaddress() != null) {
if (!m.getaddress().equals("null")) {
holder.address1.setText(m.getaddress());
}
if (m.getbuilding() != null) {
if (!m.getbuilding().equals("null")) {
holder.building1.setText("Building/Flatno: "+m.getbuilding());
}
if (m.getlandmark() != null) {
if (!m.getlandmark().equals("null")) {
holder.landmark1.setText("Landmark: "+m.getlandmark());
}
if (m.getaddress_type() != null) {
if (!m.getaddress_type().equals("null")) {
String type=m.getaddress_type();
if(type.equals("1")){
holder. home.setVisibility(View.VISIBLE);
holder. others_text.setText("Home");
}
else if(type.equals("2")){
holder. work.setVisibility(View.VISIBLE);
holder. home.setVisibility(View.GONE);
holder. others_text.setText("Work");
}
else{
holder.others.setVisibility(View.VISIBLE);
holder. home.setVisibility(View.GONE);
holder. others_text.setText("Other");
}
}
}
}
}
holder.checkbox.setChecked(m.isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
movieItems.get(getPosition).setSelected(buttonView.isChecked());
final Child m=new Child();
m.setSelected(isChecked);
}
});
holder.checkbox.setTag(position);
}
}
@Override
public int getItemCount() {
return movieItems.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
}
Logcat 错误
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2186)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:4255)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:9945)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:5747)
at abservetech.com.foodapp.Checkinpage$AddressAdapter1$1.onCheckedChanged(Checkinpage.java:857)
at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
at abservetech.com.foodapp.Checkinpage$AddressAdapter1.onBindViewHolder(Checkinpage.java:841)
at abservetech.com.foodapp.Checkinpage$AddressAdapter1.onBindViewHolder(Checkinpage.java:709)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
`
最佳答案
在模型类中取一个 bool 值:
private boolean isCheck;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
在 Adapter 的 onBindViewHolder 中:
private CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
boolean flag= movieItems.get(getPosition).isCheck();
for (int i = 0; i < movieItems.size(); i++) {
if (getPosition == i) {
movieItems.get(getPosition).setCheck(true);
} else {
movieItems.get(getPosition).setCheck(false);
}
}
notifyDataSetChanged();
// m.setSelected(isChecked);
});;
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(movieItems.get(position).isCheck());
holder.checkbox.setOnCheckedChangeListener(checkedListener);
}
关于android - 如何在android中选择和取消选择recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38521525/