Skip to content

Commit

Permalink
Add Infinite Loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed May 29, 2014
1 parent 0b75378 commit 6b15c9b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.trinea.android.demo"
android:versionCode="47"
android:versionName="3.1.5" >
android:versionCode="50"
android:versionName="3.2.5" >

<uses-sdk
android:minSdkVersion="9"
Expand Down
10 changes: 5 additions & 5 deletions res/layout/auto_scroll_view_pager_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
android:id="@+id/auto_scroll_view_pager_inner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:layout_marginBottom="@dimen/dp_40"
android:layout_marginTop="@dimen/dp_20"
android:text="@string/auto_scroll_view_pager_inner" />

<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
Expand All @@ -17,13 +17,13 @@
android:layout_height="@dimen/view_pager_height"
android:layout_below="@+id/auto_scroll_view_pager_inner" />

<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
<TextView
android:id="@+id/view_pager_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view_pager"
android:layout_below="@+id/view_pager"
android:layout_centerHorizontal="true"
android:padding="@dimen/dp_8" />
android:textSize="14sp" />

<include
android:layout_width="match_parent"
Expand Down
34 changes: 26 additions & 8 deletions src/cn/trinea/android/demo/AutoScrollViewPagerDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cn.trinea.android.common.util.ListUtils;
import cn.trinea.android.demo.adapter.ImagePagerAdapter;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;

import com.viewpagerindicator.CirclePageIndicator;

/**
* AutoScrollViewPagerDemo
*
Expand All @@ -21,28 +22,30 @@
public class AutoScrollViewPagerDemo extends BaseActivity {

private AutoScrollViewPager viewPager;
private CirclePageIndicator indicator;
private TextView indexText;

private Button innerViewPagerDemo;

private List<Integer> 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<Integer> imageIdList = new ArrayList<Integer>();
imageIdList = new ArrayList<Integer>();
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
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/cn/trinea/android/demo/ViewPagerMulTiFragmentDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
31 changes: 29 additions & 2 deletions src/cn/trinea/android/demo/adapter/ImagePagerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,29 @@ public class ImagePagerAdapter extends RecyclingPagerAdapter {
private List<Integer> imageIdList;

private int size;
private boolean isInfiniteLoop;

public ImagePagerAdapter(Context context, List<Integer> 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
Expand All @@ -49,12 +61,27 @@ 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;
}

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;
}
}

0 comments on commit 6b15c9b

Please sign in to comment.