【问题标题】:convertView loses onitemClick after the listView is scrolled滚动listView后convertView丢失onitemClick
【发布时间】:2012-10-03 12:58:04
【问题描述】:

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

其中一个文本视图设置了android:autoLink="web" 属性,并且列表视图上有一个 onItemClickListener。

问题是每次 TextView 自动链接其内容时,下次转换其父视图时,它不再接收来自 onItemClickListener 的点击。

让我用一个例子来澄清一下:

  • view1、view2、view3 和 view4 位于屏幕的列表视图中。
  • view2 有一个链接,它会出现,然后单击打开链接。
  • 对于view1、view 3和view4的item点击正常。
  • 滚动listview,view1转换为position5,view2转换为position6。
  • 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;
    }

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

    <?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);
    }

在别人问之前,这就是 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>

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

【问题讨论】:

  • 澄清问题的最佳方法是发布适当的代码。
  • 嗨。这是您对 listview+adapter+loader 的标准良好实践实现。我现在将编辑帖子。
  • 同时显示createNewView(),因为这是创建和设置视图的地方。
  • 我没有理会这些,因为它只是在膨胀布局和设置 Holder,但我现在会更新它。

标签: android android-adapter android-adapterview


【解决方案1】:

根据 Romain Guy here 的说法,这是为了支持轨迹球/dpad 导航而设计的。 Comment 27 有一个解决方法,通过在每个列表视图项上设置后代可聚焦性:

setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

android:descendantFocusability="blocksDescendants"

【讨论】:

  • 你只是在等我放赏金来回答它吗?呵呵。谢谢,效果很好。我更新了我当前的项目,我将回到我做的一个个人项目,该项目在列表视图中有一个拖放,其中一个项目(正在回收的)错过了一些操作。 (你可以在 22 小时内奖励你的赏金),如果你已经给出了正确的答案,为什么还要阻止?
  • 不,您的原始编辑只是将其排在“最近”问题列表的顶部。不过,很高兴它对你有用。关于赏金,我认为他们使用至少 24 小时的计时器来防止人们将其奖励给出现的第一个答案,因为很快就会出现更好的答案。
  • 谢谢,效果很好。我有一个 textview 作为我的 listview 项目布局的一部分,它有 android:autoLink="web" 所以最终用户可以点击链接。添加您的添加后,现在一切正常,但在某些项目不可点击之前。
【解决方案2】:

我想我知道会发生什么。当你执行 setText() 时,有时它会清除很多东西。您可能必须使用 ClickableSpan 将链接操作放回文本视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多