Настройка адаптера RecyclerView в ViewPager2 - PullRequest
0 голосов
/ 22 января 2020

Я создал RecyclerView с горизонтальной прокруткой и PageSnapHelper. Теперь я думаю заменить RecyclerView на ViewPager2. Могу ли я просто установить адаптер RecyclerView, который я создал ранее, для нового ViewPager2.?

Здесь указан класс адаптера

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
}

}

1 Ответ

1 голос
/ 26 февраля 2020

Я только что установил в качестве утилиты просмотра viewpager2. Это работает.

...