Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using page adapter without fragments

Is it possible to use ViewPager without fragments? All examples i could find load several fragments to the ViewPager.

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

if it's possible can anyone point me to an example for that ?

like image 223
Mortalus Avatar asked Aug 08 '13 04:08

Mortalus


People also ask

Can I use ViewPager with views not with fragments?

yes...you can use View instead of Fragment in viewpager.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties when using ViewPager . ViewPager2 has several advantages such as vertical orientation support, RTL and access to DiffUtil .

What is the difference between FragmentPagerAdapter vs FragmentStatePagerAdapter?

FragmentPagerAdapter calls detach(Fragment) on the transaction instead of remove(Fragment). FragmentStatePagerAdapter: the fragment instance is destroyed when it is not visible to the User, except the saved state of the fragment.

How do I use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

I had the similar problem and it took me some time to figure it out. Posting 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);
}

The viewPager is not updated automatically when the data is changed int the adapter. For that I used

pager.setAdapter(null);
pager.setAdapter(myAdapter);
pager.setCurrentItem(0);
like image 144
Painless Avatar answered Sep 20 '22 06:09

Painless