android - convertView在listView滚动后失去onitemClick

标签 android android-adapter android-adapterview

我的适配器出现了一个稍微奇怪的错误。 适配器正在创建的 View 是左侧的缩略图和包含几行的表格。每行有两个 TextView 。

其中一个 TextView 设置了 android:autoLink="web" 属性, ListView 上有一个 onItemClickListener。

问题是,每次 TextView 自动链接它的内容时,下次它的父 View 被转换时,它不再从 onItemClickListener 接收点击。

让我用一个例子来说明:

  • view1、view2、view3 和 view4 在屏幕上的 ListView 上。
  • view2 有一个链接,它会出现,然后单击链接打开。
  • 项目点击对 View 1、 View 3 和 View 4 正常工作。
  • 滚动 ListView ,将 View 1 转换为位置 5,然后将 View 2 转换为位置 6。
  • 位于 position6 的项目不包含链接,但也不会针对 position6 元素触发 onItemClick。

textview 的自动链接功能肯定会改变我的布局,但我不知道是什么。每次调用我的适配器上的 getView 时,必须有一个我可以重置的属性,但是哪个?

感谢您的帮助。

编辑

让我们看一些代码,这是非常标准/良好的做法。 我适配器的 getView 是: @覆盖 public View getView(int position, View convertView, ViewGroup parent) {

    // Inflate a new layout if needed
    if (convertView == null)
        convertView = createNewView();

    // Gets the item from my array
    AGplus item = (AGplus) getItem(position);
    // Gets the holder pointing to the views
    Holder h = (Holder) convertView.getTag();
    // That's a test version, I won't be using date.toString() later on
    h.date.setText(new Date(item.getDate()).toString());
    // This guys is giving me a headache,
    // If it parses the link, I can't never click on this convertView anymore,
    // event re-converting them for a text that does not contain links
    h.txt.setText(item.getTitle());


    // some image download stuff that doesn't matter for this code

    return convertView;
    }

使用的布局是图像和表格,我在表格中膨胀和插入的行数因每个适配器而异。表格布局是带有 ImageView 的水平线性布局和带有一些边距内容的表格布局,这是行布局:

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

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/text" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/text" />

    </TableRow>

如果我完全删除 android:autoLink="web" 我会得到所有的点击,但如前所述,一旦 View 获得“自动链接”然后我回收该 View ,我无法再次点击该 View 。

编辑

这是布局膨胀:

    private View createNewView() {

    // Instantiate view
    View v = getLayoutInflater().inflate(R.layout.expandable_child_view, null);
    TableLayout table = (TableLayout) v.findViewById(R.id.table);
    Holder h = new Holder();
    v.setTag(h);

    // Instantiate rows
    h.thumb = (ImageView) v.findViewById(R.id.img);
    h.date = (TextView) createNewRow(table, "Date: ");
    h.txt = (TextView) createNewRow(table, "Text: ");
    return v;
    }

    private View createNewRow(ViewGroup group, String title) {
    View row;
    row = getLayoutInflater().inflate(R.layout.item_table_row, null);
    ((TextView) row.findViewById(R.id.title)).setText(title);
    group.addView(row);
    return row.findViewById(R.id.text);
    }

在别人问之前,这是 expandable_child_view 布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/ic_launcher" />

        <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TableLayout>
    </LinearLayout>

正如我之前所说,只是一个带有 ImageView 和表格以及一些边距的线性布局。

最佳答案

根据 Romain Guy here ,这是通过设计来支持轨迹球/dpad 导航。 Comment 27有一个解决方法,通过在每个 ListView 项上设置后代可聚焦性:

setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

android:descendantFocusability="blocksDescendants"

关于android - convertView在listView滚动后失去onitemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12863662/

相关文章:

java - ListView 两次显示列表项

android - Android中的自定义adapterview setselection实现

android - 扩展 AdapterView

java - 如何从 FirestorePagingAdapter 中的 onLoadingStateChanged 方法隐藏/显示进度条?

android - 适用于Superpowered音频SDK的android native中的回调

android - 应用程序第二次在 ListView 的适配器中播放音频时崩溃

java - 当我将函数 setVisible (View.gone) 应用于适配器时,如何从与适配器对应的列表中删除空格?

android - 解决 SpellCheckerSession 泄漏问题?

android - 在 onCreate 中从 NavigationDrawer 内的 ListView 中选择第一项?

android - 在 Fragment(回收器 View )上调用 setAdapter 不起作用