2012-06-21 115 views
0

我使用item.xml創建了一個GridView,其中包含LinearLayout中的圖像。Android GridView ItemClikListneter issue

的GridView代碼:

<GridView 
      android:id="@+id/gridView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true" 
      android:columnWidth="150dip" 
      android:gravity="center" 
      android:horizontalSpacing="10dip" 
      android:numColumns="auto_fit" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="15dip" > 
     </GridView> 

item.xml代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/itemLayout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/selector" 
android:clickable="true" 
android:gravity="center_horizontal" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/itemIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:scaleType="matrix" 
    > 
</ImageView> 

然後,我添加selector.xml爲LinearLayout背景避免顯示選定的區域上時點擊項目。

selector.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 

<item android:drawable="@android:color/transparent" android:state_enabled="true" android:state_pressed="true"/> 
<item android:drawable="@android:color/transparent" android:state_enabled="true" android:state_focused="true"/> 
<item android:drawable="@android:color/transparent" android:state_enabled="true"/> 

添加android:clickable="true"到item.xml LinearLayout,後GridViewItemClickListner不work.Otherwise它的工作原理,但並不適用selector.xml效果到LinearLayout。我想爲應用selector.xml效果和作品ItemClickListner也。

請幫我解決這個問題。謝謝。

回答

0

只需將屬性android:focussable=false添加到item.xml中的ImageView即可。這應該做的伎倆。

<ImageView 
    android:id="@+id/itemIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:focussable=false 
    android:scaleType="matrix" 
    > 
</ImageView> 

既然你要分配點擊真到這個ImageView的,它會在默認情況下的焦點,因此不允許被點擊你的GridView的項目。因此將其設置爲不可聚焦狀態將幫助您實現兩個點擊事件。

0

移除機器人:可點擊從item.xml佈局的線性佈局和imageview的=「真」,並添加在此imageview的3張操作數

android:focusable="false" 
android:clickable="false" 
android:focusableInTouchMode="false" 

之後,使一個xml在抽拉background.xml與後這樣的事情參數

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@color/focusedcolor" 
    <item android:state_pressed="true" android:drawable="@color/selectedcolor" /> 
    <item android:drawable="@color/normalcolor" /> 
</selector> 

添加在您的item.xml線性佈局

android:background="@drawable/background" 

在我的代碼,我colors.xml文件夾類似

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="focusedcolor">#b6c4c5</color> 
<color name="selectedcolor">#ff33ffff</color> 
<color name="normalcolor">#f7ff1f</color> 
</resources> 

,而不是顏色U還可以,如果ü要使用圖像從繪製文件夾中的圖像比背景。xml你必須改變

android:drawable="@color/focusedcolor" line by 

android:drawable="@drawable/imagename" 
+0

什麼是與android的問題:drawable =「@ android:color/transparent」background.xml(作爲我的代碼selector.xml)? – prs

+0

selector.xml效果不適用,因爲android:focusable =「false」 android:clickable =「false」 android:focusableInTouchMode =「false」。而且我不想爲佈局背景應用顏色。 – prs

+0

@it對我有用,因爲我已經用於listview的行xml你必須設置背景到item.xml的LinearLayout,因爲我已經寫了你可以改變顏色,如果你想要透明的顏色,因爲你已經在你的selector.xml中使用檢查它 – Khan