2016-04-08 225 views
0

我有一個選項列表CheckBoxesRecyclerView。當我點擊複選框時,狀態會發生變化,但在滾動上下後,複選框的狀態將丟失。在RecyclerView中保存複選框狀態

如何保存複選框的狀態?我試圖改變適配器狀態:

private void setAdapter() { 
    if (mAdapter == null) { 
     mAdapter = new FacetChildAdapter(mValues, getActivity(), query.getSpecValueAsString(mParentValue.getSlug())) { 
      @Override 
      protected void onCheckBoxRowClicked(CheckboxRow box, Value value, int adapterPosition) { 
       if (type == FacetChildType.Brands) { 
        if (box.isChecked()) { 
         query.removeBrand(value); 
        } else { 
         query.addBrand(value); 
        } 
       } 

       else if (type == FacetChildType.Categories) { 
        if (box.isChecked()) { 
         query.removeCategory(value); 
        } else { 
         query.addCategory(value); 
        } 
       } 

       else if (type == FacetChildType.Deals) { 
        if (box.isChecked()) { 
         query.removeDealType(value); 
        } else { 
         query.addDealType(value); 
        } 
       } 

       else if (type == FacetChildType.Specifications) { 
        if (box.isChecked()) { 
         query.removeSpecification(mParentValue.getSlug(), value); 
        } else { 
         query.addSpecification(mParentValue.getSlug(), value); 
        } 
       } 

       box.setChecked(!box.isChecked()); 
       mHeading.setBackText(getResources().getString(R.string.apply)); 
      } 

     }; 
     mRecyclerView.setAdapter(mAdapter); 

    } else { 
     mAdapter.setSource(query.getSpecValueAsString(mParentValue.getSlug())); 
     mAdapter.refresh(mValues); 
    } 

} 

FacetChildAdapter:

public class FacetChildAdapter extends GenericRecycleAdapter<Value, Holders.TextImageHolder> { 
private String source; 


public FacetChildAdapter(List<Value> list, Context context, String source) { 
    super(list, context); 
    this.source = source; 
} 

public void setSource(String source) { 
    this.source = source; 
} 

@Override 
protected void onItem(Value s) { 

} 

public Holders.TextImageHolder getCustomHolder(View v) { 
    return new Holders.TextImageHolder(v) { 
     @Override 
     public void onCheckBoxRowClicked(CheckboxRow v) { 
      FacetChildAdapter.this.onCheckBoxRowClicked(v, mList.get(getAdapterPosition()), getAdapterPosition()); 

     } 
    }; 
} 

protected void onCheckBoxRowClicked(CheckboxRow box, Value value, int adapterPosition) { 

} 

@Override 
public int getLayout() { 
    return R.layout.facet_child_row; 
} 

@Override 
public void onSet(final Value item,final Holders.TextImageHolder holder) { 
    holder.checkboxRow.setTitle(Html.fromHtml(item.getFullName())); 
    holder.checkboxRow.setSubText("(" + String.valueOf(item.getCount()) + ")"); 
    holder.checkboxRow.setChecked(ShowProductsWithId.containsValueInValueStringLine(source, item.getSlug())); 
    holder.checkboxRow.setDisabled(!item.isEnabled()); 

} 

}

+0

創建一個像已選中,那麼在循環時間複選框位置的一個HashMap數組** onSet()**方法得到它們 –

+0

可能的重複http://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items –

回答

2

它可以通過兩種方式維護:

1)使用HashMap來存儲複選框視圖的位置。 您可以存儲位置並存儲特定視圖的唯一ID,然後在Adapter使用後可以輕鬆獲取其中的複選框位置。

2)時,您可以使用消氣二傳手類,那麼你就在其中創建了一個整型變量,然後你可以輕鬆地設置在這個變量的位置就像

  • 時,您可以選中該複選框,然後變值是1
  • 時,你可以取消選中該複選框,然後變量的值是0

維護您的複選框選中的位置或在下面的方式選中。

-1
class TeamNamesAdapter extends RecyclerView.Adapter<TeamNamesAdapter.ViewHolder> 
     implements ItemTouchHelperAdapter, View.OnClickListener { 

    private ArrayList<Person> people; 

    public TeamNamesAdapter(TinyDB db) { 
     people = new ArrayList<>(); 
     try { 
      ArrayList<Object> peopleAsObjects = db.getListObject(PEOPLE, Person.class); 
      for (Object obj : peopleAsObjects) { 
       people.add((Person) obj); 
      } 

      if (db.getListObject(PEOPLE_ORDER, Person.class) == null) { 
       db.putListObject(PEOPLE_ORDER, peopleAsObjects); 
      } 
     } 
     catch (NullPointerException e) { 
      return; 
     } 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_names_list_item, null); 

     ViewHolder viewHolder = new ViewHolder(view); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     Person curr = people.get(position); 
     holder.name.setText(curr.name); 
     holder.name.setChecked(curr.available); 
    } 

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

    public void addName(String name) { 
     if(people.contains(name) || name.isEmpty()) 
      return; 

     people.add(new Person(name, true)); 
     notifyDataSetChanged(); 
     update(true); 
    } 

    @Override 
    public void onItemDismiss(int position) { 
     people.remove(position); 
     notifyItemRemoved(position); 
     update(true); 
    } 

    @Override 
    public void onItemMove(int fromPosition, int toPosition) { 
     if (fromPosition < toPosition) { 
      for (int i = fromPosition; i < toPosition; i++) { 
       Collections.swap(people, i, i + 1); 
      } 
     } 
     else { 
      for (int i = fromPosition; i > toPosition; i--) { 
       Collections.swap(people, i, i - 1); 
      } 
     } 
     notifyItemMoved(fromPosition, toPosition); 
     update(true); 
     undoRandomizeButton.setEnabled(false); 
    } 

    @Override 
    public void onClick(View v) { 
     if (!(v instanceof CheckedTextView)) 
      return; 

     CheckedTextView ctv = (CheckedTextView) v; 
     ctv.toggle(); 

     String name = ctv.getText().toString(); 
     for (Person p : people) { 
      if (p.name.equals(name)) { 
       p.available = ctv.isChecked(); 
       update(true); 
       return; 
      } 
     } 
    } 

    class ViewHolder extends RecyclerView.ViewHolder { 
     CheckedTextView name; 
     public ViewHolder(View itemView) { 
      super(itemView); 

      name = (CheckedTextView)itemView.findViewById(R.id.name_in_list); 
      name.setOnClickListener(TeamNamesAdapter.this); 
     } 
    } 
} 

這是一個用於包含CheckedTextView的列表的recyclerview適配器。我保存一個名爲Person的類,其中包含一個字符串和一個布爾值。你的情況是類似的,因爲你只能在ArrayList中保存布爾值(而不是Person),並且在onBindViewHolder上,將checkbox.isChecked設置爲該位置中的布爾值。您可以在RecyclerView適配器上設置View.OnClickerListener,並將onClickListener的ViewHolder設置爲YourAdapterName.this。且實施方法,像我一樣,只是不顧Person東西,只是更新boolean 你可以看到,在適配器onBindViewHolder方法,