2016-07-22 147 views
0

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

如果你打電話嗎? –

回答

2

取一個布爾模型類:

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

是的,我想一次只檢查一個 –

1

試試這個:

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

我已發佈我的適配器代碼 –