【问题标题】:Textview`s LinkMovementMethod blocks listitem touch eventTextview 的 LinkMovementMethod 阻止 listitem 触摸事件
【发布时间】:2013-02-27 22:29:37
【问题描述】:

我对 Listview 有一些问题。 Listview 的列表项由 imageview 和 textview 组成。 Textview 包含可点击的链接。当我将 LinkMovementMethod 设置为 textviews 时,listitems 不会收到 onclick 事件。我仍然找不到任何正确的解决方案=(。任何人都可以解释如何解决它或举例说明在 listitem 中使用带有可点击链接的 textview 吗?谢谢。 这是适配器中的文本视图创建:

    TextView text = (TextView) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                        .inflate(R.layout.post_content_text, null);
SpannableString sString = new SpannableString(d.getText());
//I have my own class UrlSpan that contain fields startPosition, endPosition and urlLink
for (UrlSpan us : ((TextData) d).getUrls())
                {
                    URLSpan urlSpan = new URLSpan(us.getUrl());
                    sString.setSpan(urlSpan, us.getStart(), us.getEnd(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
                }
text.setText(sString);
text.setMovementMethod(LinkMovementMethod.getInstance());
view.addView(text);

列表项是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="visible" >

    <LinearLayout
        style="?PostListRow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="65dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/user_image"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_margin="3dp"
                android:clickable="true"
                android:src="@drawable/ic_no_photo" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="55dp"
                android:layout_margin="0dp"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/user_title"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:textSize="@dimen/title_in_list" />

                <TextView
                    android:id="@+id/post_datetime"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:textSize="@dimen/text_size_small" />
            </LinearLayout>

            <TextView
                android:id="@+id/post_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="123" />
        </LinearLayout>
<!-- it is a view where textview with links is to be added to -->
        <LinearLayout
            android:id="@+id/post_data"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="6" >
        </LinearLayout>

        <TextView
            android:id="@+id/post_view"
            style="?PostText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/topic_edit_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 输入你的代码。如果您有自定义适配器,则将您的适配器代码与 clicklistner...
  • 我看到了你的帖子,我相信这里已经解决了:stackoverflow.com/questions/8558732/…

标签: android listview textview listitem linkmovementmethod


【解决方案1】:

尝试添加这个属性 android:descendantFocusability="beforeDescendants" 到你的列表视图

来自文档:

它定义了 ViewGroup 和它的后代在寻找一个 View 以获取焦点时的关系。在我看来,当您的列表视图中有一些活动视图(如按钮或编辑文本等)时,您必须定义 descendantFocusability

【讨论】:

  • 你可以解释一下添加这个属性的作用。
  • 来自文档,它定义了 ViewGroup 及其后代在寻找 View 以获取焦点时的关系。在我看来,当您的列表视图中有一些活动视图(如按钮或编辑文本等)时,您必须定义 descendantFocusability
  • 在 ListView 中使用 SpannableString 和 LinkMovementMethod 时,阻止后代可聚焦性不起作用。也许您可以从这个 SO 接受的答案中尝试该方法...stackoverflow.com/questions/8558732/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多