2013-10-03 20 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); 
相关问题