简体   繁体   中英

Clickable url in listview item

I have done an Android chat with using of list view and Firebase. I want to do links in chat messages active. Links are in different formats in the text of message_text textview. How I can do them active?

My code:

item_in_message.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_in">

<TextView
    android:id="@+id/message_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="fdsfsdf"
    android:textStyle="normal|bold" />

<TextView
    android:id="@+id/message_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/message_user"
    android:layout_marginTop="5dp"
    android:text="dsfsdfds"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textColor="@android:color/white"
    android:textSize="18sp" />

<TextView
    android:id="@+id/message_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/message_text"
    android:text="sdfsdfsd" />

item_out_message.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/bubble_out">

    <TextView
        android:id="@+id/message_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="fdsfsdf"
        android:textStyle="normal|bold" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/message_user"
        android:textColor="@android:color/white"
        android:layout_marginTop="5dp"
        android:text="dsfsdfds"
         android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/message_text"
        android:text="sdfsdfsd" />
</RelativeLayout>

add these lines in xml to the textview tag

 android:autoLink="web"
 android:linksClickable="true"

You need make a List View Base Adapter implementation

for example

listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public boolean onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //here you can use the position to determine what checkbox to check
            //this assumes that you have an array of your checkboxes as well. called checkbox
            checkBox[position].setChecked(!checkBox.isChecked());
        }
    });

ListView is your list adapter List adapter reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM