'Converting RecyclerView to ViewPager / PagerAdapter

Background - New to Android, but pretty nifty with moving layouts around, understanding Java, Kotlin and XML. However this task seems to be way above my head.

Problem - I'm looking to convert the following Java file (RecyclerView) into a Kotlin file (ViewPager) - since I already have a ViewPager hooked-up with the same scrolling behaviour as desired. I get the impression it's a 10min job for a seasoned developer. If that's the case I wonder if I could call upon some assistance from the community? At least to work out where to start. I can't seem to find a guide on how to convert a RecyclerView into a PagerAdapter or ViewPager.

Essentially the existing ViewPager I'm using has static data (5 items) and this one could have tens of items, so needs to be dynamic with a separate datasource (items of CardItem).


RecyclerViewAdapter - Current (Java)

package com.APPNAME.fragments.cards;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import com.APPNAME.R;
import com.APPNAME.databinding.FragmentCardsRecentlyViewedBinding;
import com.APPNAME.model.cardItem.CardItem;



public class RecentlyViewedAdapter extends RecyclerView.Adapter<RecentlyViewedAdapter.RecentlyViewedViewHolder> {

    public OnCardClicked listener;
    private ArrayList<CardItem> items = new ArrayList<>();

    public void addItems(ArrayList<CardItem> list) {
        items = list;
        notifyDataSetChanged();
    }

    @Override
    public RecentlyViewedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FragmentCardsRecentlyViewedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_cards_recently_viewed, parent, false);
        return new RecentlyViewedViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(RecentlyViewedViewHolder holder, int position) {
        holder.binding.setViewModel(new RecentlyViewedViewModel(items.get(position)));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    interface OnCardClicked {
        void onCardClicked(View view, CardItem cardItem);
    }

    class RecentlyViewedViewHolder extends RecyclerView.ViewHolder {
        FragmentCardsRecentlyViewedBinding binding;

        public RecentlyViewedViewHolder(FragmentCardsRecentlyViewedBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
            binding.cardView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCardClicked(v, items.get(getAdapterPosition()));
                }
            });
        }
    }
}

ViewPagerAdapter - Future (Kotlin)

package com.APPNAME.fragments.cards
import android.support.annotation.DrawableRes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
import kotlinx.android.synthetic.main.fragment_cards_recently_viewed.view.*
import com.APPNAME.R
import com.APPNAME.activities.BaseActivity
import com.APPNAME.activities.cards.NewCardActivity
import com.APPNAME.model.cardItem.CardItem
import com.APPNAME.views.wrapContentViewPager.ObjectAtPositionPagerAdapter


class RecentlyViewedItemAdapter constructor(private val activity: BaseActivity) : ObjectAtPositionPagerAdapter() {

    private var items = ArrayList<CardItem>()

    override fun instantiateItemObject(container: ViewGroup, position: Int) : Any {
    return getImageView(container, R.drawable.placeholder_card_image) { NewCardActivity.start(activity, it) }
    }

    private fun getImageView(container: ViewGroup, @DrawableRes imageResourceId: Int, onClick: (imageResourceId: Int) -> Unit = {}): View {
    val layoutInflater = LayoutInflater.from(container.context)
        val layout = layoutInflater.inflate(R.layout.fragment_cards_recently_viewed, container, false)
        val image = layout.recentlyViewedImage
        image.setImageResource(imageResourceId)
        image.setOnClickListener { onClick(imageResourceId) }
        container.addView(layout)
        return layout
    }

    override fun isViewFromObject(view: View, anObject: Any) = (view == anObject)
    override fun getCount() = 5     //Placeholder

    override fun destroyItemObject(container: ViewGroup, position: Int, view: Any) {
        container.removeView(view as View)
    }
}


Solution 1:[1]

If you have relatively large number of items and need views to be recycled then the RecyclerView is the right option for you. It will be definitely easier than creating a custom view pager adapter with a fixed item count and a view holder pattern. The default/generic PageAdapter is not recycling anything - it's added to make sure you have all your views initiated and ready to swipe through. However, you can use https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html that will destroy/recreate the fragments when no longer used or reused.

Solution 2:[2]

Job done. I actually surprised myself by being able to modify the recycler view to mimmick the PageView's behaviour precisely (including padding, snap scrolling etc). Thanks to the simple subclass SnapHelper. Saved me a tonne of work, so no need to refactor the codebase.

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

Solution 3:[3]

PagerSnapHelper solved my problem.

   binding.recyclerViewHomeAnnouncement.apply {
        layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
        binding.recyclerViewHomeAnnouncement.layoutManager = layoutManager
        setHasFixedSize(true)
        itemAnimator = DefaultItemAnimator()
        adapter = homeAnnouncementAdapter
    }
    
    val snapHelper: SnapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(binding.recyclerViewHomeAnnouncement)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir Gladun
Solution 2 David West
Solution 3 Ça?la Akgül