Using a page adapter without fragments

Can I use ViewPager without fragments? All the examples that I could find load several fragments in the ViewPager.

I want to use it similarly to ListView . I would like to create a custom adapter that will inflate the layout according to the position of the pager.

If possible, can someone give me an example for this?

+6
source share
3 answers

Yes, it is quite possible. Since ViewPager.setAdapter() accepts any instance of PageAdapter , you just need to create your own subclass of PagerAdapter (it's kind of like subclassing BaseAdapter or ArrayAdapter that you use in ListView ). just remember that for the PAgerAdapter, the docs say:

When implementing the PagerAdapter, you must override the following methods at a minimum:

Hope this helps

+7
source

I had a similar problem and it took me a while to figure this out. Send my adapter here for future reference.

 /** * A page adapter which works with a large data set by reusing views. */ public abstract class ListPageAdapter<T> extends PagerAdapter { // Views that can be reused. private final List<View> mDiscardedViews = new ArrayList<View>(); // Views that are already in use. private final SparseArray<View> mBindedViews = new SparseArray<View>(); private final ArrayList<T> mItems; private final LayoutInflater mInflator; private final int mResourceId; public ListPageAdapter(Context context, int viewRes) { mItems = new ArrayList<T>(); mInflator = LayoutInflater.from(context); mResourceId = viewRes; } @Override public int getCount() { return mItems.size(); } @Override public boolean isViewFromObject(View v, Object obj) { return v == mBindedViews.get(mItems.indexOf(obj)); } @Override public void destroyItem(ViewGroup container, int position, Object object) { View view = mBindedViews.get(position); if (view != null) { mDiscardedViews.add(view); mBindedViews.remove(position); container.removeView(view); } } @Override public Object instantiateItem(ViewGroup container, int position) { View child = mDiscardedViews.isEmpty() ? mInflator.inflate(mResourceId, container, false) : mDiscardedViews.remove(0); T data = mItems.get(position); initView(child, data, position); mBindedViews.append(position, child); container.addView(child, 0); return data; } public void add(T item) { mItems.add(item); } public T remove(int position) { return mItems.remove(position); } public void clear() { mItems.clear(); } /** * Initiate the view here */ public abstract void initView(View v, T item, int position); } 

ViewPager does not update automatically when data changes in the adapter. For this I used

 pager.setAdapter(null); pager.setAdapter(myAdapter); pager.setCurrentItem(0); 
+11
source

This is a Kotlin solution. Set the object of this class as the adapter viewPager-

 class PageAdapter : PagerAdapter() { private val pageLayouts = listOf(R.layout.home_page_1, R.layout.home_page_2) override fun instantiateItem(container: ViewGroup, position: Int): Any = LayoutInflater.from(container.context).inflate(pageLayouts[position], container, false).apply { container.addView(this) } override fun destroyItem(container: ViewGroup, position: Int, 'object': Any) = container.removeView('object' as View) override fun getCount(): Int = 2 override fun isViewFromObject(view: View, 'object': Any): Boolean = (view == 'object') } 
0
source

Source: https://habr.com/ru/post/951267/


All Articles