使用OnTouchListener在android中实现滑块结构

时间:2014-11-24 05:20:25

标签: java android

我正在研究一个具有双滑块结构的项目。我使用ontouchlistener引起滑动。有两个滑块,一个是左滑块,另一个是右滑块。滑动手势右滑块突然停止工作。我没有对主类进行更改。当右滑块打开时,调用web服务来填充其中的列表视图。

2 个答案:

答案 0 :(得分:2)

       public class Swipe_Activity extends Activity {
      LinearLayout swipe_layout;
         @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
        swipe_layout=(LinearLayout) findViewById(R.id.layout_swipe);
        swipe_layout.setOnTouchListener( new OnSwipeTouchListener(this));
        }
       public class OnSwipeTouchListener implements OnTouchListener {

       private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 60;
        private static final int SWIPE_VELOCITY_THRESHOLD = 60;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }  
                    result = true;
                } 
             /*   else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }*/
               //     result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
        Log.i("Post", "right");
    }

    public void onSwipeLeft() {
        Log.i("Post", "left");
        Intent intent=new Intent(Swipe_Activity.this,Questions_Answers_Activity.class);
        startActivity(intent);
    }

    public void onSwipeTop() {
        Log.i("Post", "Top");
    }

    public void onSwipeBottom() {
        Log.i("Post", "Bottom");
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }


}

答案 1 :(得分:0)

更改您的代码,

在GestureListener中

@Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                             onSwipeLeft();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }