简体   繁体   中英

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

在此处输入图片说明

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

<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"

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.

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