2013-08-19 97 views
0

我有一個非常簡單的佈局 - 它有兩個視圖:列表視圖和完全覆蓋的「覆蓋」視圖;下面是一個例子:忽略覆蓋視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/listView"/> 

    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

     <ImageView 
       android:layout_width="360dp" 
       android:layout_height="270dp" 
       android:id="@+id/imageView" 
       android:layout_centerInParent="true" 
       android:background="#8888"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="This is some text" 
       android:id="@+id/button" 
       android:layout_below="@+id/imageView" 
       android:layout_centerHorizontal="true"/> 
    </RelativeLayout> 
</RelativeLayout> 

我做了疊加觀看比雷真的是,沒有任何視圖更簡單一點說應該希望有一個觸摸(即,沒有按鈕或滾動區) - 一些LinearLayout的散亂)。我想要做的是不僅忽略佈局中的觸摸,而且忽略佈局的所有子視圖。我想要接觸的唯一視圖是ListView。

我已經以最簡單的方式實現它 - 非常類似於上面 - 並且觸摸永遠不會到達底層ListView(它在拖動時不會滾動)。

我真的沒有第一個線索如何爲Android做到這一點。另一個SO回答(https://stackoverflow.com/a/9462091/875486)指向使用某些標誌(特別是FLAG_NOT_TOUCHABLE和FLAG_NOT_FOCUSABLE),但我無法完全弄清楚如何使用這些標誌。

回答

3

爲什麼不在xml中爲你的RelativeLayout分配一個id,並給它一個OnTouchListener以便在代碼中不做任何事情。這將覆蓋沒有偵聽器的基礎視圖。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <ListView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/listView"/> 

    <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

     <ImageView 
       android:layout_width="360dp" 
       android:layout_height="270dp" 
       android:id="@+id/imageView" 
       android:layout_centerInParent="true" 
       android:background="#8888"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="This is some text" 
       android:id="@+id/button" 
       android:layout_below="@+id/imageView" 
       android:layout_centerHorizontal="true"/> 
    </RelativeLayout> 
</RelativeLayout> 

...

RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.layout); 
mLayout.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // Intercept touch events so they are not passed to underlying views. 
      return true; 
     } 
    }); 

ListView mListView = (ListView) findViewById(R.id.listView); 
mListView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      //Do stuff 
     } 
    }); 
+0

我看到如何將忽略的RelativeLayout和子視圖的觸摸,但將ListView控件,然後讓那些觸動?這是最終的目標。 我可以給它一個快速測試,我認爲... –

+0

ListView仍然會收到觸動。 – Nick

+0

謝謝。當底層視圖是一個ListView時,你還需要用'setOnDragListener()'來設置一個返回false的拖拽監聽器。 –