0

我创建了一个带有水平滚动和 PageSnapHelper 的 RecyclerView。现在我想用 ViewPager2 替换 RecyclerView。?我可以简单地设置我之前为新的 ViewPager2 创建的 RecyclerView 适配器吗?

适配器类在这里

class QuoteAdapter(
val context: Context,
val list: ArrayList<ResultsItem>
) : RecyclerView.Adapter<QuoteAdapter.QuoteViewHolder>() {

var i = 0;

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
QuoteViewHolder {

    val view = LayoutInflater.from(context)
        .inflate(R.layout.item_quote, parent, false)
    return QuoteViewHolder(view)
}

override fun getItemCount() = list.size

override fun onBindViewHolder(holder: QuoteViewHolder, position: Int) {



    holder.quote.text = list[position].quoteText
    holder.quote_by.text = "- ${list[position].quoteAuthor}"


    ColorStore()

    if (position == 0) {
        holder.quote_bg.setBackgroundColor(ContextCompat.getColor(context, R.color.md_blue_400))
    } else {
        holder.quote_bg.setBackgroundColor(ContextCompat.getColor(context, colorList.random()))
    }
    holder.quote_bg.setOnClickListener {
        holder.quote_bg.setBackgroundColor(ContextCompat.getColor(context, colorList.random()))
    }



}

class QuoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val quote = itemView.quote_text
    val quote_by = itemView.by_text
    val quote_bg = itemView.quote_bg
}

}

4

1 回答 1

3

我只是将recyclerview 适配器设置为viewpager2。有用。

于 2020-02-26T11:00:23.223 回答