私は、私のarraylistによってフィードされた私のrecyclerviewで複数のオブジェクトを実装する方法を知っています。特に多くのコードを変更したいとは思いません。ここで私は、現在持っているものです: Viewholder:ArrayListのによって供給Android - 複数のオブジェクトを持つRecyclerview/arraylist?
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);
}
}
の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);
}
は基本的に私が何をしたいのか第2のレイアウトを追加することです5つのチェックボックスしかない特定のエクササイズの下で膨らませるために。私はいくつかの答えを読んだが、私がアンドロイドの新しい人であるので、それらをかなり混乱させることが分かった。多くのコードを変更しなくても簡単な方法です助けてくれてありがとう!
申し訳ありませんアダプタクラスを追加するのを忘れてしまいました。私はすでにそれを行っており、うまくいきます。私が抱えている問題は、リサイクラーとビューホルダーに2番目と3番目のカスタムレイアウトを追加する方法がわかりません。 – LBJ33