在網上看到了viewpager以後本身看了看,效果不錯,一樣eoe社區也有不少相關的文章,好比http://www.eoeandroid.com/forum.php?mod=viewthread&tid=157771&page=21#pid1384160,你們能夠看看,使用viewpager的時候你們不要忘了導入android-support-v4.jar這個包,本身能夠去下載。 可是在使用的時候發現以上找到的viewpager不能實現循環滑動,這對於用戶體驗可能不是太好,因此本身又開始在此基礎上尋找其餘的方法,最終發現瞭如下解決辦法: 將MyPagerAdapter修改一下:
004 |
public class MyPagerAdapter extends PagerAdapter { |
005 |
public List<View> views; |
008 |
public MyPagerAdapter(Context context,List<View> views) { |
010 |
this.context=context; |
011 |
mCount = views.size(); |
014 |
public void destroyItem(View collection, int position, Object arg2) { |
015 |
if (position >= views.size()) { |
016 |
int newPosition = position%views.size(); |
017 |
position = newPosition; |
018 |
// ((ViewPager) collection).removeView(views.get(position)); |
021 |
position = -position; |
022 |
// ((ViewPager) collection).removeView(views.get(position)); |
026 |
public void finishUpdate(View arg0) { |
029 |
public int getCount() { |
030 |
return mCount+1;//此處+1才能向右連續滾動 |
033 |
public Object instantiateItem(View collection, int position) { |
034 |
if (position >= views.size()) { |
035 |
int newPosition = position%views.size(); |
037 |
position = newPosition; |
041 |
position = -position; |
045 |
((ViewPager) collection).addView(views.get(position), 0); |
046 |
} catch (Exception e) { |
048 |
return views.get(position); |
051 |
public boolean isViewFromObject(View view, Object object) { |
052 |
return view == (object); |
055 |
public void restoreState(Parcelable arg0, ClassLoader arg1) { |
058 |
public Parcelable saveState() { |
062 |
public void startUpdate(View arg0) { |
065 |
同時若是你要的效果裏面有上面連接中的那個白色的動畫效果,一樣也須要再修改一個地方 |
069 |
public class MyOnPageChangeListener implements OnPageChangeListener { |
070 |
int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量 |
071 |
int two = one * 2;// 頁卡1 -> 頁卡3 偏移量 |
073 |
public void onPageSelected(int arg0) { |
074 |
Animation animation = null; |
080 |
if (currIndex == 1) { |
081 |
animation = new TranslateAnimation(one, 0, 0, 0); |
082 |
} else if (currIndex == 2) { |
083 |
animation = new TranslateAnimation(two, 0, 0, 0); |
087 |
if (currIndex == 0) { |
088 |
animation = new TranslateAnimation(offset, one, 0, 0); |
089 |
} else if (currIndex == 2) { |
090 |
animation = new TranslateAnimation(two, one, 0, 0); |
094 |
if (currIndex == 0) { |
095 |
animation = new TranslateAnimation(offset, two, 0, 0); |
096 |
} else if (currIndex == 1) { |
097 |
animation = new TranslateAnimation(one, two, 0, 0); |
102 |
animation.setFillAfter(true);// True:圖片停在動畫結束位置 |
103 |
animation.setDuration(300); |
104 |
cursor.startAnimation(animation); |
107 |
public void onPageScrolled(int arg0, float arg1, int arg2) { |
110 |
public void onPageScrollStateChanged(int arg0) { |
這樣一來,其餘地方不須要修改,便可實現viewpager的循環滑動效果 php
本身繼續修改瞭如下,發現能夠實現向左無限循環(貌似是無限)的,個人方法以下 有幾個方法作如下改動就好了 android
02 |
public void destroyItem(View collection, int position, Object arg2) { |
04 |
// ((ViewPager) collection).removeView(views.get(position%views.size())); |
08 |
public void finishUpdate(View arg0) { |
12 |
public int getCount() { |
13 |
return Integer.MAX_VALUE;//設置成最大值以便循環滑動 |
17 |
public Object instantiateItem(View collection, int position) { |
19 |
((ViewPager) collection).addView(views.get(position%views.size()), 0); |
20 |
} catch (Exception e) { |
22 |
return views.get(position%views.size()); |
最後再初始化頁面時mPager.setCurrentItem(3*100);//設置初始頁面,爲0的話開始不能向左滑動
這樣的話就能實現相似的無限循環(向左其實只有100次,只是通常沒人會在那兒向左移動那麼屢次而已) ide
涉及ViewPager更新問題 動畫
ViewPager的PagerAdapter不能夠更新數據 this
在作項目的時候,發現即便調用了galleryAdapter.notifyDataSetChanged(); spa
可是ViewPager仍是不會更新原來的數據。 .net
後來在stackoverflow上面找到了方法,原文連接: rest
http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view orm
因而花了一點時間,修改了代碼: blog
01 |
protected PagerAdapter galleryAdapter = new PagerAdapter() { |
04 |
public boolean isViewFromObject(View arg0, Object arg1) { |
05 |
return arg0 == ((View)arg1); |
09 |
public int getCount() { |
14 |
public Object instantiateItem(View container, int position) { |
15 |
return bindGalleryAdapterItemView(container, position); |
19 |
public void destroyItem(View container, int position, Object object) { |
20 |
((ViewPager) container).removeView((View) object); |
24 |
public void finishUpdate(View arg0) {} |
27 |
public void restoreState(android.os.Parcelable state, ClassLoader loader) { |
32 |
public Parcelable saveState() { |
37 |
public void startUpdate(View arg0) {} |
40 |
public int getItemPosition(Object object) { |
注意:POSITION_NONE 是一個PagerAdapter的內部常量,值是-2, API裏面有說明: int android.support.v4.view.PagerAdapter.POSITION_NONE = -2 [0xfffffffe] 能夠更新數據了。嘿嘿。