我正在关注Google开发者文档:
https://developer.android.com/training/gestures/viewgroup.html
我正在做与其示例代码完全相同的情况,其中我在父母上有滑动手势,但如果我不滑动,那么我将处理对孩子的点击而不是拦截父母。 然而,目前的流程是这样的:
Action.DOWN - > Hits Parent's onInterceptTouchEvent I return false since I am not waiting for this case
Action.DOWN - > Hits Child's onTouch Handler, I return false since I still want to possibly use the parents code
Action.DOWN -> Hits Parents' onTouch Handler, I return true because i want to get the rest of the notifications for the gesture and then pass on to the child when appropriate. If I return false at this point, then the event will die and I wont get Action.UP
在这一点上我完全被降级为父母的onTouchListener
并且感觉归于那里,因为有条件我想回到孩子的{{1} onTouch
1}}处理程序。特别令人困惑的是Android文档中的以下规定:
down事件将由该视图组的子进程处理,或者由您自己的onTouchEvent()方法处理;这意味着你应该实现onTouchEvent()以返回true,这样你将继续看到手势的其余部分(而不是寻找父视图来处理它)。此外,通过从onTouchEvent()返回true,您将不会在onInterceptTouchEvent()中收到任何后续事件,并且所有触摸处理必须像on normal一样在onTouchEvent()中进行。 只要你从这个函数返回false,每个后续事件(包括最后一个)将首先在这里传递给目标的onTouchEvent()。
所以我的愿望是至少在父母的Action.MOVE
内onInterceptTouchEvent
之前,以便我可以确定这个手势是否是孩子点击/触摸或父母的刷卡。我似乎只能在父母ACTION_UP
中找到onTouchEvent
,因为我在true
返回Action.DOWN
,正如文档所述,我完全将我降级为我的onTouch
父母onInterceptTouchEvent
方法。
我怎样才能保持onTouchEvent
旋转,同时还要使true
永远不会返回onTouch
,直到我希望父母代替孩子做某事为止?
这是我的逻辑思路,但是当我这样做时,我必须让我的父母false
返回ActionDown
onTouch
,然后从{ onInterceptTouchEvent
和 binding.linLayoutWrapper.setOnTouchListener(new View.OnTouchListener() {
int downX, moveX, upX;
int downY, moveY;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//binding.linLayoutWrapper.getParent().requestDisallowInterceptTouchEvent(true);
downX = (int) motionEvent.getX();
downY = (int) motionEvent.getY();
return false;
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
return false;
} else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
moveY = (int) motionEvent.getY();
moveX = (int) motionEvent.getX();
if (Math.abs(downX - moveX) > Math.abs(downY - moveY)){
upX = (int) motionEvent.getX();
if (upX - downX > 100) {
// swipe right
CalendarDay cal = binding.calendarView.getCurrentDate();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(cal.getDate());
cal1.add(Calendar.WEEK_OF_YEAR, -1);
binding.calendarView.setCurrentDate(cal1.getTime());
binding.linLayoutWrapper.getParent().requestDisallowInterceptTouchEvent(true);
return true;
} else if (downX - upX > 100) {
CalendarDay cal = binding.calendarView.getCurrentDate();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(cal.getDate());
cal1.add(Calendar.WEEK_OF_YEAR, 1);
binding.calendarView.setCurrentDate(cal1.getTime());
binding.linLayoutWrapper.getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
} else {
binding.linLayoutWrapper.getParent().requestDisallowInterceptTouchEvent(false);
}
}
return false;
}
});
范例。
触控清单上的家长:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
Log.d("HELP", " in parent action up");
mIsScrolling = false;
return false;
}
switch (action) {
case MotionEvent.ACTION_MOVE: {
if (mIsScrolling) {
return true;
}
final int xDiff = calculateDistanceX(ev);
if (xDiff > mTouchSlop) {
// Start scrolling!
mIsScrolling = true;
return true;
}
break;
}
case MotionEvent.ACTION_DOWN: {
xNot = ev.getX();
return false;
}
}
return false;
}
父母onInterceptTouchListener:
relativeLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent(getContext(), EditAvailabilityActivity.class);
if (event != null) {
int nurseId = AccountManager.sharedManager().getCurrentAccount().getId();
Conflict conflict = new Conflict();
conflict.setNurseId(nurseId);
conflict.setId(event.getConflictId());
conflict.setEndDate(event.getParentEnd());
conflict.setStartDate(event.getParentStart());
conflict.setStartTime(event.getStartTime());
conflict.setEndTime(event.getEndTime());
conflict.setIsAllDay(event.getAllDay() == 1);
intent.putExtra(EditAvailabilityActivity.EXTRA_CONFLICT, Parcels.wrap(conflict));
}
intent.putExtra(EditAvailabilityActivity.EXTRA_MODE, true);
((Activity) getContext()).startActivityForResult(intent, EDIT_AVAILABILITY_REQUEST_CODE);
return true;
}
return false;
}
});
最后我的孩子的onTouch听众:
onTouchListener
更新:
如果我从孩子onInterceptTouchEvent
返回true,则会转到父母的onTouchListener
。我不确定当他们说'#34; 此外,通过从onTouchEvent()返回true,您将不会在onInterceptTouchEvent()中收到任何后续事件,并且所有触摸处理必须在onTouchEvent()中正常发生。"这似乎是一个明目张胆的矛盾,但也许它们意味着当你从父母的onTouchListener"那是准确的吗?
当我将孩子 relativeLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent(getContext(), EditAvailabilityActivity.class);
if (event != null) {
int nurseId = AccountManager.sharedManager().getCurrentAccount().getId();
Conflict conflict = new Conflict();
conflict.setNurseId(nurseId);
conflict.setId(event.getConflictId());
conflict.setEndDate(event.getParentEnd());
conflict.setStartDate(event.getParentStart());
conflict.setStartTime(event.getStartTime());
conflict.setEndTime(event.getEndTime());
conflict.setIsAllDay(event.getAllDay() == 1);
intent.putExtra(EditAvailabilityActivity.EXTRA_CONFLICT, Parcels.wrap(conflict));
}
intent.putExtra(EditAvailabilityActivity.EXTRA_MODE, true);
((Activity) getContext()).startActivityForResult(intent, EDIT_AVAILABILITY_REQUEST_CODE);
return true;
}
return true; //this makes it behave strangely
}
});
改为此时:
oninterceptTouchEvent
它有效地处理了点击,但它似乎以一种奇怪的方式这样做。它通过Action_UP
一直回到父onTouch
{i}}来违反Android文档。这看起来很有希望,因为如果我能够在手势上走得那么远,并且仍然可以从目标的 Action.DOWN - > Parent's onInterceptTouchEvent
Action.Down -> Hits Child's OnTouchListener which is returning "true"
Action.Cancel -> Hits Parent's onInterceptTouchEvent
方法中获得正确的结果,那么我会得到我想要的东西。可悲的是,在这种情况下,当我尝试滑动手势时,我只能在父母和孩子之间得到以下的rapartee:
// component.vue
created () {
this.$store.dispatch('getGithubAPI')
}
// store action
async getGithubAPI ({ commit, state }) {
await this.$axios.$get('https://api.github.com/users/kaungmyatlwin', { headers: { 'Access-Control-Allow-Origin': '*' } })
.then(resp => {
console.log(resp.data)
})
}
这是预期的流程,因此一厢情愿地认为它有效,但这与我注意到的早期矛盾不符。