From 6b15c9b347a85a45a4b3a6e847aac36a525d93e1 Mon Sep 17 00:00:00 2001 From: Trinea Date: Fri, 30 May 2014 00:32:47 +0800 Subject: [PATCH] Add Infinite Loop --- AndroidManifest.xml | 4 +-- res/layout/auto_scroll_view_pager_demo.xml | 10 +++--- .../android/demo/AutoScrollViewPagerDemo.java | 34 ++++++++++++++----- .../demo/ViewPagerMulTiFragmentDemo.java | 3 +- .../demo/adapter/ImagePagerAdapter.java | 31 +++++++++++++++-- 5 files changed, 63 insertions(+), 19 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c59f23a..a3fde2e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="50" + android:versionName="3.2.5" > - + android:textSize="14sp" /> imageIdList; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState, R.layout.auto_scroll_view_pager_demo); viewPager = (AutoScrollViewPager)findViewById(R.id.view_pager); - indicator = (CirclePageIndicator)findViewById(R.id.indicator); + indexText = (TextView)findViewById(R.id.view_pager_index); - List imageIdList = new ArrayList(); + imageIdList = new ArrayList(); imageIdList.add(R.drawable.banner1); imageIdList.add(R.drawable.banner2); imageIdList.add(R.drawable.banner3); imageIdList.add(R.drawable.banner4); - viewPager.setAdapter(new ImagePagerAdapter(context, imageIdList)); - // indicator.setViewPager(viewPager); + viewPager.setAdapter(new ImagePagerAdapter(context, imageIdList).setInfiniteLoop(true)); + viewPager.setOnPageChangeListener(new MyOnPageChangeListener()); viewPager.setInterval(2000); viewPager.startAutoScroll(); - viewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % imageIdList.size()); + viewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % ListUtils.getSize(imageIdList)); // the more properties whose you can set // // set whether stop auto scroll when touching, default is true @@ -68,6 +71,21 @@ public void onClick(View v) { }); } + public class MyOnPageChangeListener implements OnPageChangeListener { + + @Override + public void onPageSelected(int position) { + indexText.setText(new StringBuilder().append((position) % ListUtils.getSize(imageIdList) + 1).append("/") + .append(ListUtils.getSize(imageIdList))); + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} + + @Override + public void onPageScrollStateChanged(int arg0) {} + } + @Override protected void onPause() { super.onPause(); diff --git a/src/cn/trinea/android/demo/ViewPagerMulTiFragmentDemo.java b/src/cn/trinea/android/demo/ViewPagerMulTiFragmentDemo.java index 13b6926..f1d21ee 100644 --- a/src/cn/trinea/android/demo/ViewPagerMulTiFragmentDemo.java +++ b/src/cn/trinea/android/demo/ViewPagerMulTiFragmentDemo.java @@ -41,8 +41,7 @@ protected void onCreate(Bundle savedInstanceState) { // to cache all page, or we will see the right item delayed viewPager.setOffscreenPageLimit(TOTAL_COUNT); viewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin)); - MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener(); - viewPager.setOnPageChangeListener(myOnPageChangeListener); + viewPager.setOnPageChangeListener(new MyOnPageChangeListener()); viewPagerContainer.setOnTouchListener(new OnTouchListener() { diff --git a/src/cn/trinea/android/demo/adapter/ImagePagerAdapter.java b/src/cn/trinea/android/demo/adapter/ImagePagerAdapter.java index c9a61f0..9ab6ba0 100644 --- a/src/cn/trinea/android/demo/adapter/ImagePagerAdapter.java +++ b/src/cn/trinea/android/demo/adapter/ImagePagerAdapter.java @@ -26,17 +26,29 @@ public class ImagePagerAdapter extends RecyclingPagerAdapter { private List imageIdList; private int size; + private boolean isInfiniteLoop; public ImagePagerAdapter(Context context, List imageIdList) { this.context = context; this.imageIdList = imageIdList; this.size = ListUtils.getSize(imageIdList); + isInfiniteLoop = false; } @Override public int getCount() { // Infinite loop - return Integer.MAX_VALUE; + return isInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(imageIdList); + } + + /** + * get really position + * + * @param position + * @return + */ + private int getPosition(int position) { + return isInfiniteLoop ? position % size : position; } @Override @@ -49,7 +61,7 @@ public View getView(int position, View view, ViewGroup container) { } else { holder = (ViewHolder)view.getTag(); } - holder.imageView.setImageResource(imageIdList.get(position % size)); + holder.imageView.setImageResource(imageIdList.get(getPosition(position))); return view; } @@ -57,4 +69,19 @@ private static class ViewHolder { ImageView imageView; } + + /** + * @return the isInfiniteLoop + */ + public boolean isInfiniteLoop() { + return isInfiniteLoop; + } + + /** + * @param isInfiniteLoop the isInfiniteLoop to set + */ + public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) { + this.isInfiniteLoop = isInfiniteLoop; + return this; + } }