在切换布局时,RecyclerView复选框会丢失值

时间:2016-08-05 21:10:02

标签: android android-fragments android-recyclerview android-tablayout android-checkbox

我的Recyclerview出现问题。似乎每当手机变为横向模式,或者在应用程序中切换布局时,之前选择的复选框都不会恢复。

EDIT ---------------------------------------------- -

这是我的回收器适配器:

public MyRecyclerAdapter(Context context, ArrayList<Workout> workout) {
    mContext = context;
    this.workout = workout;
}
// INITIALIZE HOLDER
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null);
    MyViewHolder holder = new MyViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.exercise.setText(workout.get(position).getExercise());
    holder.percent.setText(workout.get(position).getPercent());
    holder.reps.setText(workout.get(position).getReps());
    holder.weight.setText(workout.get(position).getWeight());
    holder.check1.setOnCheckedChangeListener(null);
    final Workout isCheck = workout.get(position);
    holder.check1.setChecked(isCheck.isCheck1());


    holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            isCheck.setCheck1(isChecked);
        }
    });

}

这是在每个片段类中设置适配器的地方(这在onCreateView中):

//RECYCLERVIEW
RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout()));
return view;
}

这里是设置片段的地方:

public class WorkoutDaysActivity extends BaseActivity{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_days);
    mToolBar = activateToolbar();
    setUpNavigationDrawer();
    getSupportActionBar().setTitle("Workout");

    ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
    this.addPages(vp);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(vp);
    tabLayout.setOnTabSelectedListener(listener(vp));

}

//ADD ALL PAGES
private void addPages(ViewPager pager) {
    MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
    Intent intent = getIntent();
    String workout = intent.getStringExtra("workout");
    Bundle args = new Bundle();
    args.putString("workout", workout);

    MondayFragment mondayFragment = new MondayFragment();
    mondayFragment.setArguments(args);
    adapter.addPage(mondayFragment);
    WedFragment wedFragment = new WedFragment();
    wedFragment.setArguments(args);
    adapter.addPage(wedFragment);
    FridayFragment fridayFragment = new FridayFragment();
    fridayFragment.setArguments(args);
    adapter.addPage(fridayFragment);
    pager.setAdapter(adapter);
}

2 个答案:

答案 0 :(得分:1)

没错,每次关闭时重新创建片段并重新打开,当方向发生变化时也会发生同样的情况。要回答你的问题,是的!有一些方法可以解决这个问题,我会使用SharedPraferences保存复选框的状态并将其恢复到该状态。实施例

    int firstSecondOrThird; //first page, second page or thrid page.

    public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
        mContext = context;
        this.workout = workout;
        this.firstSecondOrThird = thePosition;
    }


    final SharedPreferences prefs;

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.exercise.setText(workout.get(position).getExercise());
        //...
        prefs = mContext.getSharedPreferences("ehehehe", Context.MODE_PRIVATE);

        holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false)); //restore the state of the CheckBox
        isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false)); //not sure what's this just copying from you ^^


        holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck.setCheck1(isChecked);
                prefs.edit().putBoolean(firstSecondOrThird+"checkState"+position, isChecked).apply(); //save the CheckBox's state
            }
        });

    }

然后像这样发起:

    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout(), 1)); //in your first page

    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout(), 2)); //in your second page, and 3 or the third

答案 1 :(得分:1)

您是否尝试保留片段实例

在这里查看Android Dev Reference: https://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

控制是否在活动重新创建(例如从配置更改)中保留片段实例。这只能用于不在后栈中的片段。如果设置,则在重新创建活动时片段生命周期会略有不同