在扩展片段一类使用onInterceptTouchEvent [英] Using onInterceptTouchEvent in a class that extends Fragment

查看:232
本文介绍了在扩展片段一类使用onInterceptTouchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

视图

我已经设置的查看,我用它来检测简单的滑动手势的onTouchListener,这只要完美的作品还有刷卡不会在任何可点击的项目的启动。我想刷卡不管它从哪里开始工作。

我明白的ToggleButtons和GridView很可能消耗触摸事件,并在其中扩展的ViewGroup我可以在活动的子类覆盖onInterceptTouchEvent或dispatchTouchEvent类。

任何想法我怎么能应对这种情况延长片段时?

我在这里找到的最接近的是:<一href=\"http://stackoverflow.com/questions/25475812/android-adding-onintercepttouchevent-to-a-fragment\">Android加入onInterceptTouchEvent到遗憾的是没有出现过答复这个问题的片段。

谢谢期待。


解决方案

好了,到了那里到底。

我创建的 RelativeLayout的这是我的布局的根,然后在此我重写 onintercepttouchevent

 公共类RootLayout扩展RelativeLayout的{    公共RootLayout(上下文的背景下){
        超级(上下文);
    }    公共RootLayout(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公共RootLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr){
        超(背景下,ATTRS,defStyleAttr);
    }
    @覆盖
    公共布尔onInterceptTouchEvent(MotionEvent EV){
        返回true;
    }
}

然后在我的XML的新的子类取代RelativeLayout的。

 &LT; com.walker.leigh.dipswitch2.RootLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / root_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

感谢您的帮助:)

I have Fragment with a View which contains some clickable elements (ToggleButtons and a GridView).

I have set an onTouchListener on the View which I use to detect simple swipe gestures, this works perfectly well as long as the swipe doesn't start on any of the clickable items. I would like the swipe to work regardless of where it starts.

I understand that the ToggleButtons and GridView are probably consuming the touch event and in classes which extend ViewGroup I could override onInterceptTouchEvent or dispatchTouchEvent in subclasses of Activity.

Any ideas how I could deal with this situation when extending Fragment?

The closest I've found here is: Android adding onInterceptTouchEvent to a fragment Unfortunately there have been no replies to this question.

Thanks in anticipation.

解决方案

Okay, got there in the end.

I create a subclass of which is the root of my layout and then within this I override .

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;
    }
}

Then replace the RelativeLayout in my xml with the new subclass.

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

Thanks for your help :)

这篇关于在扩展片段一类使用onInterceptTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆