如何为列表中的列表创建RecyclerView适配器

时间:2019-02-17 05:10:33

标签: android kotlin android-recyclerview

我正在构建一个食谱应用程序(静态数据)。

我创建了一个“食谱”类,并在一个单例对象中创建了一个食谱实例列表。

Recipes类包含以下参数:

title: String
ingredients: List<String>
instructions : String
time: Long serves:
Int cost: String
source : String

现在,我想为配料创建一个适配器,以便为配料列表创建一个RecyclerView

我的问题是将配料列表(而不是食谱列表)传递给适配器。

我正在尝试不同的方法,但不确定该怎么做。已经被这个问题困扰了几天了。

这是食谱类:

class Recipes(val title: String, val ingredients: List<String>, val instructions: String, val time: Long, val serves: Int, val level: String, val source: String) {

    private val prepTimeHours: Long = time / 60
    private val preTimeMinutes: Long = time.rem(60)
    val prepTime: String = "${prepTimeHours.toString()}:${preTimeMinutes.toString()}"

    val peopleServed: String = "$serves adults"
}

这是单身人士:

object AllRecipes {

    val recipeBook = listOf(
        Recipes ("Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("2Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("3Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("4Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("5Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("6Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("7Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"),
        Recipes ("8Red wine-braised baby octopus with black olives", listOf("vegetables", "fruits", "candy"), "cook like this make like that", 90, 6, "easy", "https://www.foodandwine.com/recipes/red-wine-braised-baby-octopus-with-black-olives"))
}

这是我当前的适配器:

class IngredientsAdapter(val context: Context, val ingredient: List<Recipes>) : RecyclerView.Adapter<IngredientsAdapter.Holder>() {

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val singleIngredient = itemView.findViewById<TextView>(R.id.single_ingredient)

        fun bindText(textVar: Recipes, context: Context) {
            singleIngredient.text = textVar.ingredients[adapterPosition]
        }
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bindText(ingredient[position], context)
    }

    override fun getItemCount(): Int {
        return ingredient.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.ingredients_layout, parent, false)
        return Holder(view)
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以查看answer here。关键思想是为您的每种成分的RecyclerView中的每一项添加额外的视图。实际上,您不必为您的案例实现嵌套的RecyclerView。由于成分有限,只需在每个项目上添加更多布局即可。

answer显示了三种不同的方式。如果配料数量不多,则主要TextView的每个项目都可以有多个RecyclerView,并且View.GONE的默认可见性也可以。根据您拥有的成分的位置,您可以考虑启用某些成分的可见性,而另一些成分仍然不可见。

希望有帮助!