ViewPager相信大家都很熟悉,4.x前都需要导入android-support-v4.jar,4.x后sdk已提供支持; 应用场景也很常见,一般应用于程序的引导页;应用也很简单,一个Adapter就可以搞定,代码如下:
publicclassMyViewPagerAdapterextendsPagerAdapter{
privateList mListViews;
publicMyViewPagerAdapter(List mListViews) {
this.mListViews = mListViews;//构造方法,参数是我们的页卡,这样比较方便。
}
@Override
publicvoiddestroyItem(ViewGroup container,intposition, Object object) {
container.removeView(mListViews.get(position));//删除页卡
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {//这个方法用来实例化页卡
container.addView(mListViews.get(position), 0);//添加页卡
returnmListViews.get(position);
}
@Override
publicintgetCount() {
returnmListViews.size();//返回页卡的数量
}
@Override
publicbooleanisViewFromObject(View arg0, Object arg1) {
returnarg0==arg1;//官方提示这样写
}
}
但这不是我们今天要讲的主题,我们今天说的是关键词是:动态加载、循环滑屏….
应用场景不再局限与程序引导,而在数据交互之间体现;下面请看实现效果图:
下面我们来看看如何去做,先上Activity代码,代码是主项目中抠下来的:
viewPager = (ViewPager) findViewById(R.id.viewPager);
listViews = newArrayList();
//初始化ViewPager
bookShelfView = newBookshelfCompositeView(this);//
bookCityView = newOxygenBookCityView(this);//
readFriendsView = newReadFriendsCircleView(this);//
listViews.add(bookShelfView);//
listViews.add(bookCityView);//
listViews.add(readFriendsView);//
mPagerAdapter = newMyPagerAdapter(listViews,this);
mPagerAdapter.setLoadTag(false);
viewPager.setAdapter(mPagerAdapter);
viewPager.setCurrentItem(COUNT);
viewPager.setOnPageChangeListener(listener);
/**
* 创建View
*/
publicvoidcreateView(intcurrentItem) {
if(currentItem == BOOKSHELF_ITEM) {
// 书架
if(bookShelfViewinstanceofIPanelView) {
((IPanelView) bookShelfView).onCreate();
}
} elseif(currentItem == BOOKCITY_ITEM) {
// 书城
if(bookCityViewinstanceofIPanelView) {
((IPanelView) bookCityView).onCreate();
}
} elseif(currentItem == READFRIENDS_ITEM) {
// 读友圈
if(readFriendsViewinstanceofIPanelView) {
((IPanelView) readFriendsView).onCreate();
}
}
}
OnPageChangeListener listener = newOnPageChangeListener() {
@Override
publicvoidonPageSelected(intarg0) {
// TODO Auto-generated method stub
LogUtil.e("当前页面索引:"+ arg0);
}
@Override
publicvoidonPageScrolled(intarg0,floatarg1,intarg2) {
// TODO Auto-generated method stub
}
@Override
publicvoidonPageScrollStateChanged(intarg0) {
// TODO Auto-generated method stub
}
};
Activity主要对ViewPager做了初始化的操作,主要是Adapter代码实现:
publicclassMyPagerAdapterextendsPagerAdapter {
publicList listViews =null;
publicHashMap loadTag;
publicMainNewActivity mContext;
publicintmIndex;//滑动索引
publicMyPagerAdapter(List listViews, MainNewActivity mContext) {
this.listViews = listViews;
this.mContext = mContext;
loadTag = newHashMap();
}
/**
* 设置加载标志
*
* @param tag
*/
publicvoidsetLoadTag(booleantag) {
if(listViews !=null&& listViews.size() >0) {
intsize = listViews.size();
for(View views : listViews) {
loadTag.put(views, tag);
}
}
}
@Override
publicintgetCount() {
returnInteger.MAX_VALUE;
}
@Override
publicbooleanisViewFromObject(View arg0, Object arg1) {
returnarg0 == arg1;
}
@Override
publicvoiddestroyItem(View container,intposition, Object object) {
// LogUtil.e("TipsPagerAdapter", "destroyItem is called " + position);
// ((ViewPager) container).removeView(listViews.get(position %
// listViews.size()));
}
@Override
publicCharSequence getPageTitle(intposition) {
returnsuper.getPageTitle(position);
}
@Override
publicObject instantiateItem(View container,intposition) {
LogUtil.e("TipsPagerAdapter","instantiateItem is called "+ position);
try{
((ViewPager) container).addView(
listViews.get(position % listViews.size()), 0);
} catch(Exception e) {
}
// ((ViewPager) container).addView(listViews.get(position);
// return listViews.get(position);
LogUtil.e("instantiateItem>>>>>>"+ position % listViews.size());
returnlistViews.get(position % listViews.size());
}
/**
* 进行View预加载处理
*/
@Override
publicvoidsetPrimaryItem(View container,intposition, Object object) {
// TODO Auto-generated method stub
super.setPrimaryItem(container, position, object);
LogUtil.e("setPrimaryItem>>>>>>"+ position % listViews.size());
mIndex = position % listViews.size();
if(loadTag.get(listViews.get(mIndex)) ==false) {
mContext.createView(mIndex);
loadTag.put(listViews.get(mIndex), true);
}
}
}
这里有必要解释和注意一下:
循环滑动是通过初始化一个较大的CurrentItem(页数)在getview中通过position % listViews.size()实现,看上去实现了循环滑动,实则假循环,但当用户向左滑动CurrentItem次后就无法滑动,所以建议将CurrentItem值越大越好,试想一下哪个用户那么无聊不停去滑动直到突破你的滑动值呢。
动态加载在setPrimaryItem中实现,每次滑动都会调用setPrimaryItem,所以在这里可以进行网络加载,这里我loadTag判断是否需要重复加载。
讲到这里就今天的内容就告一段落。由于这个实现是项目中的一部分,所以源码今天就不放了,等有时间我将整理成一个demo放上来。