【问题标题】:Using onInterceptTouchEvent in a class that extends Fragment在扩展 Fragment 的类中使用 onInterceptTouchEvent
【发布时间】:2015-05-18 10:09:55
【问题描述】:

我有一个包含一些可点击元素(ToggleButtons 和一个 GridView)的视图片段。

我在 View 上设置了一个 onTouchListener,我用它来检测简单的滑动手势,只要滑动不在任何可点击项目上开始,它就可以很好地工作。我希望无论从哪里开始滑动都能正常工作。

我知道 ToggleButtons 和 GridView 可能正在消耗触摸事件,并且在扩展 ViewGroup 的类中,我可以在 Activity 的子类中覆盖 onInterceptTouchEvent 或 dispatchTouchEvent。

任何想法在扩展 Fragment 时如何处理这种情况?

我在这里找到的最接近的是:Android adding onInterceptTouchEvent to a fragment 不幸的是,这个问题没有任何回复。

感谢期待。

【问题讨论】:

  • onInterceptTouchEventViewGroup 的方法,而不是Activity
  • 好的,谢谢。我指的是关于 Activity 的 dispatchTouchEvent。
  • 所以你应该覆盖ViewGroup#onInterceptTouchEvent
  • 我不明白当我的类扩展 Fragment 时我该怎么做。
  • 见 onCreateView,你的类扩展了 Fragment 但它必须以某种方式创建一个根视图

标签: android android-fragments ontouchlistener ontouch


【解决方案1】:

好的,终于到了。

我创建了RelativeLayout 的子类,这是我的布局的根,然后在其中我覆盖了onInterceptTouchEvent

public class RootLayout extends RelativeLayout {

    public RootLayout(Context context) {
        super(context);
    }

    public RootLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RootLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){
        return true;
    }
}

然后用新的子类替换我的xml中的RelativeLayout。

<com.walker.leigh.dipswitch2.RootLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

感谢您的帮助:)

【讨论】:

  • 不完整的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多