android - 为什么带有可点击链接的 ListView 项目不可点击?

标签 android android-listview listviewitem

我花了很多时间尝试创建 ListView,其中包含启动新 Activity 的可点击项目和打开浏览器页面的URL 链接

应该是以下项目:

一些文本 [URL 在这里]

通过点击Some text 新 Activity 开始

通过点击 [URL] 网络浏览器启动

我尝试像 here 中那样实现它或 here但无论如何这对我不起作用。

也许我没有设置一些那里没有提到的属性。

简单的 ListView :

<ListView
    android:id="@+id/news_list_view_full"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    android:padding="@dimen/content_padding_small"
    android:layout_below="@id/news_header"/>

ListView 的项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/news_item_padding"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/news_text_view_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:textColorLink="@color/sosbg_orange"
        android:textAppearance="@style/CustomRegularTextStyle"
        android:textColor="@color/sosbg_white" />

</LinearLayout>

和适配器:

public View getView(...View view...) {
    ...
    TextView text = (TextView) view.findViewById(R.id.news_text_view_text);
    text.setText(Html.fromHtml("Hello world <a href='http://google.com'>http://google.com</a>"));
    text.setMovementMethod(LinkMovementMethod.getInstance());
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // start new activity
        }
    }
}

因此,目前,链接是可点击的,并打开带有适当页面的浏览器

但是listview的item是不可点击的。因此,新 Activity 不会启动。

能否请您建议一些我可以尝试实现所需结果的步骤?

最佳答案

为什么要使用可点击的 View 。?

如果您需要文本可点击,请使用 TextView 的文本对象而不是 View 。

将其替换为。

view.setOnClickListener(new View.OnClickListener() {

       }

像这样..

text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // start new activity
    }
}

关于android - 为什么带有可点击链接的 ListView 项目不可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26255438/

相关文章:

Android:关闭软键盘时显示空白

android - 隐藏 ListView 行而没有间距

android - Richtext/CK 编辑器在 Android 上不工作

android - 如何使用 onInterceptTouchEvent 从 RecyclerView 中点击特定 View ?

android - Activity 开始时如何播放声音

android - 需要删除 ListView 中所有现有的页脚

android - 如何在 Android 中以编程方式关闭 USB 大容量存储?

java - Android - 使用游标适配器在 ListView 中格式化时间戳

android - ListView 中的字母索引..忽略部分索引

以编程方式设置 android.R.attr.listChoiceIndicatorMultiple 时的 android.content.res.Resources$NotFoundException