在ListView项的Andr​​oid SpannableString [英] Android SpannableString on ListView item

查看:157
本文介绍了在ListView项的Andr​​oid SpannableString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表适配器一个ListView(延伸BaseAdapter)。我的列表项视图从这个布局膨胀:

I have a ListView with a custom list adapter (that extends BaseAdapter). My list items' view are inflated from this layout:

<?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="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        >
        <TextView
            android:id="@+id/txtOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
        />

        <TextView
            android:id="@+id/txtTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </LinearLayout>
</LinearLayout>

在getView方法中的项目视图中创建并填充:

At getView method the items' view are created and populated:

if (convertView == null) {
    convertView = inflater.inflate(R.layout.teste, parent, false);
}
((TextView) convertView.findViewById(R.id.txtOne)).setText("Some text");
final SpannableString ss = new SpannableString("Another text");
ss.setSpan(new ClickableSpan() {
    @Override
    public void onClick(final View widget) {
        startActivity(new Intent(widget.getContext(), AnotherActivity.class));
    }
    }, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView) convertView.findViewById(R.id.txtTwo)).setText(ss);

它显示我对项目的看法spannable字符串,但我不能触摸它。我只能碰整个项目视图。

It's showing my spannable string on item's view, but I cannot touch on it. I can only touch the entire item view.

我要开始另一个活动时,用户触摸spannable字符串,但我也希望整个项目视图toucheable。

I want to start another activity when user touches on spannable string but I also want the entire item view toucheable.

我是什么失踪?

推荐答案

您需要做两件事情之一:

You need to do one of two things:


  1. 的android:linksClickable =真正的的TextView 布局

在code: txtViewTwo.setMovementMethod(LinkMovementMethod.getInstance());

最后的结果会是一样的。

The end result would be the same.

这篇关于在ListView项的Andr​​oid SpannableString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆