2013-10-03 18 views
0

我有一個問題,因爲我無法捕獲GridView中的OnItemClick事件(該項目有一個TextView和WebView)。我在GridView控件正確加載數據,但onItemClick不起作用這裏是我的代碼:Android的OnItemClick事件不適用於GridView白色的WebView和文本

list_item_layout_redes.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/estilocasilla" > 

<com.mhp.desarrollo.laycosandroid.CuadroImagenRed ======THIS IS A CLASS THAT EXTENDS WEBVIEW 
    android:id="@+id/imagen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:scrollbars="none" /> 

<TextView 
    android:id="@+id/texto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:background="#55000000" 
    android:paddingBottom="15dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="15dp" 
    android:textColor="@android:color/white" 
    android:textStyle="bold" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 

activity_redes.xml

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

    ..... 

      <GridView 
       android:id="@+id/gvRedes1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/grisMedio" 
       android:cacheColorHint="#00000000" 
       android:numColumns="2" 
       android:textAlignment="textStart" > 
      </GridView> 
     ... 
</FrameLayout> 

和這部分代碼在活動並已onItemClick部分

if (gvRedes1 != null) { 
      gvRedes1.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> a, View v, int position, 
         long id) 

有人可以幫我嗎?我找不到解決方案。

問候 {

+0

也許是因爲你正在用你的WebView覆蓋整個項目。嘗試在整個視圖中設置一個OnClickListener,而不是爲getView()方法中的每個項目設置 – Carnal

+0

謝謝Carnal,現在我可以看到GridView的項目被選中,但即使onItemClick仍然不起作用。你能幫助嗎?問候 – user2316075

+0

您可以將OnClick而不是OnItemClick設置爲getView()方法中每個充氣項目的每個「視圖」。然後在onClick中,你可以做你通常會在onItemClick中做的事情......因爲你在getView()中有位置,所以你知道你點擊了哪個項目! – Carnal

回答

0

在你Activity創建以下Listener

gvRedes1.setOnGridListener(gridListener); 

public interface GridListener{ 

    public void onItemClicked(int position); 

} 

private GridListener gridListener = new XMPPConnectionListener() { 
    @Override 
    public void onItemClicked(int position) { 

    } 
}; 

然後在你的Activity你這個Listener以這樣的方式像添加到您的適配器

您將需要創建方法setOnGridListener您的適配器將以上創建的偵聽器作爲輸入,並將輸入設置爲您的適配器類中的實例類GridListener。喜歡的東西:

private GridListener gridListener; 
public void setOnGridListener(GridListener listener){ 
    gridListener = listener; 
} 

現在,在您的適配器,當你點擊一個項目在getView()像我建議要麼使用onClick,你可以用這個監聽回調到您的Activity

gridListener.onItemClicked(position); 
相關問題