中的recycleview我已經創建了recycleview.That recycleview包含複選框。如果我點擊recycleview中的第一個項目,複選框被選中後,我點擊第二個項目意味着第一個和第二個項目複選框checked.Its工作不錯,但我想取消第一檢查項目,如果我點擊第二項checkbox.`如何選擇和取消選擇Android
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 error
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)
`
只寫一個toggleCheckBox()方法,如果被選中,簡單地檢查,它取消選中它,反之亦然 – Eenvincible
@Abserve技術檢查你的POJO類變量上onBindViewHolder() – Nisarg
如果你打電話嗎? –