Android:ListView中的HTML链接 - 突出显示的问题

时间:2010-06-23 20:09:01

标签: android listview hyperlink android-listview listviewitem

我编写了使用ListActivity的应用程序。列表中的每个项目都包含ImageViewTextView。长按列表条目会触发一些菜单和颜色效果,因为onCreateContextMenu被覆盖了。有时TextView包含我想要互动的HTML链接。我阅读了#1697908并激活了链接,因此启动了浏览器/ youtube播放器。一切都会很好,但长按的颜色效果消失了(上下文菜单仍然出现)。

有人可以告诉我如何连接这两个功能并获得颜色效果吗?

3 个答案:

答案 0 :(得分:1)

您可以在自定义列表适配器中使用Linkify。 Linkify允许您使用选择器设置颜色,如下所示:

                Linkify.addLinks(
                        holder.messageText,
                        messageDetailsMatcher,
                        "content://com.myApp/message/view?messageId=",
                        null, new myLinkTransformFilter(msgId));


                ColorStateList colors = null;
                try {
                    XmlResourceParser xpp = getResources().getXml(
                            R.color.link_color_selector);
                    colors = ColorStateList.createFromXml(getResources(),
                            xpp);
                } catch (Exception e) {
                    Log.e("someError", e);
                }
                holder.messageText.setLinkTextColor(colors);

(注意:holder.messageText是持有者对象中的简单TextView)

然后你有一个像这样的/res/color/color_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="@drawable/message_focused" /> <item android:state_pressed="true" android:state_enabled="false" android:color="@drawable/message_pressed" /> <item android:state_enabled="false" android:color="@drawable/message_enabled" /> <item android:state_active="true" android:color="@drawable/message_active" /> <item android:color="@drawable/message_default" /> </selector>

答案 1 :(得分:0)

我在开头使用了ImageView和Textview,但你可以使用WebView避免这种问题并保持html的交互性。阅读本文

How can I have a floating image (right aligned) with text that wraps around the image in Android Layout?

答案 2 :(得分:0)

我设法解决了这个问题。也许不是直接以我想要的方式,但这对我来说已经足够了。我没有将监听器添加到TextView,而是将其添加到整行。突出显示正如我所料。这种行为对我的应用程序来说是可以接受的,但是是某种解决方法,所以我仍然想知道它是否可以做得更好。