【问题标题】:Groupie RecyclerView All Items Added To The Same Expandable ItemGroupie Recycler查看添加到同一可扩展项目的所有项目
【发布时间】:2019-06-27 14:52:09
【问题描述】:

我正在使用 Groupie 库来显示可扩展项目,我的问题是,通过 api 调用检索到的项目并没有显示在它们的每个父可扩展标题下,而是一起显示在列表。

我想要这个:

  • 标题 1
    • 子标题 1
    • 子标题 1
    • 子标题 1
  • 标题 2
    • 子标题 2
    • 子标题 2

我有这个:

  • 标题 1
  • 标题 2
    • 子标题 1
    • 子标题 1
    • 子标题 1
    • 子标题 2
    • 子标题 2

这是我的代码

 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)))
            }
        }

    })
}

【问题讨论】:

    标签: android kotlin android-recyclerview expandablelistview groupie


    【解决方案1】:

    您似乎错过了使用Section.setHeader。因此,您需要为每个w 集合项创建一个标头实例WorkoutItem("Workout ${(w.workoutId)}",然后将此实例传递给ExpandableGroup 构造函数和Section.setHeader

    【讨论】:

    • 嗨@valerii,谢谢你的回复。我听到你在说什么并理解它,只是不确定是否能够应用它,因为这是我尝试图书馆的第一周,所以我仍然在思考它。但是现在将尝试您的建议,如果能够使其发挥作用,将确保我回到这里并将其作为接受的答案。再次感谢。 :)
    • 嗨@valerii,我认为问题实际上可能来自等待api响应。我对部分标题进行了一些操作,但结果似乎与我在 api 的 onSuccess 之前粘贴此行 exp.add(Section(ExerciseItem(workoutId))) 时相同,我让这些项目出现在它们的父项下,但是当我将它们放在 api 调用中时它们都出现在最后一项下方。问题是我现在更加卡住了,因为我可能需要像观察者这样的东西来让我的项目在 api 调用完成或类似的东西时填充我真的不认识它们。
    • 那么这段代码可能对你有用。 override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {val expandableGroup = ExpandableGroup(WorkoutItem("Workout $workoutId"), false)for (e in getExercisesByWorkout?.exercises!!) {expandableGroup.add(Section(ExerciseItem(workoutId)))}adapter.add(expandableGroup)}
    • 我建议你看一下 rxjava+retrofit 的 rest api 调用。 RxJava 基于观察者模式的思想,提供了方便地在线程之间切换的机会。我在以下项目中使用了 rxjava2 和 retrofit2。 github.com/ValeriiVolkov/Weathergithub.com/ValeriiVolkov/Recipes
    • 非常感谢@Valerii。它现在有效,我已将您的答案标记为已接受。在同一个 api 调用上同时启动可扩展组及其项目是完全有意义的。我的计划之一是学习 RXJava,因为当一个 api 比另一个先完成时,我一直遇到这类问题。再次感谢你的帮助。非常感激。 :)
    【解决方案2】:

    根据 @valerii 的回复,解决了这个问题:在同一个 api 调用中启动了可扩展组及其项目。

    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)
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多