// log_begin
private static final DetailedLog Log = new DetailedLog();
private static final String LOG_TAG = Common.LOG_TAG;
// log_end
private LayoutInflater inflater = null;
protected abstract LayoutInflater getInflater();
protected abstract int getItemViewType(int position);
protected abstract int getLayoutId(int position, int itemViewType);
protected abstract int[] getViewIds(int position, int layoutId);
protected abstract void handleView(int position, View v, View parent);
private SparseArray<ArrayList<View>> viewCache = new SparseArray<ArrayList<View>>();
/**
重点看这里
**/
@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {if (inflater == null) {
inflater = getInflater();
}
View convertView = null;
//取到视图类型
int itemViewType = getItemViewType(arg1);ArrayList<View> convertViews = viewCache.get(itemViewType);
ArrayList<View> views = null;
if (convertViews == null || convertViews.isEmpty()) {
//取到视图布局id
int layoutId = getLayoutId(arg1, itemViewType);convertView = inflater.inflate(layoutId, null);
if (convertView != null) {
int[] viewIds = getViewIds(arg1, layoutId);
if (viewIds != null && viewIds.length != 0) {
views = new ArrayList<View>();
for (Integer viewId : viewIds) {
View view = convertView.findViewById(viewId);
if (view != null) {
views.add(view);
} else {
throw new NullPointerException("layout(id:"
+ layoutId + ") findViewById(" + viewId
+ ")");
}
}
convertView.setTag(views);
}
} else {
throw new NullPointerException("inflate layout(id:" + layoutId
+ ")");
}
} else {
// 重用视图
convertView = convertViews.remove(convertViews.size() - 1);views = (ArrayList<View>) convertView.getTag();
}
if (views != null) {
for (View v : views) {
handleView(arg1, v, convertView);
}
}
arg0.addView(convertView);
return convertView;
}
/**重点看这里**/
@Overridepublic void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
View view = (View) arg2;
arg0.removeView(view);
//缓存视图以重用
int itemViewType = getItemViewType(arg1);ArrayList<View> convertViews = viewCache.get(itemViewType);
if (convertViews == null) {
convertViews = new ArrayList<View>();
viewCache.put(itemViewType, convertViews);
}
convertViews.add(view);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void finishUpdate(View arg0) {
}
}
用例
public class MainActivity extends Activity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
MultifariousPageAdapter mSectionsPagerAdapter;
//经过缓存重用,这里定义了3000个页面也没问题
int pageCount = 3000;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
/**使用方法重点看这里**/
mSectionsPagerAdapter = new MultifariousPageAdapter() {
@Override
public int getCount() {
// TODO 自动生成的方法存根
return pageCount;
}
@Override
protected void handleView(int position, View v, View parent) {
// TODO 自动生成的方法存根
Toast.makeText(
MainActivity.this,
"position=" + position + ",v=" + v + ",parent="
+ parent, Toast.LENGTH_LONG).show();
switch (v.getId()) {
case R.id.page1_label:
// ((TextView) v).setText("这是页面"+ Integer.toString(position
// + 1));
break;
default:
break;
}
}
//提供视图中的view id
@Override
protected int[] getViewIds(int position, int layoutId) {
// TODO 自动生成的方法存根
switch (layoutId) {
case R.layout.page1:
return new int[] { R.id.page1_label };
default:
break;
}
return null;
}
// 根据视图类型提供layout id
@Override
protected int getLayoutId(int position, int itemViewType) {
// TODO 自动生成的方法存根
switch (itemViewType) {
case 0:
return R.layout.page1;
case 1:
return R.layout.page2;
case 2:
return R.layout.page3;
case 3:
return R.layout.page4;
case 4:
return R.layout.page5;
case 5:
return R.layout.page6;
default:
return 0;
}
}
@Override
protected LayoutInflater getInflater() {
// TODO 自动生成的方法存根
return LayoutInflater.from(MainActivity.this);
}
@Override
public CharSequence getPageTitle(int position) {
// TODO 自动生成的方法存根
return Integer.toString(position + 1);
}
// 共有0、1、2、3、4、5 共6种视图类型
@Override
protected int getItemViewType(int position) {
// TODO 自动生成的方法存根
return position % 6;
}
};
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
……
}
完整例子:http://download.csdn.net/detail/z13759561330/8660511
APK:http://download.csdn.net/detail/z13759561330/8660549