2012年10月29日月曜日

How to create looping ViewPager

ループできる ViewPager です。ちゃんと作ってないので以下の制限があります。
  • ページ数が 3の倍数じゃないといけない(時間がなくて対応してない)
  • 連続ページ切り替え(スワイプ後のスクロールが終わる前にさらにスワイプ)に対応してない
  • わかんないけどなんかあるかも
ポイントは isViewFromObject() を駆使することと、ViewPager を extends して onLayout で位置を戻すことです。
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ViewPager mViewPager;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.   
  10.         mPagerAdapter = new MyPagerAdapter(this);  
  11.         mViewPager = (ViewPager) findViewById(R.id.viewpager);  
  12.         mViewPager.setOffscreenPageLimit(2);  
  13.         mViewPager.setAdapter(mPagerAdapter);  
  14.         mViewPager.setOnPageChangeListener(mPagerAdapter);  
  15.         mViewPager.setCurrentItem(1false);  
  16.     }  
  17.   
  18.     private MyPagerAdapter mPagerAdapter;  
  19.   
  20.     private class MyPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {  
  21.   
  22.         /** 
  23.          * from 0 to 2 
  24.          */  
  25.         int focusedPosition = 0;  
  26.   
  27.         /** 
  28.          * from 0 to PAGE_NUM -1 
  29.          */  
  30.         int contentPosition = 0;  
  31.   
  32.         /** 
  33.          * number of pages (3x) 
  34.          */  
  35.         int PAGE_NUM = 9;  
  36.   
  37.         Context mContext;  
  38.         LayoutInflater mInflater;  
  39.   
  40.         public MyPagerAdapter(Context context) {  
  41.             mContext = context;  
  42.             mInflater = LayoutInflater.from(context);  
  43.         }  
  44.   
  45.         @Override  
  46.         public int getCount() {  
  47.             return 3;  
  48.         }  
  49.   
  50.         SparseArray<View> mViews = new SparseArray<View>();  
  51.   
  52.         @Override  
  53.         public Object instantiateItem(ViewGroup container, int position) {  
  54.             int newPosition = calcContentPosition(position);  
  55.             View view = createContent(newPosition);  
  56.             container.addView(view, 0);  
  57.             mViews.put(position, view);  
  58.             view.setTag(position);  
  59.             // return view;  
  60.             return Integer.valueOf(position);  
  61.         }  
  62.   
  63.         @Override  
  64.         public boolean isViewFromObject(View view, Object object) {  
  65.             int index = (Integer) view.getTag();  
  66.             int position = (Integer) object;  
  67.             int newPosition = (position + contentPosition) % 3;  
  68.             return newPosition == index;  
  69.         }  
  70.   
  71.         /** 
  72.          * create content's view 
  73.          *  
  74.          * @param position 
  75.          *            from 0 to PAGE_NUM - 1 
  76.          * @return 
  77.          */  
  78.         private View createContent(int position) {  
  79.             View view = mInflater.inflate(R.layout.list, nullfalse);  
  80.             updateContent(view, position);  
  81.             return view;  
  82.         }  
  83.   
  84.         /** 
  85.          * update content's view 
  86.          *  
  87.          * @param view 
  88.          * @param position 
  89.          *            from 0 to PAGE_NUM -1 
  90.          */  
  91.         private void updateContent(View view, int position) {  
  92.             ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,  
  93.                     createList(position));  
  94.             ListView listView = (ListView) view.findViewById(R.id.list);  
  95.             listView.setAdapter(adapter);  
  96.             listView.setScrollingCacheEnabled(false);  
  97.         }  
  98.   
  99.         private List<String> createList(int index) {  
  100.             List<String> list = new ArrayList<String>();  
  101.             for (int i = 0; i < 50; i++) {  
  102.                 list.add(index + " *************************** " + index + ":" + (i + 1));  
  103.             }  
  104.             return list;  
  105.         }  
  106.   
  107.         /** 
  108.          * get position of content 
  109.          *  
  110.          * @param position 
  111.          *            from 0 to 2 
  112.          * @return content position (from 0 to PAGE_NUM - 1) 
  113.          */  
  114.         private int calcContentPosition(int position) {  
  115.             int offset = position - 1;  
  116.             int newPosition = contentPosition + offset;  
  117.             if (newPosition < 0) {  
  118.                 newPosition = PAGE_NUM - 1;  
  119.             } else {  
  120.                 newPosition = newPosition % PAGE_NUM;  
  121.             }  
  122.             return newPosition;  
  123.         }  
  124.   
  125.         @Override  
  126.         public void destroyItem(ViewGroup container, int position, Object object) {  
  127.             int count = mViewPager.getChildCount();  
  128.             int index = (Integer) object;  
  129.             for (int i = 0; i < count; i++) {  
  130.                 View v = mViewPager.getChildAt(i);  
  131.                 if (isViewFromObject(v, Integer.valueOf(index))) {  
  132.                     container.removeView(v);  
  133.                     break;  
  134.                 }  
  135.             }  
  136.         }  
  137.   
  138.         /** 
  139.          * update specified view's content 
  140.          *  
  141.          * @param index 
  142.          *            0 or 2 
  143.          */  
  144.         void updateContents(int index) {  
  145.             int count = mViewPager.getChildCount();  
  146.             for (int i = 0; i < count; i++) {  
  147.                 View v = mViewPager.getChildAt(i);  
  148.                 if (isViewFromObject(v, Integer.valueOf(index))) {  
  149.                     final int newPosition = calcContentPosition(index);  
  150.                     updateContent(v, newPosition);  
  151.                     break;  
  152.                 }  
  153.             }  
  154.         }  
  155.   
  156.         @Override  
  157.         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  158.         }  
  159.   
  160.         @Override  
  161.         public void onPageSelected(int position) {  
  162.             focusedPosition = position;  
  163.         }  
  164.   
  165.         @Override  
  166.         public void onPageScrollStateChanged(int state) {  
  167.             if (state == ViewPager.SCROLL_STATE_IDLE) {  
  168.                 handlePageScrollStateChangedToIdle();  
  169.             }  
  170.         }  
  171.   
  172.         private void handlePageScrollStateChangedToIdle() {  
  173.             switch (focusedPosition) {  
  174.                 case 0:  
  175.                     // scroll to previous page  
  176.                     if ((--contentPosition) < 0) {  
  177.                         contentPosition = PAGE_NUM - 1;  
  178.                     }  
  179.                     updateContents(0);  
  180.                     break;  
  181.   
  182.                 case 2:  
  183.                     // scroll to next page  
  184.                     contentPosition = (++contentPosition) % PAGE_NUM;  
  185.                     updateContents(2);  
  186.                     break;  
  187.                 default:  
  188.                     break;  
  189.             }  
  190.         }  
  191.     }  
  192. }  
  1. public class MyViewPager extends ViewPager {  
  2.   
  3.     public MyViewPager(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.     }  
  6.       
  7.     @Override  
  8.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  9.         super.onLayout(changed, l, t, r, b);  
  10.           
  11.         if(getCurrentItem() != 1) {  
  12.             setCurrentItem(1false);  
  13.         }  
  14.     }  
  15. }  
res/layout/main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <com.sample.viewpager.MyViewPager   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/viewpager"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent" />  
res/layout/list.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ListView   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/list"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:cacheColorHint="#00000000" />  

3 件のコメント:

  1. It doesn't work when I put a methode
    @Override
    public float getPageWidth(int position) {
    return 0.4f;
    }
    in MyPagerAdapter class

    返信削除
  2. And also how can i change it all for fragment instead of Views

    返信削除