Android 切换布局时,RecyclerView复选框将失去价值

Android 切换布局时,RecyclerView复选框将失去价值,android,android-fragments,android-recyclerview,android-tablayout,android-checkbox,Android,Android Fragments,Android Recyclerview,Android Tablayout,Android Checkbox,我的回收视图有问题。似乎每当手机进入横向模式,或在应用程序中切换布局时,先前选中的复选框都不会恢复 编辑------------------------------------------------ 这是我的回收器适配器: public MyRecyclerAdapter(Context context, ArrayList<Workout> workout) { mContext = context; this.workout = workout; } // IN

我的回收视图有问题。似乎每当手机进入横向模式,或在应用程序中切换布局时,先前选中的复选框都不会恢复

编辑------------------------------------------------

这是我的回收器适配器:

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);
        }
    });

}
这里是碎片设置的地方:

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);
}

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

    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
            }
        });

    }

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

    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
            }
        });

    }
您是否尝试保留片段实例

请在此处查看Android开发人员参考:

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

您是否尝试保留片段实例

请在此处查看Android开发人员参考:


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

我很高兴它成功了,您还需要其他帮助吗?我厌倦了Hanks,这很管用,不幸的是,因为我有一个可扩展列表视图,每个选项都会将应用程序发送到同一个带有3个选项卡的tablayout,每个选项卡都有recycler视图,它会为每个片段和每个可扩展列表视图选项保存选中的选项卡LOL。。有什么简单的方法可以解决这个问题吗?不只是一个视图持有者,每个回收器视图都使用相同的视图,只是数据不同,我从我的可扩展列表视图发送一个包,然后在创建片段的位置附加包,然后在每个片段中,我检查哪个字符串是从可扩展列表视图发送的,并使用switch语句设置数据好的,您可以发布适配器的构造函数以及如何为每个列表初始化适配器吗?让我们来。我很高兴它能工作,您还需要其他帮助吗?我厌倦了Hanks,这很管用,不幸的是,因为我有一个可扩展列表视图,每个选项都会将应用程序发送到同一个带有3个选项卡的tablayout,每个选项卡都有recycler视图,它会为每个片段和每个可扩展列表视图选项保存选中的选项卡LOL。。有什么简单的方法可以解决这个问题吗?不只是一个视图持有者,每个回收器视图都使用相同的视图,只是数据不同,我从我的可扩展列表视图发送一个包,然后在创建片段的位置附加包,然后在每个片段中,我检查哪个字符串是从可扩展列表视图发送的,并使用switch语句设置数据好的,您可以发布适配器的构造函数以及如何为每个列表初始化适配器吗?让我们来看看。
    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