我对Listview有一些问题。 Listview包含由imageview和textview组成的列表项。 Textview包含可点击的链接。当我将LinkMovementMethod设置为textviews时,listitems不会收到onclick事件。我仍然找不到任何正确的解决方案=(。任何人都可以解释如何解决它或给出一个使用textite与listitem内的可点击链接的例子?谢谢。 我试过很多方法,但我不能做有用的!坦克你的帮助。这是适配器中的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>