我对 Listview 有一些问题。Listview 的列表项由 imageview 和 textview 组成。Textview 包含可点击的链接。当我将 LinkMovementMethod 设置为 textviews 时,listitems 不会收到 onclick 事件。我仍然找不到任何正确的解决方案=(。任何人都可以解释如何解决它或举例说明在 listitem 中使用带有可点击链接的 textview 吗?谢谢。这是在适配器中创建的 textview:
TextView text = (TextView) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.post_content_text, null);
SpannableString sString = new SpannableString(d.getText());
//I have my own class UrlSpan that contain fields startPosition, endPosition and urlLink
for (UrlSpan us : ((TextData) d).getUrls())
{
URLSpan urlSpan = new URLSpan(us.getUrl());
sString.setSpan(urlSpan, us.getStart(), us.getEnd(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
}
text.setText(sString);
text.setMovementMethod(LinkMovementMethod.getInstance());
view.addView(text);
列表项是:
<?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="fill_parent"
android:visibility="visible" >
<LinearLayout
style="?PostListRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/user_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="3dp"
android:clickable="true"
android:src="@drawable/ic_no_photo" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_margin="0dp"
android:orientation="vertical" >
<TextView
android:id="@+id/user_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clickable="true"
android:textSize="@dimen/title_in_list" />
<TextView
android:id="@+id/post_datetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
<TextView
android:id="@+id/post_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="123" />
</LinearLayout>
<!-- it is a view where textview with links is to be added to -->
<LinearLayout
android:id="@+id/post_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="6" >
</LinearLayout>
<TextView
android:id="@+id/post_view"
style="?PostText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone" />
<LinearLayout
android:id="@+id/topic_edit_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>