开发者

Android SpannableString on ListView item

开发者 https://www.devze.com 2023-03-03 09:44 出处:网络
I have a ListView with a custom list adapter (that extends BaseAdapter). My list items\' view are inflated from this layout:

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_cont开发者_开发问答ent"
        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>

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);

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

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

What am I missing?


You need to do one of two things:

  1. android:linksClickable="true" on the TextView in layout

  2. in code: txtViewTwo.setMovementMethod(LinkMovementMethod.getInstance());

The end result would be the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消