首页 > 代码库 > Gallery实现快速拖动只滑动一页的解决办法

Gallery实现快速拖动只滑动一页的解决办法



这年头用gallery的已经很少了,此文提供一个一次滑动只滑动到下一页的方法(包括快速滑动)。


import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class RecommendGallery extends Gallery {
   
    public RecommendGallery(Context context) {
     super(context);
    }

    public RecommendGallery(Context context, AttributeSet attrs) {
     super(context, attrs);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
     // TODO Auto-generated method stub
     int kEvent;
           if(isScrollingLeft(e1, e2)){ //Check if scrolling left
             kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
           }
           else{ //Otherwise scrolling right
             kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
           }
           onKeyDown(kEvent, null);
           return true;
    

    }
    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
           return e2.getX() > e1.getX();
    }
   }