0

I'm hoping someone can show me the ropes of hopefully ViewPager2, but I'm basically in need of making a viewpager where I can swipe between different images.

I will have a category, let's call it Category A. Category A can have multiple items in it, each with a picture and small description.

I would love to do a Header out of Category A, then below that I want to be able to swipe through these items, while the description below the viewpager of items, updates to match the corresponding item shown.

This is also done within a recyclerview, since I can have multiple categories.

Right now I'm trying something like

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/category"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Hosomaki" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/category" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

and then in my RecyclerAdapter, I was thinking of being able to do something like this

private class CategoryItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bind(item: CategoryItemViewModel) = with(itemView) {
    itemView.category.text = item.category.title

    // Something here is what I'm missing
    val adapter = ViewPagerAdapter(itemView.context, item.items)
    itemView.viewPager.adapter = adapter
    }
}

Hopefully my question makes sense.. :D

Mikkel Larsen
  • 876
  • 2
  • 14
  • 26
  • You should be using `ViewPager2` for vertical scroll not `ViewPager` .. Also i did not get whats the problem here ? can you pls edit your question with proper problem – ADM Mar 23 '20 at 15:08
  • It's more of a question. Like what I have now doesn't work. I'm not sure how I would solve this to match the solution I want. – Mikkel Larsen Mar 23 '20 at 15:12
  • From what i understand you are doing it right .. where `ViewPagerAdapter` should be extending a RecyclerView.Adapter and use `ViewPager2`.. – ADM Mar 23 '20 at 15:16
  • Alright I'm not doing that now. I'll try thanks – Mikkel Larsen Mar 23 '20 at 15:18

0 Answers0