如何选择和取消选择Android

问题描述:

中的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) 

`

+0

只写一个toggleCheckBox()方法,如果被选中,简单地检查,它取消选中它,反之亦然 – Eenvincible

+0

@Abserve技术检查你的POJO类变量上onBindViewHolder() – Nisarg

+0

如果你打电话吗? –

取一个布尔模型类:

private boolean isCheck; 

public boolean isCheck() { 
    return isCheck; 
} 

public void setCheck(boolean check) { 
    isCheck = check; 
} 

In 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); 
} 
+0

prevoius值不unchecked @Nisarg –

+0

@Abserve Tech你只想检查一次?对? – Nisarg

+0

是的,我想一次只检查一个 –

试试这个:

private int lastSelectedPosition = -1; // adapter variable 

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

      if (getPosition != lastSelectedPosition) { 
       m.setSelected(isChecked); 
       if (lastSelectedPosition != -1) { 
        movieItems.get(lastSelectedPosition).setSelected(false) // unselect last selection 
        notifyItemChanged(lastSelectedPosition); // notify adapter 
       } 
      } 

      lastSelectedPosition = isChecked ? getPosition : -1; // remember last selected position 
     } 
    }); 
    holder.checkbox.setTag(position); 

UPDATE

为了避免RecyclerView例外尝试包括notifyItemChanged在处理程序中调用:

Handler handler = new Handler(); 
final Runnable r = new Runnable() { 
    public void run() { 
     notifyItemChanged(lastSelectedPosition); // notify adapter 
    } 
}; 
handler.post(r); 
+0

如何在recycleview中获取getItemAtPosition? –

+0

PLZ明确解释 –

+0

我已发布我的适配器代码 –