convertView在ListView滚动后失去onItemClick事件

3
我在我的适配器上遇到了一个有点奇怪的错误。 适配器创建的视图是左侧的缩略图和带有几行的表格。每行都有两个文本视图。
其中一个文本视图设置了android:autoLink="web"属性,并且listview上设置了onItemClickListener。
问题在于,每次TextView自动链接其内容后,下一次其父视图被转换时,它不再接收来自onItemClickListener的点击。
让我用一个例子来澄清一下:
- view1、view2、view3和view4在屏幕上的列表视图中。 - view2有一个链接,当点击链接时,它打开。 - 对于view1、view 3和view4,项目单击正常工作。 - 滚动listview并将view1转换为position5,然后将view2转换为position6。 - 位置6处的项不包含链接,但也没有触发onItemClick元素的点击。
文本视图的自动链接功能肯定会改变我的布局,但我不知道是什么。必须有一个我可以对我的适配器上的每个调用重置的属性,但是哪个?
感谢任何帮助。
编辑
让我们看一些代码,这是相当标准/良好的实践。我的适配器中的getView是:
    // 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",我可以获取所有的点击事件,但正如之前所述,一旦视图被"自动链接",然后我回收该视图,就无法再次获取该视图上的点击事件。
编辑
以下是布局膨胀代码:
    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);
    }

还有人问之前的问题,那就是可扩展子视图布局:

    <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>

如我之前所说,只需使用线性布局、图像视图和表格以及一些边距即可。


澄清问题的最佳方式是发布适当的代码。 - Sam
嗨。这是您标准的好实践实现,使用listview+adapter+loader。我现在会编辑帖子。 - Budius
请同时显示 createNewView(),因为这是视图被创建和设置的地方。 - Geobits
我之前没有去处理那些内容,因为那只是简单地填充布局并设置 Holder,但现在我会进行更新。 - Budius
2个回答

6
根据Romain Guy 这里 的说法,这是为了支持轨迹球/DPAD导航而特意设计的。评论27 提供了一个解决方法,即在每个列表项上设置后代焦点性。
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

或者

android:descendantFocusability="blocksDescendants"

你是在等我放赏金才回答的吗?呵呵呵。谢谢,它很好用。我已经在我的当前项目中更新了它,接下来我会回到一个个人项目上,这个项目在列表视图上有一个拖放功能,其中一个被回收的项目缺少了一些操作。(您可以在22小时内颁发悬赏),为什么SO会阻止,如果您已经给出了正确的答案呢? - Budius
不,你的原始编辑只是将它推到了“最近”问题列表的顶部。虽然我很高兴它对你有用。关于赏金,我认为他们使用最少24小时的计时器来防止人们将其授予第一个出现的答案,因为可能很快会出现更好的答案。 - Geobits
谢谢,非常好用。我的ListView项布局中有一个TextView,并且它具有android:autoLink="web",以便最终用户可以单击链接。在添加您的补充之后,现在一切都正常了,但是在此之前,某些项目无法单击。 - j2emanue

0

我想我知道可能出了什么问题。当你使用setText()时,有时它会清除很多东西。你可能需要使用ClickableSpan将链接操作放回到textview中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接