简体   繁体   English

(Android)如何在ListView中设置到文本视图的超链接?

[英](Android) How to set hyperlink to a textview inside ListView?

I have a listview containing some news. 我有一个包含一些新闻的列表视图。 Each news is a row which comtains a title, content, publisher and date. 每个新闻都是一行,其中包含标题,内容,发布者和日期。 Now I want to set hyperlinks to every title TextView. 现在,我想为每个标题TextView设置超链接。 How to implement that? 如何实施? The following is my code: 以下是我的代码:

private void show_news() {
    ArrayList<Map<String, Object>> list = new ArrayList<>();
    get_data(list);
    ListView lv = (ListView) findViewById(R.id.News_List);
    SimpleAdapter adp = new SimpleAdapter(getApplicationContext(), list, R.layout.news_item, new String[]{"news_title", "news_content", "news_publisher", "news_date"}, new int[]{R.id.news_title, R.id.news_content, R.id.news_publisher, R.id.news_date});
    lv.setAdapter(adp);
    /* the code above runs properly*/
    /*Now I want to set hyperlink to my title TextView..*/

}

This is my news_item.xml: 这是我的news_item.xml:

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

<TextView
    android:id="@+id/news_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:textColor="#000000"
    android:textSize="25sp" />

<TextView
    android:id="@+id/news_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:textSize="18sp" />

<TextView
    android:id="@+id/news_publisher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:textSize="12sp" />

<TextView
    android:id="@+id/news_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp" />
</LinearLayout>

I am new to Android programming. 我是Android编程的新手。 Can anyone help me with this? 谁能帮我这个? Thanks a lot! 非常感谢!

LinearLayout first = (LinearLayout) adp.getView(0, null, lv);
TextView title = (TextView)first.getChildAt(0);

That doesn't necessarily get the first element of the list view. 那不一定获得列表视图的第一个元素。 It doesn't even necessarily get the first element on screen. 它甚至不一定会在屏幕上显示第一个元素。 That's very implementation specific (you're assuming the list view doesn't have any children other than its rows, which is actually wrong in many cases). 那是非常特定于实现的(您假设列表视图除了行之外没有其他子级,在许多情况下这实际上是错误的)。

If you want to change the first item of a list view, do 如果要更改列表视图的第一项,请执行

Item item = adb.getItem(0)
item.title= "New Title"
adp.notifyDataSetChanged();

You can extend the SimpleAdapter and use the derived adapter. 您可以扩展SimpleAdapter并使用派生的适配器。 When you extend the adapter, set the hyperlink property for the title by calling: 扩展适配器时,通过调用以下命令设置标题的超链接属性:

<the title textview>.setAutoLinkMask(Linkify.WEB_URLS);

in the method getView method of the adapter. 在适配器的getView方法中。 A commonly used approach in Android ListView adapter is to use a so called view holder, which is a key technique to get better listview performance, and you can check the details from the developer document: Hold View Objects in a View Holder Android ListView适配器中常用的方法是使用所谓的视图持有人,这是获得更好的列表视图性能的关键技术,您可以从开发人员文档中查看详细信息: 在视图持有人中持有视图对象

Suppose you employ this approach, the code would looks more or less like: 假设您采用这种方法,那么代码看起来或多或少像:

 holder.titleView.setAutoLinkMask(Linkify.WEB_URLS);

If your textview does not have a URL, then you can set the text in HTML form as: 如果您的textview没有URL,则可以将HTML形式的文本设置为:

holder.titleView.setText(
    Html.fromHtml(
        "<a href=\"http://www.someurl.com\">Breaking News</a> "));
holder.titleView.setMovementMethod(LinkMovementMethod.getInstance());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM