切换片段时将选定的项目保存在RecyclerView中

时间:2018-07-25 08:25:40

标签: android android-fragments android-recyclerview

我有一个由3个片段标签组成的活动,您可以在其中切换。每个片段都有一个RecyclerView,单击时可以选择/突出显示项目。这是通过自定义RecyclerView适配器中holder.setSelected(true)中的onBindViewHolder完成的。

问题是当我在三个片段之间切换时,所选项目在看不见时被删除。我尝试在onPause中保存RecyclerView的状态并在onResume中恢复它,但这没有用。

所选项目如何正确保存和检索?

这是我自定义的RecyclerView适配器(在当前版本中未使用interface OnItemClickListener):

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder> {

private String[] mExampleList;
private OnItemClickListener mListener;

public interface OnItemClickListener {

}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

public static class ExerciseViewHolder extends RecyclerView.ViewHolder {

    private TextView mTextView1;
    private ImageView mImageView;
    CardView mCardView;

    public ExerciseViewHolder(View itemView, final OnItemClickListener listener) {
        super(itemView);

        mTextView1 = itemView.findViewById(R.id.textViewExercise);
        mImageView = itemView.findViewById(R.id.imageView);
    }

    void setSelected(boolean selected){
        mTextView1.setSelected(selected);
    }
}

public ExerciseAdapter(String[] exampleList) {
    mExampleList = exampleList;
}

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

    ExerciseViewHolder evh = new ExerciseViewHolder(v, mListener);

    return evh;
}

@Override
public void onBindViewHolder(@NonNull final ExerciseViewHolder holder, final int position) {

    holder.mTextView1.setText(mExampleList[position]);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.setSelected(true);
            Day1Activity.setTimer();

        }
    });

    holder.mImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.setSelected(false);
        }
    });

}

@Override
public int getItemCount() {
    return mExampleList.length;
}

这是包含RecyclerView的片段之一:

public class Frag1 extends Fragment {

private RecyclerView mRecyclerView;
private ExerciseAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


private View rootView;

DataModel dataModel = new DataModel();

String[] exercises = dataModel.C_lifts();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.frag1_layout, container, false);

   buildRecyclerView();

    return rootView;
}

private void buildRecyclerView() {
    mRecyclerView = rootView.findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getContext());
    mAdapter = new ExerciseAdapter(exercises);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);

}

使用ViewPager处理片段的活动:

public class Day1Activity extends AppCompatActivity {

private static CountDownTimer countDownTimer;
private static TextView textViewTimer;

private static final String TAG = "Activity";

private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.day1_layout);

    mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

    mViewPager = findViewById(R.id.viewPagerContainer);
    setupViewPager(mViewPager);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    Toolbar toolbar = findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    textViewTimer = findViewById(R.id.textViewTimer);
}

private void setupViewPager(ViewPager viewPager) {
    int weekNumber = MainActivity.getWeekNumber();

    String frag1Title = "C-S";
    String frag2Title = "C-B";
    String frag3Title;
    if(weekNumber < 5) {
        frag3Title = "Rack Pulls";
    } else {
        frag3Title = "2ct Paused Deadlift";
    }

    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new Frag1(),frag1Title);
    adapter.addFragment(new Frag2(),frag2Title);
    adapter.addFragment(new Frag3(),frag3Title);
    viewPager.setAdapter(adapter);

}

public static void setTimer() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
    }

    countDownTimer = new CountDownTimer(240*1000, 1000) {

        public void onTick(long millisUntilFinished) {
            textViewTimer.setText("Seconds remaining: " + millisUntilFinished/1000);
        }

        public void onFinish() {
            textViewTimer.setText("Time is up!");
        }

    }.start();
}

非常感谢您的帮助!谢谢

1 个答案:

答案 0 :(得分:0)

将所选项目保存到SparseBooleanArray,其中 key position 项目,而 value true false 取决于选择。

您必须将这个数组保存在某个地方,以便稍后在重新创建Fragment时重用它。当Fragment被分离时,您可以将其保存在onSaveInstanceState()中,当您返回到Fragment时,只需检查保存的实例是否具有您存储的数组并将其传递给RecyclerView适配器即可使用在onBindViewHolder()