我正在尝试在RelativeLayout中向左或向右滑动。我写了一些代码,但无法向左或向右滑动。我正在使用GestureDetector 这是我的来源
private GestureDetector gesturedetector = null;
private RelativeLayout swipelayout;
@SuppressLint("UseValueOf")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.strada_menu_result_loadmore,
container, false);
swipelayout = (RelativeLayout) rootView.findViewById(R.id.swipelayout);
gesturedetector = new GestureDetector(new MyGestureListener());
swipelayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gesturedetector.onTouchEvent(event);
return true;
}
});
return rootView;
}
public boolean dispatchTouchEvent(MotionEvent ev){
return gesturedetector.onTouchEvent(ev);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
if (Math.abs(dY) >= SWIPE_THRESHOLD_VELOCITY
&& Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY &&
Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
if (dX > 0) {
Toast.makeText(getActivity(), "Right Swipe",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Left Swipe",
Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}
我在做错了什么?如果有人知道解决方案,请帮助我
感谢
答案 0 :(得分:6)
你只需要一个OnTouchListener,这应该是简单的答案。看一看。 我在一些工作项目中使用了这个代码,所以一切都应该正常。但请告诉我是否有遗漏并且无效。
yourRelativeLayout.setOnTouchListener(new OnTouchListener() {
int downX, upX;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX();
Log.i("event.getX()", " downX " + downX);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX();
Log.i("event.getX()", " upX " + upX);
if (upX - downX > 100) {
// swipe right
}
else if (downX - upX > -100) {
// swipe left
}
return true;
}
return false;
}
});
答案 1 :(得分:1)
试试这会对你有帮助....
OnSwipeTouchListener.java :
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());
}
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() {
}
}
用法:
imageView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});