Textview`s LinkMovementMethod blocks listitem touch event
Asked Answered
Y

1

8

I have some problem with Listview. Listview has list items that consist of imageview and textview. Textview contains clickable links. When I set LinkMovementMethod to textviews, listitems doesn`t receive onclick event. I still cannot find any right solution=(. Can anyone explane how to solve it or give an example of using textview with clickable links inside listitem? Thanks. Here is textview creation in adapter:

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

and list item is:

<?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>
Yingling answered 12/3, 2013 at 12:5 Comment(2)
put your code. if you have custom adapter then put your adapter code with clicklistner...Parenthood
I came across your post and I believe it has been solved here: #8559232Ethiopic
B
0

try to add this attribute android:descendantFocusability="beforeDescendants" to your listview

from the documentation:

it defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. it seems to me that when you have some active views like buttons or edittext and etc in your listview you have to define descendantFocusability

Berners answered 17/4, 2015 at 9:45 Comment(3)
you could explain what adding this attribute does.Bicarb
from docs it defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. it seems to me that when you have some active views like buttons or edittext and etc in your listview you have to define descendantFocusabilityBerners
Blocking descendant focusability does not work when using a SpannableString and LinkMovementMethod in a ListView. Perhaps you could try the approach from this SO accepted answer... #8559232Scintillation

© 2022 - 2024 — McMap. All rights reserved.