问题
I'm using the Groupie library for showing expandable items and my issue is that instead of the items retrieved through an api call being shown under each of their parent expandable header, they're all shown together under the last item on the list.
I would like to have this:
- Header 1
- Child header 1
- Child header 1
- Child header 1
- Header 2
- Child header 2
- Child header 2
I have this instead:
- Header 1
- header 2
- Child header 1
- Child header 1
- Child header 1
- Child header 2
- Child header 2
This is my code
private fun updateUICurrentExercise(getCurrentWorkoutExercise: GetCurrentWorkoutExercise?) {
adapter = GroupAdapter()
val workouts = getCurrentWorkoutExercise?.workouts
for (w in workouts!!) {
exp = ExpandableGroup(WorkoutItem("Workout ${(w.workoutId)}"), false)
getExercisesByWorkoutAPI(w.workoutId.toString())
adapter.add(exp)
}
rvWorkout.adapter = adapter
}
private fun getExercisesByWorkoutAPI(workoutId: String) {
GetExercisesByWorkoutAPI.postData(jo, object : GetExercisesByWorkoutAPI.ThisCallback {
override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {
for (e in getExercisesByWorkout?.exercises!!) {
exp.add(Section(ExerciseItem(workoutId)))
}
}
})
}
回答1:
It looks like you missed to use Section.setHeader. So you will need to create one header instance WorkoutItem("Workout ${(w.workoutId)}"
for each w
collection item and then pass this instance into ExpandableGroup
constructor and Section.setHeader
回答2:
Based on @valerii's reply that's the fix for the issue: having both the expandable group and its items initiated within the same api call.
private fun updateUICurrentExercise(getCurrentWorkoutExercise: GetCurrentWorkoutExercise?) {
adapter = GroupAdapter()
val workouts = getCurrentWorkoutExercise?.workouts
for (w in workouts!!) {
getExercisesByWorkoutAPI(w.workoutId.toString())
}
rvWorkout.adapter = adapter
}
private fun getExercisesByWorkoutAPI(workoutId: String) {
GetExercisesByWorkoutAPI.postData(jo, object : GetExercisesByWorkoutAPI.ThisCallback {
override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {
val expandableGroup = ExpandableGroup(WorkoutItem("Workout $workoutId"), false)
for (e in getExercisesByWorkout?.exercises!!) {
expandableGroup.add(Section(ExerciseItem(e.exerciseName)))
}
adapter.add(expandableGroup)
}
})
}
来源:https://stackoverflow.com/questions/54506416/groupie-recyclerview-all-items-added-to-the-same-expandable-item