ViewPager 滑动速度设置,并实现点击按钮滑动

ViewPager 滑动速度设置,并实现点击按钮滑动

 

     使用过ViewPager的童鞋,都会感觉到设置界面滑动挺简单的。但是有时候却满足不了UI设计的要求。

     在用这个ViewPager的时候我遇到两个问题,不知道你们遇到没有。这里做个笔记,总结一下:

     第一个问题是,ViewPager在我们滑动放手后,速度和动画的变化率是固定的。

     第二个问题的,我们再添加左右按钮后,如点击滑动到前一页面(通过mViewPager.setCurrentItem(viewID, true);),一闪就了,用户感觉不到动画效果。

     其实这两个问题的的根源都是一样的。我们能都改变速度和动画的变化率,那么就可以解决了。

 

还是写个简单的demo,先看效果图:

               

 

右边这张是滑动中的。滑动放手后,动画的变化率设为加速度,也就是先慢后快。这里改变了Interpolator。

每按下向左键,滑动速度会快0.1秒。每按下向右键,滑动速度会慢0.1秒。 所以你不停的按向右键,那么将会变得很慢很慢。

 

布局main.xml:

 




    

    
       


ViewPagerDemoActivity类:

package blog.csdn.net.liyulei316686082;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;

public class ViewPagerDemoActivity extends Activity {
    /** Called when the activity is first created. */
 
 private ViewPager mViewPager = null;
 private Button mLeft = null;
 private Button mRight = null;
 
 private static final int PAGER_NUM = 10;//10个页面
 private int mCurrentViewID = 0;         //当前页面
 private int mMyDuration = 100;          //持续时间
 
 private FixedSpeedScroller mScroller;
 private YLeiPageAdapter mYLeiPageAdapter = null;
 private List mListViews;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mViewPager = (ViewPager)findViewById(R.id.viewpager);
        mLeft = (Button)findViewById(R.id.left); 
        mRight = (Button)findViewById(R.id.right); 
        mLeft.setOnClickListener(mOnClickListener);
  mRight.setOnClickListener(mOnClickListener);
  
  mListViews = new ArrayList();
  for(int i= 1; i<= PAGER_NUM; i++){
   MyPagerView view = new MyPagerView(this, i);
   mListViews.add(view);
  }
  mYLeiPageAdapter = new YLeiPageAdapter(this, mListViews);
  mViewPager.setAdapter(mYLeiPageAdapter);
  mViewPager.setOnPageChangeListener(mOnPageChangeListener);
  
  /*主要代码段*/
  try {             
   Field mField = ViewPager.class.getDeclaredField("mScroller");             
   mField.setAccessible(true);   
    //设置加速度 ,通过改变FixedSpeedScroller这个类中的mDuration来改变动画时间(如mScroller.setmDuration(mMyDuration);) 
   mScroller = new FixedSpeedScroller(mViewPager.getContext(), new AccelerateInterpolator());        
   mField.set(mViewPager, mScroller);         
   } catch (Exception e) {         
    e.printStackTrace();
   } 
    }
    
    private OnClickListener mOnClickListener = new OnClickListener(){
  @Override
  public void onClick(View v) {
   switch (v.getId()){
   case R.id.left:
    if(mCurrentViewID != 0){
     mCurrentViewID--;
     mViewPager.setCurrentItem(mCurrentViewID, true);
    }
    mMyDuration -= 100;
    mScroller.setmDuration(mMyDuration);
    break;
   case R.id.right:
    if(mCurrentViewID != PAGER_NUM-1){
     mCurrentViewID++;
     mViewPager.setCurrentItem(mCurrentViewID, true);
    }
    mMyDuration += 100;
    mScroller.setmDuration(mMyDuration);
    break;
   }
   
  }};
  
  private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener(){

   @Override
   public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub
   }

   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
   }
   @Override
   public void onPageSelected(int currentID) {
    // TODO Auto-generated method stub
    mCurrentViewID = currentID;
   }};
}


 

 

FixedSpeedScroller类:

package blog.csdn.net.liyulei316686082;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class FixedSpeedScroller extends Scroller {      
	private int mDuration = 1500;      
	public FixedSpeedScroller(Context context) {         
		super(context);     
		}      
	public FixedSpeedScroller(Context context, Interpolator interpolator) {         
		super(context, interpolator);     
		}      
	     
	@Override    
	public void startScroll(int startX, int startY, int dx, int dy, int duration) {    
		// Ignore received duration, use fixed one instead         
		super.startScroll(startX, startY, dx, dy, mDuration);     
		}      
	@Override     
	public void startScroll(int startX, int startY, int dx, int dy) {        
			// Ignore received duration, use fixed one instead        
			super.startScroll(startX, startY, dx, dy, mDuration);    
			} 
	public void setmDuration(int time){
		mDuration = time;
	}	
	public int getmDuration(){
		return mDuration;
	}	

}


 http://blog.csdn.net/liyulei316686082/article/details/7616734


你可能感兴趣的:(android应用)