Android:如何在 ListView 中添加 HTML 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1697908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:28:35  来源:igfitidea点击:

Android: How can I add HTML links inside a ListView?

htmlandroidlistviewhyperlink

提问by Legend

How would I go about adding clickable links inside a ListView?

我将如何在 ListView 中添加可点击的链接?

回答by Legend

This is done using the autoLink attribute of a TextView. Took me some time to dig through the documentation so putting it here with an example in case someone else is looking for it:

这是使用 TextView 的 autoLink 属性完成的。我花了一些时间来仔细阅读文档,所以把它和一个例子放在一起,以防其他人正在寻找它:

Let us assume that you are binding your listview to a custom adapter. In that case, the following piece of code goes into your getView call:

让我们假设您将列表视图绑定到自定义适配器。在这种情况下,以下代码将进入您的 getView 调用:

Code:

代码:

textcontent.setText(Html.fromHtml(item.get_text()));
textcontent.setAutoLinkMask(Linkify.WEB_URLS);

Just put the link inside the text being passed to the setText call and you're done.

只需将链接放在传递给 setText 调用的文本中,就完成了。

XML:

XML:

<TextView
                android:id="@+id/txtview"
                android:autoLink="web"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="put your link here"/>

Hope that helps...

希望有帮助...

回答by emmby

If you have text that is already in HTML format, the best thing to do is the following:

如果您的文本已经是 HTML 格式,最好的做法是:

TextView textcontent = (TextView) findViewById(...);
textcontent.setMovementMethod(LinkMovementMethod.getInstance());

String text = "<a href="http://www.stackoverflow.com">stackoverflow.com</a>";
textcontent.setText(Html.fromHtml(text));

This will cause any link tags to be clickable in your text view. Alternately, you could use android:autoLink="web"as suggested by Legend, but this has the side-effect of a) linkifying urls that are not wrapped in anchor tags, and b) potentially missing urls or linkifying things that aren't urls. If you want the smarts of autoLink then you should use it, but if all you want is to linkify the tags that are already there, you're better off using setMovementMethod().

这将导致在您的文本视图中可以点击任何链接标签。或者,您可以android:autoLink="web"按照 Legend 的建议使用,但这具有以下副作用:a) 链接未包含在锚标记中的 url,以及 b) 可能丢失 url 或链接不是 url 的内容。如果你想要自动链接的智能,那么你应该使用它,但如果你只想链接已经存在的标签,你最好使用setMovementMethod().

See this bug report for more details: http://code.google.com/p/android/issues/detail?id=2219

有关更多详细信息,请参阅此错误报告:http: //code.google.com/p/android/issues/detail?id=2219

回答by Beer Me

Hmm, it seems that adding textcontent.setMovementMethod(LinkMovementMethod.getInstance()); makes it so that the clicks on the textview's text parts are no longer passed through to the listview below.

嗯,好像加了 textcontent.setMovementMethod(LinkMovementMethod.getInstance()); 使得对 textview 的文本部分的点击不再传递到下面的列表视图。

I found a simple workaround under Issue 3414, Comment 27:

我在问题 3414,评论 27下找到了一个简单的解决方法:

An easy way to work around this problem is to call "setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);" on the listView views as they are added. You'll be able to select rows, click on rows and click on child checkboxes and buttons.

解决此问题的一种简单方法是调用“setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);” 在 listView 视图上添加它们。您将能够选择行、单击行并单击子复选框和按钮。

It worked perfectly for me, although some casting was required:

尽管需要进行一些转换,但它对我来说效果很好:

View v;
((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

回答by Duc Tran

You need to set a function setOnItemClickListener() and inside it declare something like this:

您需要设置一个函数 setOnItemClickListener() 并在其中声明如下内容:

Uri uri = Uri.parse( "http://www.google.com" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );

回答by Diiiiii

The tricky part of listview is nothing within (for instance a TextView of a Button) is clickable!

listview 的棘手部分是(例如按钮的 TextView)中的任何内容都是可点击的!

Basically you need two string arrays:

基本上你需要两个字符串数组:

  1. names that users see on the list_view;
  2. hyperlinks that you want to direct them to go.
  1. 用户在 list_view 上看到的名称;
  2. 您想要引导他们去的超链接。

In the array.xml:

在 array.xml 中:

<string-array name="search_provider_name_array">
    <item>Google</item>
    <item>Yahoo</item>
    <item>Bing</item>
    <item>Baidu</item>
</string-array>
<string-array name="search_provider_link_array">
    <item>https://www.google.com</item>
    <item>https://www.yahoo.com</item>
    <item>https://www.bing.com</item>
    <item>https://www.baidu.com</item>
</string-array>

In the layout_search_provider.xml it contains a list view:

在 layout_search_provider.xml 中,它包含一个列表视图:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv_search"
    android:dividerHeight="1dp"/>

In your activity:

在您的活动中:

public class SearchProvider implements  AdapterView.OnItemClickListener {
    private ListView lv_search;
    private String[] names = getResources().getStringArray(R.array.search_provider_name_array);
    private String[] links = getResources().getStringArray(R.array.search_provider_link_array);

    //..

    @Override
    public View onCreateView(View v, String name, Context context, AttributeSet attrs) {
        lv_search= (ListView) v.findViewById(R.id.lv_search);

        ArrayAdapter sAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, names);
        lv_search.setAdapter(sAdapter);
        lv_search.setOnItemClickListener(this);

        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i<links.length){
            Uri uri = Uri.parse(links[i]); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

}

When your list is dynamic, you can the following method(s) to update your listview.

当您的列表是动态的时,您可以使用以下方法来更新您的列表视图。

  • move the code in onCreateView() into onResume().
  • sAdapter.notifyDataSetChanged();
  • 将 onCreateView() 中的代码移动到 onResume() 中。
  • sAdapter.notifyDataSetChanged();