2015-11-02 63 views
1

我有一個recyclerview,其中每個列表項有一個帶有4個單選按鈕的radiogroup。我如何正確存儲每個radiogroup的狀態。 這是我的代碼。在向上/向下滾動狀態是不正確的。由於Radiogroup in recyclerview

public class ElementListAdapter extends RecyclerView.Adapter<ElementListAdapter.ViewHolder> { 

    private List<Element> elements = new ArrayList<>(); 
    private Context context; 

    private int[] state; 

    public ElementListAdapter(Context context, List<Element> elements) { 
     this.context = context; 
     this.elements = elements; 

     this.state = new int[elements.size()]; 
     Arrays.fill(this.state, -1); 
    } 

    @Override 
    public ElementListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout, parent, 
       false); 

     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, final int position) { 
     final Element ele = elements.get(position); 
     final String title = ele.getTitle(); 
     final String description = ele.getDescription(); 

     // Set text 
     holder.tvTitle.setText(title); 
     holder.tvDesciption.setText(description); 

     if (ele.isHeader()) {     
      holder.radioGroup.setVisibility(View.GONE); 
     } else {    
      holder.radioGroup.setVisibility(View.VISIBLE); 
     } 

     setRadio(holder, this.state[position]); 



     holder.rb1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       state[position] = 0; 
       setRadio(holder, state[position]); 
      } 
     }); 
     holder.rb2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       state[position] = 1; 
       setRadio(holder, state[position]); 
      } 
     }); 
     holder.rb3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       state[position] = 2; 
       setRadio(holder, state[position]); 
      } 
     }); 
     holder.rb4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       state[position] = 3; 
       setRadio(holder, state[position]); 
      } 
     }); 


    } 

    private void setRadio(final ViewHolder holder, int selection) { 

     System.out.println("SELECT:" + selection); 
     RadioButton b1 = holder.rb1; 
     RadioButton b2 = holder.rb2; 
     RadioButton b3 = holder.rb3; 
     RadioButton b4 = holder.rb4; 

     b1.setChecked(false); 
     b2.setChecked(false); 
     b3.setChecked(false); 
     b4.setChecked(false); 

     if (selection == 0) b1.setChecked(true); 
     if (selection == 1) b2.setChecked(true); 
     if (selection == 2) b3.setChecked(true); 
     if (selection == 3) b4.setChecked(true); 
    } 

    @Override 
    public int getItemCount() { 
     return elements.size(); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 

     public View view; 
     public TextView tvTitle; 
     public TextView tvDesciption; 

     public RadioGroup radioGroup; 
     public RadioButton rb1, rb2, rb3, rb4; 


     public ViewHolder(View itemView) { 
      super(itemView); 

      view = itemView; 
      tvTitle = (TextView) itemView.findViewById(R.id.title); 
      tvDesciption = (TextView) itemView.findViewById(R.id.description); 

      radioGroup = (RadioGroup) itemView.findViewById(R.id.radioGroup); 
      rb1 = (RadioButton) itemView.findViewById(R.id.rb1); 
      rb2 = (RadioButton) itemView.findViewById(R.id.rb2); 
      rb3 = (RadioButton) itemView.findViewById(R.id.rb3); 
      rb4 = (RadioButton) itemView.findViewById(R.id.rb4); 

     } 
    } 

} 
+0

視圖持有人回收,所以你不能默認狀態。一種解決方案是動態存儲狀態(檢查適配器位置)並將其恢復到onBindViewHolder中。不要觸發任何onCheckChange,雖然 –

回答

8

保存您的作品state最好的辦法是將狀態變量列表item model內,例如:「元素」在你的情況,比內onBindViewHolder設置狀態根據你的模型,你的情況:

改變這一點:setRadio(holder, this.state[position]);

這樣:setRadio(holder, elements.get(position).getState());

的onClick方法

前內:對於第一個 改變這樣的: state[position] = 0; setRadio(holder, this.state[position]);

這樣: elements.get(position).setState(0); setRadio(holder, elements.get(position).getState());

+0

感謝您的迴應。它似乎仍然沒有工作:/ – sujithvm

+0

我做了建議的更改並刪除了setChecked(false),它似乎工作。非常感謝:) – sujithvm

0

這就是我所做的。在我的onBindViewHolder中

if(visualScore[position] == 1){ 
       holder.rg.check(R.id.rb_1); 
      }else{ 
       if(audioScore[position] == 1){ 
        holder.rg.check(R.id.rb_2); 
       }else{ 
        if(kinestScore[position] == 1){ 
         holder.rg.check(R.id.rb_3); 
        }else{ 
         holder.rg.clearCheck(); 
        } 
       } 
      } 
      holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
        switch (checkedId) { 
         case (R.id.rb_1): 
          visualScore[position] = 1; 
          audioScore[position] = 0; 
          kinestScore[position] = 0; 
          break; 
         case (R.id.rb_2): 
          visualScore[position] = 0; 
          audioScore[position] = 1; 
          kinestScore[position] = 0; 
          break; 
         case (R.id.rb_3): 
          visualScore[position] = 0; 
          audioScore[position] = 0; 
          kinestScore[position] = 1; 
          break; 
        } 
     }); 

我將所有得分數組聲明爲適配器類中的私有全局方法。然後,點擊提交按鈕時:

int v = 0, a = 0, k = 0; 
        for (int i = 0; i < Questions.length; i++){ 
         v += visualScore[i]; 
         a += audioScore[i]; 
         k += kinestScore[i]; 
        } 
+0

不是說我的方法更好,但它是一種解決方法。當回收者視圖被滾動時,onCheckedChanged將被觸發,但值仍然保存在數組中 –