0

我有一个审查系统,允许通过 imgur 添加文本和上传图片。当我显示评论时,图片显示两次,我不明白为什么。

这是代码的一部分:

public class ReviewListAdapter extends BaseAdapter {
...
    public View getView(int position, View convertView, ViewGroup parent) {
    ....
        ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder(convertView);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
          ...

    if (text != null) {

        int begin = text.indexOf("[img]");
        int end = text.indexOf("[/img]");
        if (begin != -1 && end!=-1 && begin<end) {
            String imgUrl = text.subSequence(begin + 5, end).toString();
            imgUrl = imgUrl.substring(0,imgUrl.length()-4)+"m"+imgUrl.substring(imgUrl.length()-4, imgUrl.length());
            Log.e("IMG", imgUrl); // imgUrl == http://i.imgur.com/AL4Em6cm.jpg

            startSignal = new CountDownLatch(1);
            new GetImage().execute(imgUrl);
            try {
                startSignal.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // We should have the picture in the Drawable : myPicture
            SpannableStringBuilder ssb = new SpannableStringBuilder(text);

            Bitmap pic = Utils.drawableToBitmap(myPicture);
            ssb.setSpan(new ImageSpan(pic), begin, end + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.textView.setText(ssb, BufferType.SPANNABLE);
        }else{
            holder.textView.setText(text);
        }


    }

我还将私有类 GetImage (在同一个文件中)

private class GetImage extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {

        URL url;
        boolean isPicture = false;

        try {
            if (!params[0].equals(""))
                isPicture = true;

            if (isPicture)
                url = new URL(params[0]);
            else
                url = new URL(icon);

            Bitmap mIcon1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            if (isPicture)
                myPicture = new BitmapDrawable(ctx.getResources(), mIcon1);
            else
                myIcon = new BitmapDrawable(ctx.getResources(), mIcon1);

            startSignal.countDown();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我的列表布局 (list_item.xml )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
             android:contentDescription="thumb"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_marginTop="5dp"
                android:id="@+id/author_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/author_name"
                android:layout_gravity="center"
                android:paddingRight="8dp"
                android:textSize="12sp" />
        </RelativeLayout>
    </LinearLayout>

<TextView
    android:id="@+id/texto"
    android:layout_below="@id/ll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
 />

</RelativeLayout>

我的图片作为可跨越的内容插入到 textview

4

2 回答 2

0

我遇到了和你类似的问题。ImageSpan 在特定条件下显示两次。这是我的问题的链接。我发现如果 setBound 比某个宽度宽,图片会显示两次。尽管您的代码不涉及 setBound 调用。

于 2014-03-31T01:12:41.637 回答
0

老问题和一个利基 API,但对于发生在它上面的任何其他人:这种奇怪的行为可以完全解决,只需使用 结束范围start+1,然后删除其余文本的跨度:

span.setSpan(ImageSpan(context, drawable), start, start+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
span.delete(start+1, end)
于 2019-06-17T15:41:33.290 回答