Using page adapter without fragments
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);
Yes its totally possible. Since ViewPager.setAdapter()
takes any instance PageAdapter
, you just need to create your own subclass of PagerAdapter (its kinda like subclassing BaseAdapter or ArrayAdapter that you use in a ListView
). just remember, for PAgerAdapter the docs say:
When you implement a PagerAdapter, you must override the following methods at minimum:
- instantiateItem(ViewGroup, int)
- destroyItem(ViewGroup, int, Object)
- getCount()
- isViewFromObject(View, Object)
Hope this helps