【问题标题】:Android Picasso: Not Loading in FragmentAndroid Picasso:不加载片段
【发布时间】:2013-11-17 19:05:00
【问题描述】:

我正在尝试 Picasso,以便更轻松地使用图像进行内存管理。我一直在尝试在我的片段中实现它,但我似乎无法让它工作。

mainLayout.setBackground(new BitmapDrawable(getResources(), Picasso.with(mainLayout.getContext()).load(R.drawable.background2).get()));

其中 mainLayout 是一个线性布局。我也试过这个:

Picasso.with(getActivity().getApplicationContext()).load(R.drawable.background2).into(imageView1);

我试过 Picasso.with(this)... 但这根本不起作用。

我不断收到以下异常:

java.lang.IllegalStateException: Method call should not happen from the main thread.
at com.squareup.picasso.Utils.checkNotMain(Utils.java:71)
at com.squareup.picasso.RequestCreator.get(RequestCreator.java:206)
at ... 

我叫它的地方。

任何人都经历过或知道如何正确使用片段?

【问题讨论】:

    标签: android android-fragments picasso


    【解决方案1】:

    异常消息非常清楚地说明了这一点。 .get() 消息不是异步的,因此将在主线程上完成工作(网络、读取文件等),您应该尽可能避免。

    不过,您将图像设置为 imageView 的代码对我来说似乎是正确的。

    我认为也可以对 mainLayout 做同样的事情(毕加索会自动将可绘制/位图设置为背景)。 如果我在这里错了,请查看 Picasso 附带的 Target.class。您可以将图像加载到目标中,该目标必须提供成功和错误的处理程序。在成功处理程序中,您将在加载后获得位图,并将其设置为您的 bakground。

    有一些解决方案可能适用于您的情况。

    [编辑]

    当使用 Picasso 提供的 get() 方法时,加载将在您当前工作的线程中进行,源代码中明确说明了这一点: https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/RequestCreator.java

    /** 同步完成这个请求。不得从主线程调用。 */

    公共位图 get() 抛出 IOException {[...]}

    在您的情况下,我将使用 Target 接口,该接口在加载图像时提供回调。

    Picasso.with(getActivity()).load(R.drawable.background2).into(new Target(){
    
      @Override
      public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
         mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
      }
      
      @Override
      public void onBitmapFailed(final Drawable errorDrawable) {
          Log.d("TAG", "FAILED");
      }
    
      @Override
      public void onPrepareLoad(final Drawable placeHolderDrawable) {
          Log.d("TAG", "Prepare Load");
      }      
    });
    

    由于您在这里使用的是内部资源,为什么不直接执行以下操作?

    mainLayout.setBackgroundResource(R.drawable.background2);
    

    希望这会有所帮助。

    问候

    【讨论】:

    • 所以你是说我应该在 AsyncTask 中加载它们?
    • 不,我不建议这样做,它只会在不需要的地方增加复杂性。我将编辑我的答案并提供一些工作示例。
    • 好的,这很好,更有意义。关于mainLayout.setBackgroundResource(),在内存消耗方面这是一个好方法吗?
    • 毕加索已经在后台线程中满足您的要求。 get() 如果您想同步工作以加载位图并执行显示它以外的操作(可能是上传它)。
    【解决方案2】:

    试试看

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.imageview = (ImageView) v.findViewById(R.id.news_img);
            holder.tvName = (TextView) v.findViewById(R.id.news_title);
            holder.tvDate = (TextView) v.findViewById(R.id.news_time);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
    
        holder = (ViewHolder )v.getTag();
        Picasso.with(context).load(postList.get(position).getThumbnail()).into(holder.imageview);
        holder.tvName.setText(postList.get(position).getTitle());
        holder.tvDate.setText(postList.get(position).getDate());
    
        return v;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2015-09-22
      • 2014-07-27
      • 2018-07-06
      相关资源
      最近更新 更多