ViewFlipper + scrollView 左右滑动

来源:互联网 发布:eia数据公布 时间 编辑:程序博客网 时间:2024/05/17 06:27

(写的有点乱,,,有些地方 模糊,没有基础 勿看)

  实现 在屏幕上滑动 三个界面 切换

1 xml

  

    <ViewFlipper        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:id="@+id/viewflipper"        android:layout_weight="1"        >        <!-- 第一个页面 --><include  layout="@layout/gallery_item"/><!-- 第二个页面 --><include  layout="@layout/gallery_item2"/><!-- 第三个页面 --><include  layout="@layout/third_linearlayout"/>    </ViewFlipper>
三个界面都是很普通的 界面,里面包含了 ScrollView 而已,在每个界面上 可以上下滑动,我们要实现的是 左右滑动 也可以实现 viewFlipper左右效果(默认是左右滑动不了的,被 ScrollView“吃”掉了)

2 直接上代码了

public class MyS extends ScrollView {private GestureDetector gd = null;private Context context = null;public MyS(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;// TODO Auto-generated constructor stub}@Overridepublic boolean onTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubgd.onTouchEvent(ev);return super.onTouchEvent(ev);}public void setGestureDetector(GestureDetector.OnGestureListener gestureDetector) {this.gd = new GestureDetector(context, gestureDetector);}}
                  sc_1 = (MyS) findViewById(R.id.sc_1);  sc_2 = (MyS) findViewById(R.id.sc_2);  sc_1.setGestureDetector(this);  sc_2.setGestureDetector(this);

这样就可以了,写的很简单,但 基本上就是这样了,滑动手势 处理代码主要是在 下面(主要的代码,有用的没用的)

public class ViewflipperTestActivity extends Activity implements OnTouchListener,OnGestureListener,OnClickListener{  private ViewFlipper mFlipper;          private   GestureDetector mGestureDetector;        private static final int FLING_MIN_DISTANCE = 100;        private static final int FLING_MIN_VELOCITY = 200;        //得到之前的左边与 屏幕左边缘的距离  private int from_left = 0;  private int to_left = 0;    private TextView curr_view = null;    private boolean isFirst = true;  ImageView iv_bg = null;    private MyS sc_1 = null;  private MyS sc_2 = null;  @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            initElements();  }  private void initElements(){  sc_1 = (MyS) findViewById(R.id.sc_1);  sc_2 = (MyS) findViewById(R.id.sc_2);  sc_1.setGestureDetector(this);  sc_2.setGestureDetector(this);  mFlipper = (ViewFlipper) findViewById(R.id.viewflipper);  //注册一个用于手势识别的类            mGestureDetector = new GestureDetector(this);//给mFlipper设置一个listener            mFlipper.setOnTouchListener(this);            //允许长按住ViewFlipper,这样才能识别拖动等手势           mFlipper.setLongClickable(true);     }@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {                // 当像左侧滑动的时候               //设置View进入屏幕时候使用的动画    //或者直接:mFlipper.setInAnimation(getApplicationContext(), R.anim.in_left); add by 午夜凶林  next();  } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {         // 当像右侧滑动的时候                previous();  }            return false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onTouch(View v, MotionEvent event) {return mGestureDetector.onTouchEvent(event);}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub }@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.textView_1:move(textView_1);mFlipper.setDisplayedChild(0);break;case R.id.textView_2:move(textView_2);mFlipper.setDisplayedChild(1);break;case R.id.textView_3:move(textView_3);mFlipper.setDisplayedChild(2);break;default:break;} } @Overridepublic void onDetachedFromWindow() {try {super.onDetachedFromWindow();}catch (IllegalArgumentException e) {mFlipper.stopFlipping();}}private void next(){mFlipper.setInAnimation(AnimationUtil.inFromRightAnimation(getApplicationContext()));                //设置View退出屏幕时候使用的动画                mFlipper.setOutAnimation(AnimationUtil.outToLeftAnimation(getApplicationContext()));      mFlipper.showNext();          }private void previous(){ mFlipper.setInAnimation(AnimationUtil.inFromLeftAnimation(getApplicationContext()));                mFlipper.setOutAnimation(AnimationUtil.outToRightAnimation(getApplicationContext()));                mFlipper.showPrevious();         }


ok,这下 全了,其实 其他的 如 listView 等 吃掉左右滑动 也可以这么处理的

原创粉丝点击