【问题标题】:Android OnItemClick Event does not work in GridView whit WebView and TextAndroid OnItemClick 事件在带有 WebView 和文本的 GridView 中不起作用
【发布时间】:2013-10-03 07:44:13
【问题描述】:

我有一个问题,因为我无法在 GridView 中捕获 OnItemClick 事件(该项目具有 TextView 和 WebView)。我在 GridView 中正确加载数据,但 onItemClick 不起作用这是我的代码:

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>

这部分代码在Activity中,有onItemClick部分

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

有人可以帮我吗?我没有找到解决方案。

最好的问候 {

【问题讨论】:

  • 可能是因为您正在使用 WebView 覆盖整个项目。尝试在整个视图上设置 OnClickListener,而不是为 getView() 方法中的每个项目
  • 感谢Carnal,现在我可以看到GridView 的项目被选中了,但是即使onItemClick 仍然不起作用。你能帮忙吗?问候
  • 您可以在 getView() 方法中为每个膨胀项目设置 OnClick 而不是 OnItemClick 到每个“视图”。然后在 onClick 中,您可以执行通常在 onItemClick 中执行的操作...因为您在 getView() 中有位置,所以您知道您点击了哪个项目!
  • 感谢 Carnal,我理解您的想法,但我想在另一个 Activity 中处理该位置,而不是在适配器中。我知道可以在 Activity 中定义静态方法,但这对 mi 应用程序不利。有什么建议吗?
  • 我不太明白你的意思。处理另一个Activity中的位置?你能再解释一下吗?

标签: android gridview webview onitemclick


【解决方案1】:

在您的Activity 中创建以下Listener

public interface GridListener{

    public void onItemClicked(int position);

}

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

    }
};

然后在您的 Activity 中,您将这个 Listener 添加到您的适配器中,如下所示:

gvRedes1.setOnGridListener(gridListener);

您需要在您的适配器中创建方法setOnGridListener,它将上面创建的这个侦听器作为输入,并将输入设置为您在适配器类中拥有的实例类GridListener。类似的东西:

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

现在在您的适配器中,当您像我建议的那样使用getView() 中的onClick 单击项目时,您可以使用此侦听器向您的Activity 进行回调:

gridListener.onItemClicked(position);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多