2014-03-24 65 views
2

我在我的項目中使用抽屜佈局。我想自定義抽屜,就像Google+ Android應用程序一樣。在「主」ListView中,我添加了一個ImageView。這很好。該代碼是:自定義抽屜佈局 - 點擊透明度

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

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

<RelativeLayout 
    android:id="@+id/drawer_layout_relative" 
    android:layout_width="240dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" > 

    <ImageView 
     android:id="@+id/drawer_header_image" 
     android:layout_width="wrap_content" 
     android:layout_height="85dp" 
     android:src="@drawable/ic_logo_blue" /> 

    <ProgressBar 
     android:id="@+id/drawer_progress" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/drawer_header_image" 
     android:background="#ffffffff" /> 

    <ListView 
     android:id="@+id/drawer_listview" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_below="@id/drawer_progress" 
     android:background="#ffffffff" 
     android:choiceMode="singleChoice" /> 
</RelativeLayout> 

當你點擊的ListView項的一個片段加載到FrameLayout裏。這也是完美的。 但有一個問題。 ImageView也是「可點擊」的。所以,當抽屜打開時,一個人會點擊ImageView後面的ListView(在片段上)點擊!我不想那樣。所以我在ImageView上用clickable:false來嘗試它。不要工作...

對於這裏一點試玩視頻:http://www.vidup.de/v/Ao71r/

我怎樣才能讓ImageView的不要點擊?

回答

5

爲了防止點擊通過抽屜傳遞到背後的視圖,必須將抽屜中的視圖的setClickClick設置爲true,特別是您抽屜中所有其他視圖的父視圖,它會消耗所有的觸摸事件,如果它的孩子都沒有消耗它們的話。

更改drawer_layout_relative你的RelativeLayout代碼如下(注:我添加機器人:可點擊=「真」到相對佈局):

<RelativeLayout 
    android:id="@+id/drawer_layout_relative" 
    android:layout_width="240dp" 
    android:clickable="true" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" > 

默認情況下,如果一個視圖不處理任何點擊,它可以讓點擊進入後面的視圖。您有一個ImageView,一個ProgressBar和一個ListView:ListView處理單擊事件,因此它消耗任何接觸它。 ProgressBar和ImageViews默認只顯示內容,不處理點擊,因此係統只是將點擊傳遞給抽屜後面的列表。這三個視圖都在RelativeLayout之內。如果我們將RelativeLayout設置爲可點擊的,它將收集未由ImageView和ProgressBar處理的點擊事件,並且它不會讓點擊事件進入它下面的視圖。

想想android:clickable =「true」作爲阻止與您設置此視圖下的任何視圖進行交互的方式。

有關進一步說明,請參閱this question

+0

謝謝。作品完美。但是,當我們想要「不可點擊」時,使得視圖可點擊的小curois :) – StefMa

+1

如果我們使整個抽屜可點擊,它將保持抽屜下方的視圖看不到任何點擊。我用更多的解釋更新了我的答案,希望它有助於讓事情變得更清晰:) – anthonycr