Android - 包含多个对象的Recyclerview / arraylist?

时间:2016-09-12 17:53:30

标签: android arraylist android-recyclerview android-viewholder

我想知道如何在我的arraylist提供的recyclelerview中实现多个对象。我并不特别想改变很多代码。这是我目前的情况: Viewholder:

public class MyViewHolder extends RecyclerView.ViewHolder{

TextView exercise;
TextView percent;
TextView reps;
TextView weight;
CheckBox check1;
CardView mCardView;
ItemClickListener itemClickListener;

public MyViewHolder(View itemView) {
    super(itemView);

    exercise= (TextView) itemView.findViewById(R.id.txtExercise);
    percent= (TextView) itemView.findViewById(R.id.txtPercentage);
    reps= (TextView) itemView.findViewById(R.id.txtReps);
    weight= (TextView) itemView.findViewById(R.id.txtWeight);
    check1 = (CheckBox) itemView.findViewById(R.id.check1);
    mCardView = (CardView) itemView.findViewById(R.id.card_view);

}
}

由arraylist提供的Rv:

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    SharedPreferences pref = this.getActivity().getSharedPreferences("MyPref", 0);
    prefs = getContext().getSharedPreferences("checkState", Context.MODE_PRIVATE);

//RECYCLERVIEW
    final RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerFri);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getFriWorkout(), 3));

private ArrayList<Workout> getFriWorkout() {
    ArrayList<Workout> workout = new ArrayList<>();
    switch (workout3) {
        case "w29": {
            Workout workouts = new Workout(getActivity().getString(R.string.benchpress), "50", "5", bench50, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "60", "5", bench60, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "70", "4", bench70, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "75", "3", bench75, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "75", "3", bench75, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "80", "2", bench80, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "80", "2", bench80, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "75", "3", bench75, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "75", "3", bench75, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "70", "4", bench70, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "60", "6", bench60, false);
            workout.add(workouts);
            workouts = new Workout(getActivity().getString(R.string.benchpress), "50", "8", bench50, false);
            workout.add(workouts);

            workouts = new Workout(getActivity().getString(R.string.db_fly), "5 Sets", "10 Reps", "", false);
            workout.add(workouts);

            workouts = new Workout(getActivity().getString(R.string.squat), "50", "5", squat50, false);
            workout.add(workouts);

RecyclerView适配器:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

Context mContext;
ArrayList<Workout> workout;
SharedPreferences prefs;
int firstSecondOrThird;
int colorResId = R.color.defaultcard;

public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
    mContext = context;
    this.workout = workout;
    this.firstSecondOrThird = thePosition;
}
// 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;
}

private SparseBooleanArray mCheckedItems = new SparseBooleanArray();

//BIND DATA TO VIEWS
@Override
public void onBindViewHolder(final MyViewHolder holder, final 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);

    holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            prefs.edit().putBoolean(firstSecondOrThird + "checkState" + position, isChecked).apply();
        }
    });
    holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
}


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

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

基本上我想要做的是添加第二个布局以在仅有5个复选框的某些练习下进行充气..我已经阅读了一些其他答案,但发现它们非常令人困惑,因为我是android的新手。我想知道在不改变大量代码的情况下这是否是一种简单的方法,因为我有大量的数据要做。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

你有点走上正轨。在MyRecyclerAdapter中,您需要覆盖onCreateViewHolder()方法以扩充自定义布局并将其提供给ViewHolder。这是我在某个时间工作的项目的链接,它显示了如何执行此操作(https://github.com/ucsunil/Lockers/blob/master/app/src/main/java/com/ebay/lockers/adapters/HomePageItemsAdapter.java)。我也使用ArrayList向ViewHolder提供数据。

您需要使用onBindViewHolder()方法将各个行绑定到ViewHolder。