簡體   English   中英

Android:根視圖(RelativeLayout)中的onTouchListener無法正常工作

[英]Android: onTouchListener in root view (RelativeLayout) isn't working

我正在開發一個Android應用程序,並且試圖捕獲整個屏幕上的 OnTouch events 我所有的activities都有一個帶有兩個buttons的標題,然后是一個要更改的正文。

在我正在測試的Activity ,主體是ListView ,因此Activity具有:

-屏幕頂部的兩個Buttons

-這兩個按鈕下的ListView

我想在整個屏幕上捕獲onTouchEvents 我嘗試將一個OnTouchListener設置為我的根RelativeLayout ,並為所有其他視圖設置clickable = false和focusable = false,但是它不起作用: onTouch事件僅在我單擊第一個按鈕時觸發

這是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <include
        layout="@layout/header"
        android:clickable="false"
        android:focusable="false" />

    <ListView
        android:id="@+id/lvLocations"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/header_layout"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>

這是@ layout / header的內容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/header_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:weightSum="5" >

    <Button
        android:id="@+id/homeButton"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:layout_weight="4"
        android:clickable="false"
        android:focusable="false"
        android:onClick="Home"
        android:text="@string/home" />

    <ImageButton
        android:id="@+id/speechButton"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:clickable="false"
        android:focusable="false"
        android:onClick="ClickMediaButton"
        android:src="@drawable/micro" />

</LinearLayout>

這是我正在使用的代碼:

    findViewById(R.id.root).setOnTouchListener(new OnTouchListener() {          
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("MACT", "TOUCH!");
        }
    });

就像我說的, 僅當單擊homeButton時 ,我的日志才會顯示TOUCH。 這是為什么? 我究竟做錯了什么?

謝謝!

為什么不只為您的RelativeLayout創建一個自定義視圖來捕獲觸摸事件,然后您就可以用它做任何事情呢?

public class CustomRelativeLayout extends RelativeLayout{

    CustomRelativeLayout(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev){
        //handle touch event
        return true;
    }
}

然后,您可以通過在項目名稱空間中查找該類,在XML中使用這種相對布局。 您當然可以添加任何所需的修飾符,但這是其外觀的一個示例。

<com.example.whatever.your.project.CustomRelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

一旦接觸到觸摸,您就可以隨心所欲。 您可以將其傳遞給其他活動,也可以使用它來調用其他方法,等等。有關如何使用自定義視圖的更多信息, 請參見 android文檔的這一部分。

我終於設法解決了這個問題,並為此做了重新設計。 現在,沒有標題和ListView,而是在布局中只有一個ListView:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后將標題布局添加到我的ListView中:

View header = (View)getLayoutInflater().inflate(R.layout.header,null);
mList.addHeaderView(header);

現在,將onTouchListener設置為ListView,然后在整個屏幕上接收onTouch事件。

我知道這是一個限制性解決方案,它不能用於復雜的布局,但可以在需要的屏幕中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM