在自定义listview android中使链接可点击

时间:2013-06-19 17:38:26

标签: java android textview

我创建了一个listview wit自定义addapter来显示来自用户的推文。现在有时在推文中提供了一个链接。我希望它们是可点击的,并且listview中的项目本身不必是可点击的。这是我的listview的布局,有2行显示示例

IMG http://i44.tinypic.com/35mgq6g.png

我试图使textview中的链接(有时链接的文本)可以点击,也可以自动链接:XML和编程的WEB。但每次我点击链接时,它都会选择listitem而不是textview中的链接。

在java代码中它是这样的:

message.setAutoLinkMask(1);                 message.setLinksClickable(真);

public class UserItemAdapter extends ArrayAdapter<Tweet> {
    private ArrayList<Tweet> tweets;

    public UserItemAdapter(Context context, int textViewResourceId,
            ArrayList<Tweet> tweets) {
        super(context, textViewResourceId, tweets);
        this.tweets = tweets; 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);
        }

        Tweet tweet = tweets.get(position);
        if (tweet != null) {
            TextView username = (TextView) v.findViewById(R.id.username);
            TextView message = (TextView) v.findViewById(R.id.message);
            **message.setAutoLinkMask(1);
            message.setLinksClickable(true);**
            ImageView image = (ImageView) v.findViewById(R.id.avatar);
            TextView date = (TextView) v.findViewById(R.id.date);
            if (username != null) {
                username.setText(tweet.username);
            }

            if (message != null) {
                message.setText(tweet.message);
            }

            if (date != null) {
                date.setText(tweet.hfdate);
            }
        }
        return v;
    }
}

和listitem的xml在textview消息中就像这样

android:autoLink =“web”android:linksClickable =“true”

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px"
tools:context=".TwitterActivity$AsyncOp" >

<ImageView
    android:id="@+id/avatar"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/twitterimg" />

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/Orange" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:autoLink="web"
        android:linksClickable="true"
        android:textColor="#0099CC" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:textColor="@color/Orange" />
</LinearLayout>

</LinearLayout>

任何人都知道我做错了吗?

0 个答案:

没有答案
相关问题