ListView中的毕加索图像加载器

时间:2016-05-23 10:05:21

标签: android listview picasso

我现在面临着奇怪的问题。我使用picasso库下载并在listview中显示图像。

我使用target在运行时更改imageview大小。

private List<Target> targets = new ArrayList<>();

列出班级的顶部。

这是完整的getView方法;

@Override
public View getView(final int position, View row, ViewGroup parent) {


    final CategoryMoneyViewHolder categoryMoneyViewHolder;


    if (getItem(position) instanceof MoneyItem) {


        final MoneyItem moneyItem = (MoneyItem) getItem(position);

        if (row == null) {


            row = layoutInflater.inflate(R.layout.row_category_money, parent, false);


            categoryMoneyViewHolder = new CategoryMoneyViewHolder();


            categoryMoneyViewHolder.moneyNameTextView = (RDATextView) row.findViewById(R.id.category_money_textview_money_name);
            categoryMoneyViewHolder.moneyThumbImageView = (ImageView) row.findViewById(R.id.category_money_imageview_money_thumb);
            categoryMoneyViewHolder.detailTextView = (RDATextView) row.findViewById(R.id.category_money_textview_details);


            row.setTag(categoryMoneyViewHolder);


        } else {


            categoryMoneyViewHolder = (CategoryMoneyViewHolder) row.getTag();
        }


        categoryMoneyViewHolder.moneyNameTextView.setText(moneyItem.getName());


        row.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {


                new MoneyDetailFragment().open(activity, PassingDataKeyConstants.MONEY, moneyItem);


            }
        });





        final String URLString = BaseService.MONEY_PICTURE + moneyItem.getFront_thumb();


        Target target = new Target() {


            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {


                if (SettingsForEnablesConstants.ENABLE_PICASSO_LOG) {


                    RDALogger.debug("onBitmapLoaded by : " + position + " " + URLString);
                }


                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) categoryMoneyViewHolder.moneyThumbImageView.getLayoutParams();

                double screenWidth = RDADeviceHelpers.getScreenWidth(activity) - RDAResourcesHelpers.getDimen(activity, R.dimen._20dp) * 2;

                double ratioFloat = screenWidth / bitmap.getWidth();

                layoutParams.height = (int) (ratioFloat * bitmap.getHeight());

                layoutParams.width = (int) screenWidth;

                categoryMoneyViewHolder.moneyThumbImageView.setLayoutParams(layoutParams);
                //---------gelen resme gore para gorseli boyutlandirilmasi


                categoryMoneyViewHolder.moneyThumbImageView.setImageBitmap(bitmap);
            }


            @Override
            public void onBitmapFailed(Drawable errorDrawable) {


                if (SettingsForEnablesConstants.ENABLE_PICASSO_LOG) {


                    RDALogger.error("onBitmapFailed by :" + URLString);
                }
            }


            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {


                if (SettingsForEnablesConstants.ENABLE_PICASSO_LOG) {


                    RDALogger.debug("onPrepareLoad by :" + URLString);
                }
            }
        };


        targets.add(target);


        Picasso.with(activity).load(URLString).into(target);


    } else {


    }


    return row;
}

现在一切都很完美。但我的服务器有sequrity标头来加载图像。所以我需要使用毕加索下载器。这是代码;

final Picasso.Builder builder = new Picasso.Builder(activity);


        builder.downloader(new UrlConnectionDownloader(activity) {


            @Override
            protected HttpURLConnection openConnection(Uri uri) throws IOException {


                HttpURLConnection connection = super.openConnection(uri);

                for (Map.Entry entry : RequestHelpers.getRequestHeadersMap().entrySet()) {


                    connection.setRequestProperty(entry.getKey().toString(), entry.getValue().toString());
                }


                return connection;
            }
        });


        Picasso picasso = builder.build();

        picasso.setLoggingEnabled(SettingsForEnablesConstants.ENABLE_PICASSO_LOG);


        picasso.load(URLString).into(target);

当我把这个下载程序代码放入适配器时。每个图像在滚动时反复加载。并且卡在滚动中。我不明白为什么以及哪里出错了?

有什么意见吗?

谢谢你们。

0 个答案:

没有答案