Egos Zhang
Egos Zhang

Reputation: 1364

TextView with ImageSpans inside ListView is wrong layout

I have a ListView ,and the item is a TextView. It work well when I only use string.The TextView layout wrong when I add ImageSpan. Follow is my code. init a listview

private void initListView()
{
    SpannableString spannableString1 = replaceEmotion(this, "[Android]", 0);
    SpannableString spannableString2 = replaceEmotion(this, "123456123456123456", 0);
    for (int i = 0; i < 140; i++)
    {
        int t = (int) (Math.random() * 2);

        if (t % 2 == 0)
        {
            listData.add(spannableString1);
        }
        else
        {
            listData.add(spannableString2);
        }
    }
    listItemAdapter = new ArrayAdapter<>(this, R.layout.item_list, listData);
}

replace a string to a ImageSpan.

    public SpannableString replaceEmotion(Context context, String content, int length)
    {
        if (length < 0)
        {
            length = 0;
        }
        SpannableString result = new SpannableString(content);
        if (context == null || content == null)
        {
           return null;
        }
        int start = length;
        int end;
        while ((start = content.indexOf("[", start)) != -1 && (end = content.indexOf("]", start)) != -1)
        {
            String img = content.substring(start, end + 1);
            if ("[Android]".equals(img))
            {
                Drawable drawable = context.getResources().getDrawable(R.mipmap.ic_launcher);
                if (drawable != null)
                {
                   drawable.setBounds(0, 0, (int) context.getResources().getDimension(R.dimen.fab_margin) * 3,
                                            (int) context.getResources().getDimension(R.dimen.fab_margin) * 3);
                   ImageSpan span = new ImageSpan(drawable);
                   result.setSpan(span, start, end + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                 }
            }  
            start++;
         }
      return result;
  }

enter image description here

I have try to requestLayout,but it doesn't work.

Upvotes: 0

Views: 135

Answers (2)

Siyamed
Siyamed

Reputation: 515

Base on the looks of the result, I guess framework does not update the height of the "recycled" TextView even if the content has changed. Actually it does not update the Layout object that is being used by the TextView. Can you please try to set the layout_width of the TextView's to be "wrap_content"? It will force TextView to recalculate (and recreate) the Layout object.

Upvotes: 0

Egos Zhang
Egos Zhang

Reputation: 1364

<LinearLayout
    android:id="@+id/cotainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"/>
</LinearLayout>

i solve it by change above to below.

<LinearLayout
    android:id="@+id/cotainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"/>
</LinearLayout>

yes.only TextView android:layout_width="match_parent" to android:layout_width="wrap_content"

Upvotes: 0

Related Questions