How to show images of a single item in a ViewPager, does this require an adapter?

Viewed 46

I have fetched a list of items from the Firestore and shown it in a recyclerView. Now when I click an item I want details (a few images and texts) of this particular item shown in a ViewPager.

Following is the ItemListAdapter which is used to show items in the recyclerView.

class ItemListAdapter(
    private val context: Context, private var list: ArrayList<Items>,
    private val onClickItem: (pos: Int) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(ItemListLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = list[position]
        if (holder is MyViewHolder) {
            GlideLoader(context).loadProductPicture(model.image_url1, holder.binding.ivCardViewImage)
            holder.binding.tvCardViewText.text = model.text1
            holder.itemView.setOnClickListener {
                val intent = Intent(context, ItemsViewPagerActivity::class.java)
                intent.putExtra("itemDetail", model)
                context.startActivity(intent)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MyViewHolder(val binding: ItemListLayoutBinding) : RecyclerView.ViewHolder(binding.root)

}

Following is the Activity where the ViewPager2 is. On itemView click of the above adapter, that sends the model as an intent and ItemsViewPagerActivity receives it. Where I am stuck is, when I call the adapter from the ItemsViewPagerActivity I do not have a list to send to the adapter. In the ViewPager I only want to show some images and some text details of a particular item. There could be 1 to 10 images and 1 to 10 text details. As an example, let's say there are 5 images and texts. How can I get it done?

class ItemsViewPagerActivity : BaseActivity() {

    private lateinit var binding: ActivityItemsViewPagerBinding
    private lateinit var mItemDetail:Items

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityItemsViewPagerBinding.inflate(layoutInflater)
        setContentView(binding.root)

        if (intent.hasExtra("itemDetail")){
            mItemDetail=intent.getParcelableExtra<Items>("itemDetail")!!
            openViewPager(mItemDetail)
        }
    }

    private fun openViewPager(ItemDetails: Items) {

        val adapter = ItemsViewPagerAdapter(this,ItemDetail)
        binding.vpItems.adapter=adapter
    }
}

Following is the ItemViewPagerAdapter.

    class ItemViewPagerAdapter(
    private val context: Context, private var list: ArrayList<Items>,

    ) : RecyclerView.Adapter<ItemViewPagerAdapter.MyViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(ItemViewPagerLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val model=list[position]
        GlideLoader(context).loadProductPicture(model.image_url1, holder.binding.ivItems)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MyViewHolder(val binding: ItemViewPagerLayoutBinding):RecyclerView.ViewHolder(binding.root)
}

Following is the xml for the item_view_pager_layout.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:layout_marginBottom="10dp">

        <ImageView
            android:id="@+id/iv_items"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="Items"
            android:src="@drawable/sample_image"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <com.abc.xyz.utils.MSPTextViewBold
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="@color/colorPrimaryDark"
            tools:text="This is a sample text" />

    </LinearLayout>

</LinearLayout>

Following is the model class.

data class Item(
val item_name: String="",
var id: String="",
val image_count: String="",
val image_url1: String="",
val image_url10: String="",
val image_url2: String="",
val image_url3: String="",
val image_url4: String="",
val image_url5: String="",
val image_url6: String="",
val image_url7: String="",
val image_url8: String="",
val image_url9: String="",
val text1: String="",
val text10: String="",
val text2: String="",
val text3: String="",
val text4: String="",
val text5: String="",
val text6: String="",
val text7: String="",
val text8: String="",
val text9: String="",
)
0 Answers
Related