Java android在relativeLayout中设置了滑动监听器

时间:2017-03-09 14:45:14

标签: java android android-layout

我想创建这个:当我向上滑动(RelativeLayout)时,我将RelativeLayout的大小调整为wrap_content,当我向下滑动RelativeLayout时,我调整为150dp。我这样做了:

import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {    
    private final GestureDetector gestureDetector;    
    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }   
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }    
    private final class GestureListener extends SimpleOnGestureListener {    
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;    
        @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();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    public void onSwipeRight() {
    }    
    public void onSwipeLeft() {
    }    
    public void onSwipeTop() {
    }
    public void onSwipeBottom() {
    }
}

和此:

 relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
            relativeLayout1 = (RelativeLayout) findViewById(R.id.dolny_pasek);
            final ViewGroup.LayoutParams params = relativeLayout1.getLayoutParams();

  relativeLayout1.setOnTouchListener(new OnSwipeTouchListener(this){
            public void onSwipeTop() {
                Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
            }
            public void onSwipeRight() {
                Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
            }
            public void onSwipeLeft() {
                Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
            }
            public void onSwipeBottom() {
                Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
            }
        });

在xml布局中我有:

<RelativeLayout
        android:id="@+id/dolny_pasek"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="#e2e2e2"
       >

但是当我向下滑动,向上,向左或向右没有任何反应时,我没有看到吐司我不知道我做错了什么

0 个答案:

没有答案