Set TouchListener to root view without overriding touchevents of child view?

廉价感情. 提交于 2019-12-04 06:01:35

问题


My Activity consists of one Framelayout (named fragment_content) where I replace/add different Fragments. Now I set a TouchListener to this fragment_content to implement a "Two-Finger-Swipe" (-> just a swipe motion with two fingers).

The Problem I have is that as soon my Fragment extends from a ListFragment the TouchListener of fragment_content will be ignored. I know I can set a touchlistener to the listview, but than I have to implement the listview settings (-> scrolling, listItemClick).

Is there a way to set a Touchlistener to my root view (-> FrameLayout) without overriding the settings of my child view (-> ListFragment)?

EDIT

I wrote a class MyTouchController that extends from FrameLayout. In this class I override onInterceptTouch() Method.

In my Activity that contentView is main.xml, I init the Framelayout:

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

Now, I have the following Problem: how to override the onInterceptTouchMethod of this main_fragment (-> FrameLayout)?

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

is throwing a ClassCastEsception: android.widget.FrameLayout cannot be cast to MyMenuController (even if I declare MyTouchController in xml layout).

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>

EDIT 2 (solution)

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>

inflating the layout in code:

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

来源:https://stackoverflow.com/questions/16191914/set-touchlistener-to-root-view-without-overriding-touchevents-of-child-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!