2014-02-09 111 views
0

我有一個應用程序,有一個列表視圖。我想將imageview放在listview的一部分上。當我爲imageview設置onclick監聽器時,沒有任何反應。我如何使它可以使imageview是可點擊的,而不是與imageview重疊的列表視圖的區域。可點擊的ImageView在列表視圖

的ImageView的 「ID/imagemenu」 應該可以點擊 XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_alignParentTop="true" 
    android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/imagemenu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 

    android:src="@drawable/menub" /> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/header" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/camerab" /> 

</RelativeLayout> 

<ListView 
    android:id="@+id/listView1" 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" > 

</ListView> 

</RelativeLayout> 
+0

你能告訴我們你的xml嗎? – coder

+0

好的,我發佈了我的xml代碼 – Samantha

回答

0

你忘了補充選擇,檢查了這一點。

<ImageButton 
    android:id="@+id/imageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/selector" /> 

對應的選擇文件看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/ico_100_checked" android:state_selected="true"/> 
<item android:drawable="@drawable/ico_100_unchecked"/> 
</selector> 

在我的onCreate我打電話:

final ImageButton ib = (ImageButton) value.findViewById(R.id.imageButton); 

OnClickListener ocl =new OnClickListener() { 
    @Override 
    public void onClick(View button) { 
     if (button.isSelected()){ 
      button.setSelected(false); 
     } else { 
      ib.setSelected(false); 
      //put all the other buttons you might want to disable here... 
      button.setSelected(true); 
     } 
    } 
}; 

ib.setOnClickListener(ocl); 
//add ocl to all the other buttons 

希望這有助於。