我必須完成Activity
,當用戶在屏幕的任何地方提供正確的滑動。我嘗試過GestureDetector
,如果ScrollView
和Activity
中都不存在RescyclerView
,並且此外包含onClickListener
的視圖也不允許檢測它們的滑動,則表示工作正常。因此,我嘗試了一種不同的方式,將視圖以編程方式覆蓋在所有視圖頂部的佈局中,然後嘗試檢測其上的輕掃事件。結束向右滑動活動?
private void swipeOverToExit(ViewGroup rootView) {
OverlayLayout child = new OverlayLayout(this);
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
child.setLayoutParams(layoutParams);
rootView.addView(child);
}
OverlayLayout
public class OverlayLayout extends RelativeLayout {
private float x1, x2;
private final int MIN_DISTANCE = 150;
public OverlayLayout(Context context) {
super(context);
}
public OverlayLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OverlayLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public OverlayLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onTouchEvent will be called and we do the actual
* logic there.
*/
final int action = MotionEventCompat.getActionMasked(event);
Logger.logD("Intercept===", action + "");
// Always handle the case of the touch gesture being complete.
if (action == MotionEvent.ACTION_DOWN) {
return true; // Intercept touch event, let the parent handle swipe
}
Logger.logD("===", "Out side" + action + "");
// In general, we don't want to intercept touch events. They should be
// handled by the child view.
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
Logger.logD("Swipe Right===", MIN_DISTANCE + "");
return true;
} else {
Logger.logD("Tap===", "Tap===");
return super.onTouchEvent(event);
}
}
return true;
}
}
的邏輯是如果滑動操作執行在OverlayLayout
然後進一步落得Activity
到觸摸事件攔截到其他視圖。但是,現在我可以檢測到OverlayLayout
上的滑動事件,但其他視圖無法響應,即使我已返回return super.onTouchEvent(event);
其他條件爲onTouchEvent
,因爲您可以在我的代碼中找到它。任何人都請幫助我做到。我在這裏,超級興奮固定學習的伎倆:)
嘗試添加 @Override public boolean dispatchTouchEvent(MotionEvent ev){ super.dispatchTouchEvent(ev); return productGestureDetector.onTouchEvent(ev); }到您的活動 –
@Stella您是否在活動中嘗試過dispatchTouchEvent? – Nisarg
嘗試搜索以任何佈局控制手勢的'touchIntercept'方法。 –