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>
salvicode
  • 227
  • 2
  • 12
  • put your code. if you have custom adapter then put your adapter code with clicklistner... – Dhaval Parmar Mar 12 '13 at 12:12
  • 1
    I came across your post and I believe it has been solved here: https://stackoverflow.com/questions/8558732/listview-textview-with-linkmovementmethod-makes-list-item-unclickable – m3hughes Jun 10 '14 at 05:11

1 Answers1

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

deW1
  • 5,562
  • 10
  • 38
  • 54
greenfrvr
  • 643
  • 6
  • 19
  • you could explain what adding this attribute does. – deW1 Apr 17 '15 at 10:45
  • 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 descendantFocusability – greenfrvr Apr 17 '15 at 11:02
  • 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... http://stackoverflow.com/questions/8558732/listview-textview-with-linkmovementmethod-makes-list-item-unclickable – Dallas187 May 20 '15 at 05:04