当前位置 : 主页 > 网络编程 > 其它编程 >

自动滚动,兼容PageIndicator的ViewPager

来源:互联网 收集:自由互联 发布时间:2023-07-02
packagecn.paly.bz.view;importandroid.content.Context;importandroid.support.v4.view.Pager package cn.paly.bz.view;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import andro
packagecn.paly.bz.view;importandroid.content.Context;importandroid.support.v4.view.Pager

package cn.paly.bz.view;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * 顶部ViewPager * 自动滚动以及无限循环滚动 * @author Mr_wu * @mail 97046335@qq.com */public class HeadViewPager extends ViewPager implements Runnable { private PagerAdapter pagerAdapter; private static final int POST_DELAYED_TIME = 1000 * 5; public HeadViewPager(Context context, AttributeSet attrs) { super(context, attrs); postDelayed(this, POST_DELAYED_TIME); } // 对setAdapter的数据进行包装 private class MyPagerAdapter extends PagerAdapter { private PagerAdapter pa; public MyPagerAdapter(PagerAdapter pa) { this.pa = pa; } @Override // 关键之一:修改Count长度 public int getCount() { return pa.getCount() > 1 ? pa.getCount() + 2 : pa.getCount(); } @Override // 这里是关键之二:修改索引(如果不考虑内容问题可以全部加载进数组然后操作更简单) public Object instantiateItem(ViewGroup container, int position) { if (position == 0) { return pa.instantiateItem(container, pa.getCount() - 1); } else if (position == pa.getCount() + 1) { return pa.instantiateItem(container, 0); } else { return pa.instantiateItem(container, position - 1); } } @Override public void destroyItem(ViewGroup container, int position, Object object) { pa.destroyItem(container, position, object); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return pa.isViewFromObject(arg0, arg1); } } // 包装setOnPageChangeListener的数据 private class MyOnPageChangeListener implements OnPageChangeListener { private OnPageChangeListener listener; public MyOnPageChangeListener(OnPageChangeListener listener) { this.listener = listener; } @Override // 关键之三: public void onPageScrollStateChanged(int arg0) { if (arg0 == SCROLL_STATE_IDLE) { if (getCurrentItem() + 1 == 0) { setCurrentItem(pagerAdapter.getCount() - 1, false); } else if (getCurrentItem() + 1 == pagerAdapter.getCount() + 1) { setCurrentItem(0, false); } } listener.onPageScrollStateChanged(arg0); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { listener.onPageScrolled(arg0, arg1, arg2); } @Override // 关键四: public void onPageSelected(int arg0) { if (arg0 == 0) { // } else if (arg0 == pagerAdapter.getCount() + 1) { // } else { listener.onPageSelected(arg0 - 1); } } } @Override public void setOnPageChangeListener(OnPageChangeListener listener) { super.setOnPageChangeListener(listener == null ? null : new MyOnPageChangeListener(listener)); } @Override public void setAdapter(PagerAdapter arg0) { super.setAdapter(arg0 == null ? null : new MyPagerAdapter(arg0)); if (arg0 != null } this.pagerAdapter = arg0; } @Override // 兼容PageIndicator public PagerAdapter getAdapter() { return pagerAdapter; } @Override public int getCurrentItem() { return super.getCurrentItem() - 1; } @Override public void setCurrentItem(int item) { setCurrentItem(item, true); } @Override public void setCurrentItem(int item, boolean smoothScroll) { super.setCurrentItem(item + 1, smoothScroll); } @Override // 自动滚动关键 public void run() { if (getAdapter().getCount() > 1) { setCurrentItem(getCurrentItem() + 1, true); } postDelayed(this, POST_DELAYED_TIME); }}

>详细原理参见:http://stackoverflow.com/a/12965787

上一篇:PJSIP学习笔记11
下一篇:没有了
网友评论