将触摸侦听器设置为根视图而不覆盖子视图的触摸事件?

时间:2013-04-24 12:20:21

标签: android android-fragments android-view android-listfragment

我的Activity包含一个Framelayout(名为fragment_content),我在其中替换/添加不同的Fragments。 现在我将TouchListener设置为此fragment_content以实现“双指滑动”( - >仅用两根手指滑动动作)。

我遇到的问题是,一旦我的Fragment从ListFragment扩展,fragment_content的TouchListener将被忽略。 我知道我可以在listview中设置一个touchlistener,但我必须实现listview设置( - > scrolling,listItemClick)。

有没有办法将Touchlistener设置为我的根视图( - > FrameLayout)而不会覆盖我的子视图的设置( - > ListFragment)?

修改

我编写了一个从FrameLayout扩展的类MyTouchController。在这个类中,我重写了onInterceptTouch()方法。

在我的Activity中,contentView是main.xml,我初始化Framelayout:

FrameLayout v = (FrameLayout) findViewById(R.id.main_fragment);

现在,我遇到以下问题:如何覆盖此main_fragment的onInterceptTouchMethod( - > FrameLayout)?

MyTouchController v = (MyTouchController) findViewById(R.id.main_fragment);

抛出一个ClassCastEsception:android.widget.FrameLayout无法强制转换为MyMenuController(即使我在xml布局中声明了MyTouchController)。

main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <FrameLayout 
        android:id="@+id/main_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</FrameLayout>

编辑2(解决方案)

main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <"pkg-name".MyTouchController
        android:id="@+id/main_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <FrameLayout 
        android:id="@+id/main_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</FrameLayout>

在代码中夸大布局:

MyTouchController v = (MyTouchController) findViewById(R.id.main_fragment);

0 个答案:

没有答案