ViewPager 中 HorizontalScrollView 的动作
Actions of HorizontalScrollView inside ViewPager
我需要将 HorizontalScrollView 放在 ViewPager 中。 ViewPager的每个child都是ScrollView,里面有HorizontalScrollView。
我将 HorizontalScrollView 缩放 child。之后我需要允许用户滚动 HorizontalScrollView 但不能滚动到 ViewPager 的下一页。为了处理操作,我创建了自定义 ViewPager:
public class MyViewPager extends ViewPager {
private static final String TAG = MyViewPager.class.getName();
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
final int action = MotionEventCompat.getActionMasked(ev);
// Always handle the case of the touch gesture being complete.
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
return super.onInterceptTouchEvent(ev); // Do not intercept touch event, let the child handle it
}
switch (action) {
case MotionEvent.ACTION_MOVE: {
ViewGroup view=(ViewGroup)(getChildAt(getCurrentItem()));
if (view.getChildCount()>0) {
view = (ViewGroup) view.getChildAt(0);
if (view instanceof CustomHorizontalScroll) {
CustomHorizontalScroll scroll = (CustomHorizontalScroll) view;
if (((CustomHorizontalScroll) view).isZoom()) {
return false;
} else {
return true;
}
}
}
break;
}
default: return super.onInterceptTouchEvent(ev);
}
}catch (Exception e){
return super.onInterceptTouchEvent(ev);
}
return super.onInterceptTouchEvent(ev);
}
}
问题出在 onTouchAction 上。在我的水平视图中有 onClick 事件。如果我使用自定义 ViewPager,则有时 onClick 不起作用。我需要做什么?这种情况下如何使用ActionDown?
实施 onTouch
和 onClick
将消耗 onTouch
的触摸事件并认为它已消耗,而不是将其传递给其他各种触摸处理程序,这使您的 [=14= 】 没用。
文档:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
以下是处理这种情况的一些可用解决方案:
1-您可以使用 onToach
和 onClick
作为内部 类 而不是实现每个侦听器。
2- 您可以使用 onToach
来检测标签事件:
if (gestureDetector.onTouchEvent(arg)) {
// single tap
return true;
} else {
// action up or down code
}
我需要将 HorizontalScrollView 放在 ViewPager 中。 ViewPager的每个child都是ScrollView,里面有HorizontalScrollView。 我将 HorizontalScrollView 缩放 child。之后我需要允许用户滚动 HorizontalScrollView 但不能滚动到 ViewPager 的下一页。为了处理操作,我创建了自定义 ViewPager:
public class MyViewPager extends ViewPager {
private static final String TAG = MyViewPager.class.getName();
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
final int action = MotionEventCompat.getActionMasked(ev);
// Always handle the case of the touch gesture being complete.
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
return super.onInterceptTouchEvent(ev); // Do not intercept touch event, let the child handle it
}
switch (action) {
case MotionEvent.ACTION_MOVE: {
ViewGroup view=(ViewGroup)(getChildAt(getCurrentItem()));
if (view.getChildCount()>0) {
view = (ViewGroup) view.getChildAt(0);
if (view instanceof CustomHorizontalScroll) {
CustomHorizontalScroll scroll = (CustomHorizontalScroll) view;
if (((CustomHorizontalScroll) view).isZoom()) {
return false;
} else {
return true;
}
}
}
break;
}
default: return super.onInterceptTouchEvent(ev);
}
}catch (Exception e){
return super.onInterceptTouchEvent(ev);
}
return super.onInterceptTouchEvent(ev);
}
}
问题出在 onTouchAction 上。在我的水平视图中有 onClick 事件。如果我使用自定义 ViewPager,则有时 onClick 不起作用。我需要做什么?这种情况下如何使用ActionDown?
实施 onTouch
和 onClick
将消耗 onTouch
的触摸事件并认为它已消耗,而不是将其传递给其他各种触摸处理程序,这使您的 [=14= 】 没用。
文档:
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
以下是处理这种情况的一些可用解决方案:
1-您可以使用 onToach
和 onClick
作为内部 类 而不是实现每个侦听器。
2- 您可以使用 onToach
来检测标签事件:
if (gestureDetector.onTouchEvent(arg)) {
// single tap
return true;
} else {
// action up or down code
}