Skip to content

Instantly share code, notes, and snippets.

@evant
Created November 17, 2014 21:56
Show Gist options
  • Save evant/86b42fb39d98349685f9 to your computer and use it in GitHub Desktop.
Save evant/86b42fb39d98349685f9 to your computer and use it in GitHub Desktop.
package me.tatarka.nofragment.view;
import android.support.v4.util.SparseArrayCompat;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by evan on 10/9/14.
*/
public abstract class ViewPagerAdapter extends PagerAdapter {
private ViewRecycler viewRecycler;
private boolean hasStableIds;
private int itemViewTypeCount;
private SparseArrayCompat<Long> itemIds;
private SparseArrayCompat<View> viewPositions;
public abstract Object getItem(int position);
public abstract View getView(int position, View convertView, ViewGroup parent);
public ViewPagerAdapter() {
hasStableIds = hasStableIds();
itemViewTypeCount = getItemViewTypeCount();
viewRecycler = new ViewRecycler(itemViewTypeCount);
if (hasStableIds) {
itemIds = new SparseArrayCompat<Long>();
viewPositions = new SparseArrayCompat<View>();
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View convertView = viewRecycler.obtainViewForType(getItemViewType(position));
View view = getView(position, convertView, container);
if (view != null) {
container.addView(view);
}
if (hasStableIds) {
itemIds.put(position, getItemId(position));
viewPositions.put(position, view);
}
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
viewRecycler.recycleView(getItemViewType(position), view);
if (hasStableIds) {
viewPositions.remove(position);
itemIds.remove(position);
}
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@Override
public int getItemPosition(Object object) {
if (hasStableIds) {
for (int i = 0; i < getCount(); i++) {
View view = viewPositions.get(i);
if (view == object) {
long oldId = itemIds.get(i);
long newId = getItemId(i);
if (oldId == newId) {
return i;
}
}
}
return POSITION_NONE;
} else {
return POSITION_NONE;
}
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return false;
}
public int getItemViewTypeCount() {
return 1;
}
public int getItemViewType(int position) {
return 0;
}
private static class ViewRecycler {
private List[] recycleViews;
public ViewRecycler(int viewTypeCount) {
recycleViews = new List[viewTypeCount];
for (int i = 0; i < viewTypeCount; i++) {
recycleViews[i] = new ArrayList();
}
}
public View obtainViewForType(int type) {
List views = recycleViews[type];
if (views.isEmpty()) {
return null;
} else {
return (View) views.remove(views.size() - 1);
}
}
public void recycleView(int type, View view) {
recycleViews[type].add(view);
}
}
}
@evant
Copy link
Author

evant commented Nov 17, 2014

Subclass this and use it like a ListAdapter. Does recycling for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment