在扩展Fragment

时间:2015-05-18 10:09:55

标签: android android-fragments ontouchlistener ontouch

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

我在View上设置了一个onTouchListener,用于检测简单的滑动手势,只要滑动不在任何可点击的项目上启动,这就非常有效。无论它从哪里开始,我都希望滑动工作。

据我所知,ToggleButtons和GridView可能正在使用触摸事件,在扩展ViewGroup的类中,我可以在Activity的子类中覆盖onInterceptTouchEvent或dispatchTouchEvent。

在扩展Fragment时我是如何处理这种情况的?

我在这里找到的最接近的是:Android adding onInterceptTouchEvent to a fragment很遗憾,此问题没有回复。

感谢您的期待。

1 个答案:

答案 0 :(得分:1)

好的,最后到达那里。

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

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">

感谢您的帮助:)

相关问题