在模型中保存recylcerview的复选框状态

时间:2018-08-01 19:13:09

标签: android android-recyclerview android-viewholder

嘿,我想将ViewHolder的复选框状态保存在模型中,但是当我切换活动时,状态不会保存。

我的ViewHolder:

  mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if (isChecked) {
                        int getPosition = (int) compoundButton.getTag();
                        Log.d(TAG, "onCheckedChanged: position " + compoundButton.getTag());
                        mRecipe.get(getPosition).setSelected(compoundButton.isChecked());
                        Log.d(TAG, "onCheckedChanged: " + mRecipe.get(getPosition).isSelected());

我的onBindViewHolder

public void onBindViewHolder(final Holder holder, final int position) {

holder.mCheckBox.setTag(position);

Log.d(TAG, "onBindViewHolder: isSelected?");

if(mRecipe.get(position).isSelected()) {
    holder.mCheckBox.setChecked(true);
    Log.d(TAG, "onBindViewHolder: true");
}else {
    holder.mCheckBox.setChecked(false);
    Log.d(TAG, "onBindViewHolder: false");
}

我在onBindViewHolder上的日志每次都显示错误...

我的模特:

public class Recipe {

private boolean selected;

public boolean isSelected() { return selected; }

public void setSelected(boolean selected) { this.selected = selected; }

}

我的BottomNavigation

public static void enableNavigation(final Context context, BottomNavigationViewEx viewEx){
    viewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()){

                case R.id.ic_home:
                    Intent intent1 = new Intent(context, HomeActivity.class); //ACTIVITY_NUM = 0
                    context.startActivity(intent1);
                    CustomIntent.customType(context, "fadein-to-fadeout");
                    break;

                case R.id.ic_search:
                    Intent intent2 = new Intent(context, SearchActivity.class); // ACTIVITY_ NUM = 1
                    context.startActivity(intent2);
                    CustomIntent.customType(context, "fadein-to-fadeout");
                    break;

                case R.id.ic_cookbook:
                    Intent intent3 = new Intent(context, CookbookActivity.class); //ACTIVITY_NUM = 2
                    context.startActivity(intent3);
                    break;

                case R.id.ic_shoppinglist:
                    Intent intent4 = new Intent(context, ShoppinglistActivity.class); // ACTIVITY_NUM = 3
                    context.startActivity(intent4);

                    break;

0 个答案:

没有答案