我的RecyclerView
应该可以显示来自WorkoutSNR
和WorkoutCurcuit
的对象练习,两者都可以扩展课程Workout
。我的RecyclerView
看起来像这样:
public class ViewAdapter_ExerciseData extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Workout workout;
private Activity activity;
private static final int SNR = 1;
private static final int CURCUIT = 2;
public ViewAdapter_ExerciseData(Workout workout, Activity activity) {
this.workout = workout;
this.activity = activity;
}
@Override
public int getItemViewType(int position) {
if (workout instanceof WorkoutSNR) {
return SNR;
}
if (workout instanceof WorkoutCurcuit) {
return CURCUIT;
}
throw new RuntimeException("error");
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v1 = inflater.inflate(R.layout.rv_item_data_workout_snr_excersise, parent, false);
switch (viewType) {
case SNR:
viewHolder = new ViewAdapter_ExerciseData.Holder_WorkoutDataSNR(v1);
break;
case CURCUIT:
viewHolder = new ViewAdapter_ExerciseData.Holder_WorkoutDataSNR(v1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
}
}
当我从 Firebase 加载WorkoutSNR
时,一切正常。但是,当为WorkoutCurcuit
执行相同操作时,不会调用适配器中的getItemViewType
。这是我用来加载WorkoutCurcuit
并将适配器设置为RecyclerView
private void buildWorkoutCurcuit() {
fb.getRefWorkoutCurcuit(workoutKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final WorkoutCurcuit workoutCurcuit = dataSnapshot.getValue(WorkoutCurcuit.class);
workoutCurcuit.setKey(dataSnapshot.getKey());
final ViewAdapter_ExerciseData adapter = new ViewAdapter_ExerciseData(workoutCurcuit, Activity_WorkoutData.this);
for (int i = 0; i < workoutCurcuit.getExcersiseKeys().size(); i++) {
fb.getRefExerciseEntriesCurcuit(workoutCurcuit.getExcersiseKeys().get(i)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final ExerciseEntryCurcuit entry = dataSnapshot.getValue(ExerciseEntryCurcuit.class);
entry.setKey(dataSnapshot.getKey());
fb.getRefUserPresets(entry.getPresetKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ExcersisePresets preset = dataSnapshot.getValue(ExcersisePresets.class);
preset.setKey(dataSnapshot.getKey());
entry.setPreset(preset);
workoutCurcuit.addExercsise(entry);
rvExcersises.setAdapter(adapter);
prg.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Activity_WorkoutData.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Activity_WorkoutData.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
正如我所说,当我为WorkoutSNR
执行相同的操作时,这与我用来加载WorkoutCuruit
的方法几乎相同,我只使用其他参考,
它的工作原理应该如此。 'getItemViewType'被调用,但当我为WorkoutCurcuit
执行时,它将不会被调用。
答案 0 :(得分:0)
我在你的适配器中没有看到这个方法,如果不在那里就添加它
public int getItemCount() {
return list.size();
}
答案 1 :(得分:0)
好的,我发现了我的错误,我使用了错误的类,在一个地方被称为相同。对不起,我浪费你的时间......