我正在尝试制作锻炼日志应用程序。我设计了添加新的锻炼方法,如下图所示:
在AddWorkoutActivity中,有一个带有ExercisesListAdapter的练习的recyclerview。 在此recyclerview中,是另一个带有SetsListAdapter的集合(编号,重量,次数)的recyclerview。
我想做的是当用户单击“保存”按钮时收集数据。 我遇到的问题是,我不知道如何从ExercisesListAdapter类调用SetsListAdapter。
NewWorkoutActivity:
public class NewWorkoutActivity extends AppCompatActivity {
List<ExerciseItem> exercises;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new);
final ExercisesListAdapter exercisesListAdapter = new ExercisesListAdapter(this);
exercisesRecyclerView.setAdapter(exercisesListAdapter);
exercisesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
addExerciseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<ExerciseSetItem> esi = new ArrayList<>();
exercisesListAdapter.addExercise(new ExerciseItem("New Exercise", esi));
}
});
}
ExercisesListAdapter:
public class ExercisesListAdapter extends RecyclerView.Adapter<ExercisesListAdapter.exerciseViewHolder> {
class exerciseViewHolder extends RecyclerView.ViewHolder {
private final TextView exerciseTextView;
private final RecyclerView setsRecyclerView;
private final Button addSetButton;
private exerciseViewHolder(View view) {
super(view);
exerciseTextView = view.findViewById(R.id.exercise_name_text_view);
setsRecyclerView = view.findViewById(R.id.sets_recycler_view);
addSetButton = view.findViewById(R.id.add_set_button);
}
}
private final LayoutInflater mInflater;
private List<ExerciseItem> mExercises;
ExercisesListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public exerciseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = mInflater.inflate(R.layout.recyclerview_exercise_item, viewGroup, false);
return new exerciseViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final exerciseViewHolder exerciseViewHolder, int i) {
// Attach sets to recycler view
final SetsListAdapter exerciseSetsAdapter =
new SetsListAdapter(exerciseViewHolder.addSetButton.getContext());
exerciseViewHolder.setsRecyclerView.setAdapter(exerciseSetsAdapter);
exerciseViewHolder.setsRecyclerView.setLayoutManager(
new LinearLayoutManager(exerciseViewHolder.addSetButton.getContext()));
if(mExercises != null) {
ExerciseItem exercise = mExercises.get(i);
exerciseViewHolder.exerciseTextView.setText(exercise.name);
}
exerciseViewHolder.addSetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExercises.get(exerciseViewHolder.getAdapterPosition()).sets.
add(new ExerciseSetItem(0,0));
exerciseSetsAdapter.setSets(mExercises.
get(exerciseViewHolder.getAdapterPosition()).sets);
}
});
}
@Override
public int getItemCount() {
if(mExercises != null)
return mExercises.size();
else return 0;
}
public void setExercises(List<ExerciseItem> exercises) {
mExercises = exercises;
this.notifyDataSetChanged();
}
SetListAdapter:
public class SetsListAdapter extends RecyclerView.Adapter<SetsListAdapter.exerciseSetViewHolder> {
class exerciseSetViewHolder extends RecyclerView.ViewHolder {
private final TextView setTextView;
private final EditText weightEditText;
private final EditText repsEditText;
private exerciseSetViewHolder(View itemView) {
super(itemView);
setTextView = itemView.findViewById(R.id.new_set_text_view);
weightEditText = itemView.findViewById(R.id.new_set_weight_edit_text);
repsEditText = itemView.findViewById(R.id.new_set_reps_edit_text);
}
}
private final LayoutInflater mInflater;
private List<ExerciseSetItem> mSets;
SetsListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public exerciseSetViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = mInflater.inflate(R.layout.recyclerview_set_item, viewGroup, false);
return new exerciseSetViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull exerciseSetViewHolder workoutViewHolder, int i) {
if(mSets!= null) {
ExerciseSetItem item = mSets.get(i);
workoutViewHolder.setTextView.setText(String.valueOf(i));
workoutViewHolder.weightEditText.setText(String.valueOf(item.weight));
workoutViewHolder.repsEditText.setText(String.valueOf(item.reps));
}
}
@Override
public int getItemCount() {
if(mSets != null)
return mSets.size();
else {
return 0;
}
}
public void setSets(List<ExerciseSetItem> sets) {
mSets = sets;
notifyDataSetChanged();
}
public List<ExerciseSetItem> getSets() {
return mSets;
}
我本来想收集列表中的每个适配器,但对我来说似乎并不好。