Unaisul Hadi
Unaisul Hadi

Reputation: 678

Setting RecyclerView adapter into ViewPager2

I've created a RecyclerView with Horizontal Scrolling and PageSnapHelper. Now I think to replace RecyclerView with ViewPager2.? Can I simply set RecyclerView Adapter I've created earlier for new ViewPager2.?

Adapter Class goes here

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
}

}

Upvotes: 0

Views: 4983

Answers (1)

Unaisul Hadi
Unaisul Hadi

Reputation: 678

I just set recyclerview adapter to viewpager2. It works.

Upvotes: 3

Related Questions