Android-viewpager滑动监听

http://blog.csdn.net/lvyoujt/article/details/51138744

http://blog.csdn.net/lvyoujt/article/details/51147669

package com.cctvjiatao.viewpagedemo;  
  
import java.util.ArrayList;  
import java.util.List;  
import android.os.Bundle;  
import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentActivity;  
import android.support.v4.view.ViewPager;  
import android.support.v4.view.ViewPager.OnPageChangeListener;  
import android.util.Log;  
/** 
 * @作者: jiatao 
 * @修改时间:2016-4-12 上午7:53:22  
 * @包名:com.cctvjiatao.viewpagedemo 
 * @文件名:FragAdapter.java 
 * @版权声明:www.cctvjiatao.com 
 * @功能: 通过输出日志认识 ViewPager的滑动监听器 OnPageChangeListener 
 */  
public class MainActivity extends FragmentActivity {  
      
    private final String TAG = getClass().getSimpleName();  
  
    private ViewPager viewpager;  
    private FragAdapter adapter;  
    private List list;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        viewpager = (ViewPager) findViewById(R.id.viewpager);  
          
        list = new ArrayList();  
        list.add(new Frag1());  
        list.add(new Frag2());  
          
        adapter = new FragAdapter(getSupportFragmentManager(), list);  
        viewpager.setAdapter(adapter);  
        viewpager.setCurrentItem(0);  
          
        /** 
         * 滑动监听器OnPageChangeListener 
         *  OnPageChangeListener这个接口需要实现三个方法:onPageScrollStateChanged,onPageScrolled ,onPageSelected 
         *      1、onPageScrollStateChanged(int arg0) 此方法是在状态改变的时候调用。 
         *          其中arg0这个参数有三种状态(0,1,2) 
         *              arg0 ==1的时表示正在滑动,arg0==2的时表示滑动完毕了,arg0==0的时表示什么都没做 
         *              当页面开始滑动的时候,三种状态的变化顺序为1-->2-->0 
         *      2、onPageScrolled(int arg0,float arg1,int arg2) 当页面在滑动的时候会调用此方法,在滑动被停止之前,此方法回一直被调用。 
         *          其中三个参数的含义分别为: 
         *              arg0 :当前页面,及你点击滑动的页面 
         *              arg1:当前页面偏移的百分比 
         *              arg2:当前页面偏移的像素位置  
         *      3、onPageSelected(int arg0) 此方法是页面跳转完后被调用,arg0是你当前选中的页面的Position(位置编号) 
         */  
        viewpager.setOnPageChangeListener(new OnPageChangeListener() {  
              
            @Override  
            public void onPageSelected(int arg0) {  
                // arg0是当前选中的页面的Position  
                Log.e(TAG, "onPageSelected------>"+arg0);  
            }  
              
            @Override  
            public void onPageScrolled(int arg0, float arg1, int arg2) {  
                // arg0 :当前页面,及你点击滑动的页面;arg1:当前页面偏移的百分比;arg2:当前页面偏移的像素位置  
                Log.e(TAG, "onPageScrolled------>arg0:"+arg0+"\nonPageScrolled------>arg1:"+arg1+"\nonPageScrolled------>arg2:"+arg2);  
            }  
              
            @Override  
            public void onPageScrollStateChanged(int arg0) {  
                //arg0 ==1的时表示正在滑动,arg0==2的时表示滑动完毕了,arg0==0的时表示什么都没做。  
                if(arg0 == 0){  
                    Log.e(TAG, "onPageScrollStateChanged------>0");  
                }else if(arg0 == 1){  
                    Log.e(TAG, "onPageScrollStateChanged------>1");  
                }else if(arg0 == 2){  
                    Log.e(TAG, "onPageScrollStateChanged------>2");  
                }  
                  
            }  
        });  
    }  
  
}  


package com.cctvjiatao.viewpagerdemo;  
  
import java.lang.reflect.Field;  
import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.support.v4.view.ViewPager;  
import android.support.v4.widget.EdgeEffectCompat;  
import android.util.Log;  
import android.view.ViewGroup;  
import android.view.Window;  
import android.view.WindowManager;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.Toast;  
/** 
 * @作者: jiatao 
 * @修改时间:2016-4-13 下午6:18:27 
 * @文件名:MainActivity.java 
 * @版权声明:www.cctvjiatao.com 
 * @功能:    1、理解运用 PagerAdapter 
 *          2、深入理解 OnPageChangeListener中的onPageScrollStateChanged(int state) 
 *          3、ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法 
 */  
  
/* 
 * ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法:打开用“//”注释掉的代码即可 
 */  
  
public class MainActivity extends Activity {  
  
    private Context mContext;  
    private ViewPager mViewPager;  
    private StartPagerAdapter mStartPagerAdapter;  
    private ImageView[] dotImageViews;  
    private PageChangeListenerImpl mPageChangeListenerImpl;  
    private LinearLayout mDotsLinearLayout;  
    private boolean misScrolled = false;  
      
    //ViewPager在拖拽到最左边或最右边的时候,会显示黄色或蓝色的渐变图片,如果要禁止显示这些渐变,需要用到下面这两个属性  
//  private EdgeEffectCompat leftEdge;  
//  private EdgeEffectCompat rightEdge;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        mContext = this;  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        setContentView(R.layout.activity_main);  
        initView();  
    }  
  
    private void initView() {  
        mViewPager = (ViewPager) findViewById(R.id.guide_viewpager);  
        mDotsLinearLayout = (LinearLayout) findViewById(R.id.dotsLinearLayout);  
        mStartPagerAdapter = new StartPagerAdapter(mContext);  
        mPageChangeListenerImpl = new PageChangeListenerImpl();  
        mViewPager.setAdapter(mStartPagerAdapter);  
        mViewPager.setOnPageChangeListener(mPageChangeListenerImpl);  
//      try {//通过反射得到对象  
//          Field leftEdgeField = mViewPager.getClass().getDeclaredField("mLeftEdge");  
//          Field rightEdgeField = mViewPager.getClass().getDeclaredField("mRightEdge");  
//          Log.e(TAG, "=======leftEdgeField:" + leftEdgeField + ",rightEdgeField:" + rightEdgeField);//            if (leftEdgeField != null && rightEdgeField != null) {  
//              leftEdgeField.setAccessible(true);  
//              rightEdgeField.setAccessible(true);  
//              leftEdge = (EdgeEffectCompat) leftEdgeField.get(mViewPager);  
//              rightEdge = (EdgeEffectCompat) rightEdgeField.get(mViewPager);  
//              Log.e(TAG, "=======OK啦,leftEdge:" + leftEdge + ",rightEdge:" + rightEdge);  
//          }  
//      } catch (Exception e) {  
//          e.printStackTrace();  
//      }  
  
        initDots();  
    }  
  
    // 初始化小圆点  
    private void initDots() {  
        dotImageViews = new ImageView[mStartPagerAdapter.getCount()];  
        for (int i = 0; i < dotImageViews.length; i++) {  
            LinearLayout layout = new LinearLayout(mContext);  
            ImageView imageView = new ImageView(mContext);  
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20));  
            if (i == 0) {  
                imageView.setBackgroundResource(R.drawable.guide_dot_white);  
            } else {  
                layout.setPadding(20, 0, 0, 0);  
                imageView.setBackgroundResource(R.drawable.guide_dot_black);  
            }  
            dotImageViews[i] = imageView;  
            layout.addView(imageView);  
            mDotsLinearLayout.addView(layout);  
        }  
    }  
  
      
    private class PageChangeListenerImpl implements ViewPager.OnPageChangeListener {  
        @Override  
        public void onPageSelected(int selected) {  
            for (int i = 0; i < dotImageViews.length; i++) {  
                dotImageViews[selected].setBackgroundResource(R.drawable.guide_dot_white);  
                if (selected != i) {  
                    dotImageViews[i].setBackgroundResource(R.drawable.guide_dot_black);  
                }  
            }  
        }  
  
        @Override  
        public void onPageScrolled(int arg0, float arg1, int arg2) {  
//          if (leftEdge != null && rightEdge != null) {  
//              leftEdge.finish();  
//              rightEdge.finish();  
//              leftEdge.setSize(0, 0);  
//              rightEdge.setSize(0, 0);  
//          }  
        }  
  
        /** 
         * 重点!通过输出日志认真理解 onPageScrollStateChanged(int state) 的运用,主要是对state参数的认识 
         */  
        @Override  
        public void onPageScrollStateChanged(int state) {  
            switch (state) {  
            case ViewPager.SCROLL_STATE_DRAGGING://1 dragging(拖动),理解为:只要触发拖动/滑动事件时,则 state = ViewPager.SCROLL_STATE_DRAGGING  
                misScrolled = false;  
                Log.e("onPageScrollStateChanged", "state:"+state+"---------->misScrolled:"+misScrolled+"---------->现在的页码索引:"+mViewPager.getCurrentItem());  
                break;  
            case ViewPager.SCROLL_STATE_SETTLING://2 settling(安放、定居、解决),理解为:通过拖动/滑动,安放到了目标页,则 state = ViewPager.SCROLL_STATE_SETTLING  
                misScrolled = true;  
                Log.e("onPageScrollStateChanged", "state:"+state+"---------->misScrolled:"+misScrolled+"---------->现在的页码索引:"+mViewPager.getCurrentItem());  
                break;  
            case ViewPager.SCROLL_STATE_IDLE://0 idle(空闲,挂空挡), 理解为:只要拖动/滑动结束,无论是否安放到了目标页,则 state = ViewPager.SCROLL_STATE_IDLE  
                if (mViewPager.getCurrentItem() == mViewPager.getAdapter().getCount() - 1 && !misScrolled) {//如果当前页是最后一页,并且滑动,则触发finish()  
                    /*此处可写一些逻辑,如finish() 或 startActivity() 
                    finish();*/  
                    Log.e("在末页向左滑", "state:"+state+"---------->misScrolled:"+misScrolled+"---------->现在的页码索引:"+mViewPager.getCurrentItem());  
                    Toast.makeText(mContext, "已经是最后一页", 2).show();  
                }  
                  
                if (mViewPager.getCurrentItem() == 0 && !misScrolled) {//如果当前页是第一页,并且滑动,则触发finish()  
                    /*此处可写一些逻辑,如finish() 或 startActivity() 
                    finish();*/  
                    Log.e("在首页向右滑", "state:"+state+"---------->misScrolled:"+misScrolled+"---------->现在的页码索引:"+mViewPager.getCurrentItem());  
                    Toast.makeText(mContext, "已经是第一页", 2).show();  
                }  
                misScrolled = true;  
                Log.e("onPageScrollStateChanged", "state:"+state+"---------->misScrolled:"+misScrolled+"---------->现在的页码索引:"+mViewPager.getCurrentItem());  
                break;  
            }  
        }  
  
    }  
  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        if (null != mViewPager) {  
            mViewPager.removeAllViews();  
            mViewPager = null;  
        }  
    }  
  
}  



你可能感兴趣的:(Android-viewpager滑动监听)