【问题标题】:Picaso with ImageGetter in TextView from JSON来自 JSON 的 TextView 中带有 ImageGetter 的 Picaso
【发布时间】:2016-02-16 22:22:03
【问题描述】:

再一次,我一直试图在网上寻找答案。但是我似乎无法找到一个与我的问题相匹配的。

基本上,我有一个来自我正在使用的 API 的表情符号列表,我正试图让这些图像显示在一个不起作用的 TextView 中。图像在我的回收视图中显示为蓝色方块,直到我向上滚动到它们然后它们出现。这是我的代码,我不确定我在做什么是正确的。我不记得为什么我做了我所做的,因为它是旧代码,我正在尝试重构它。任何人都可以帮忙吗?

代码如下:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Posts posts = mPost.getItem(position);
    emoticons = mEmoticon.getItems();
    String message = null;
    String emoMessage = null;

    if (posts.getPost() != null) {
        if (posts.getPost().getMessage() != null) {
            message = posts.getPost().getMessage();
            emoMessage = message;


            if (emoticons != null) {
                for (Emoticons emoticon : this.emoticons) {
                    if (message.contains(emoticon.getEmoticon().getCode())) {
                        emoMessage = message.replaceAll(Constants.EMO_REGEX, emoticon.getEmoticon().getUrl());
                    }

                }
            }

holder.mPostTextView.setText(Html.fromHtml(emoMessage, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                Target loadTarget;
                loadTarget = new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        try {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                            URL url = new URL(source);
                            InputStream is = url.openStream();
                            Bitmap b = BitmapFactory.decodeStream(is);
                            mDrawable = new BitmapDrawable(Resources.getSystem(), b);
                            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth() + 25, mDrawable.getIntrinsicHeight() + 25);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                };
                Picasso.with(mContext).load(source).into(loadTarget);
                return mDrawable;
            }
        }, null);
}

【问题讨论】:

    标签: android json picasso fromhtml


    【解决方案1】:

    似乎当您调用 mPostTextView.setText() 时,您的 mDrawable 尚未初始化。稍后,当调用 onBitmapLoaded() 时,您将更新 mDrawable 以指向您新创建的 BitmapDrawable。但这不会更新 TextView。

    我会尝试重新排列,以便在 onBindViewHolder() 中调用

    Picasso.with(mContext).load(source).into(loadTarget);

    然后在 onBitmapLoaded() 结束时调用 mPostTextView.setText()。如果您同时需要保留对 emoMessage 的引用,可以将其作为 Target 中的字段传递。

    【讨论】:

    • 但是我在我的 setText() 中完成了所有这些,我将如何改变它?
    • 抱歉 - 我又看了一遍,我认为我的想法行不通。这可能有帮助吗? stackoverflow.com/questions/25194549/…
    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多