我有无尽的recyclervIEw,几乎可以模仿Instagram列表/提要. Image正在加载到全屏宽度ImagevIEw中.我正在使用Picasso加载图像.这是我的代码:
public class HomeAdapter extends RecyclerVIEw.Adapter {Context context;private Contract contract;List<Feed> Feeds;static final int ITEM_TYPE_header = 0;static final int ITEM_TYPE_LOAD_MORE = 1;static final int ITEM_TYPE_DESIGN = 2;public HomeAdapter(Context context, List<Feed> Feeds) { this.context = context; this.Feeds = Feeds;}@OverrIDepublic RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); VIEw row; if (vIEwType == ITEM_TYPE_header) { row = inflater.inflate(R.layout.List_item_home_header, parent, false); return new headerHolder(row); } else if (vIEwType == ITEM_TYPE_LOAD_MORE) { row = inflater.inflate(R.layout.List_item_load_more, parent, false); return new LoadMoreHolder(row); } else if (vIEwType == ITEM_TYPE_DESIGN) { row = inflater.inflate(R.layout.List_item_Feed_home, parent, false); return new DesignItemHolder(row); } return null;}@OverrIDepublic voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder, final int position) { Feed Feed = Feeds.get(position); if (holder instanceof headerHolder) { headerHolder headerHolder = (headerHolder) holder; header header = (header) Feed; headerHolder.tvTitle.setText(header.getTitle()); headerHolder.tvDescription.setText(header.getDescription()); } else if (holder instanceof LoadMoreHolder) { LoadMoreHolder loadMoreHolder = (LoadMoreHolder) holder; LoadMore loadMore = (LoadMore) Feed; if(loadMore.getType() == LOAD_MORE_TYPE_PROGRESS) { loadMoreHolder.pbLoadMore.setVisibility(VIEw.VISIBLE); loadMoreHolder.rlMessageRetry.setVisibility(VIEw.GONE); } else { loadMoreHolder.pbLoadMore.setVisibility(VIEw.GONE); loadMoreHolder.rlMessageRetry.setVisibility(VIEw.VISIBLE); if(StringUtils.isValID(loadMore.getTitle())) { loadMoreHolder.tvTitle.setText(loadMore.getTitle()); loadMoreHolder.tvTitle.setVisibility(VIEw.VISIBLE); } else loadMoreHolder.tvTitle.setVisibility(VIEw.GONE); if(StringUtils.isValID(loadMore.getDescription())) { loadMoreHolder.tvDescription.setText(loadMore.getDescription()); loadMoreHolder.tvDescription.setVisibility(VIEw.VISIBLE); } else loadMoreHolder.tvDescription.setVisibility(VIEw.GONE); } loadMoreHolder.btnRetry.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onRetryLoadMoreClick(); } }); } else if (holder instanceof DesignItemHolder) { final DesignItemHolder designItemHolder = (DesignItemHolder) holder; final DesignFeed designFeed = (DesignFeed) Feed; String Title = getFeedTitle(designFeed.getDesigncategory().getTitle(), designFeed.getEvent().getTitle()); designItemHolder.tvcategory.setText(StringUtils.toupperCaseSentence(designFeed.getDesigncategory().getTitle())); designItemHolder.tvDate.setText(DateTimeUtils.formatTime(designFeed.getDate())); designItemHolder.tvTitle.setText(StringUtils.toupperCaseSentence(Title)); designItemHolder.tvBrand.setText(getBrandTitle(designFeed.getBrand().getTitle())); if(StringUtils.isValID(designFeed.getShoplink())) { designItemHolder.tvShop.setVisibility(VIEw.VISIBLE); if(StringUtils.isValID(designFeed.getPrice())) { designItemHolder.tvPrice.setVisibility(VIEw.VISIBLE); designItemHolder.tvPrice.setText(designFeed.getPrice() + " PKR"); } else designItemHolder.tvPrice.setVisibility(VIEw.GONE); designItemHolder.tvShop.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onopenShoplink(designFeed.getShoplink()); } }); } else { designItemHolder.tvPrice.setVisibility(VIEw.GONE); designItemHolder.tvShop.setVisibility(VIEw.GONE); } showFeedImage(designFeed, designItemHolder.ivthumbnail, designItemHolder.progressbar); Picasso.get() .load(designFeed.getDesigncategory().getthumbnail()) .transform(new Circletransform()) .placeholder(new colorDrawable(ContextCompat.getcolor(context, R.color.colorGreylight))) .into(designItemHolder.ivcategory); designItemHolder.ivthumbnail.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onDesignFeedClick(designFeed); } }); designItemHolder.tvcategory.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onDesigncategoryClick(designFeed.getDesigncategory()); } }); designItemHolder.ivcategory.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onDesigncategoryClick(designFeed.getDesigncategory()); } }); designItemHolder.tvTitle.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onDesigncategoryClick(designFeed.getDesigncategory()); } }); designItemHolder.tvBrand.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onBrandClick(designFeed.getBrand()); } }); designItemHolder.ibFavourite.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onFavouriteClick(designFeed); } }); designItemHolder.ibShare.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onShareClick(designFeed); } }); designItemHolder.ibDownload.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(contract != null) contract.onDownloadClick(designFeed); } }); }}private voID showFeedImage(final DesignFeed designFeed, final ImageVIEw ivthumbnail, final Progressbar progressbar) { Picasso.get() .load(designFeed.getthumbnail()) .placeholder(new colorDrawable(ContextCompat.getcolor(context, R.color.colorGreylight))) .networkPolicy(NetworkPolicy.OFFliNE) .into(ivthumbnail, new Callback() { @OverrIDe public voID onSuccess() { } @OverrIDe public voID one rror(Exception e) { //Try again online if cache Failed Picasso.get() .load(designFeed.getthumbnail()) .placeholder(new colorDrawable(ContextCompat.getcolor(context, R.color.colorGreylight))) .into(ivthumbnail, new Callback() { @OverrIDe public voID onSuccess() { } @OverrIDe public voID one rror(Exception e) { } }); } });}@OverrIDepublic int getItemVIEwType(int position) { Feed Feed = Feeds.get(position); if (Feed instanceof header) return ITEM_TYPE_header; else if (Feed instanceof LoadMore) return ITEM_TYPE_LOAD_MORE; else if (Feed instanceof DesignFeed) return ITEM_TYPE_DESIGN; else return super.getItemVIEwType(position);}@OverrIDepublic int getItemCount() { return Feeds.size();}public voID insertFeedAtStart(Feed Feed) { Feeds.add(0, Feed); notifyDataSetChanged();}public voID updateFeeds(ArrayList<Feed> mFeeds) { Feeds.addAll(mFeeds); notifyDataSetChanged();}public voID addLoadMoreItem(Feed loadMoreFeed) { Feeds.add(loadMoreFeed); notifyItemInserted(getItemCount() - 1);}public voID removeItem(int index) { Feeds.remove(index); notifyDataSetChanged();}private class DesignItemHolder extends RecyclerVIEw.VIEwHolder { TextVIEw tvTitle, tvcategory, tvBrand, tvDate, tvPrice, tvShop; ImageVIEw ivcategory, ivthumbnail; Imagebutton ibFavourite, ibShare, ibDownload; Progressbar progressbar; DesignItemHolder(VIEw itemVIEw) { super(itemVIEw); tvTitle = itemVIEw.findVIEwByID(R.ID.tvTitle); tvcategory = itemVIEw.findVIEwByID(R.ID.tvcategory); tvBrand = itemVIEw.findVIEwByID(R.ID.tvBrand); tvDate = itemVIEw.findVIEwByID(R.ID.tvDate); ivcategory = itemVIEw.findVIEwByID(R.ID.ivcategory); ivthumbnail = itemVIEw.findVIEwByID(R.ID.ivthumbnail); tvPrice = itemVIEw.findVIEwByID(R.ID.tvPrice); tvShop = itemVIEw.findVIEwByID(R.ID.tvShop); ibFavourite = itemVIEw.findVIEwByID(R.ID.ibFavourite); ibShare = itemVIEw.findVIEwByID(R.ID.ibShare); ibDownload = itemVIEw.findVIEwByID(R.ID.ibDownload); progressbar = itemVIEw.findVIEwByID(R.ID.progressbar); }}private class headerHolder extends RecyclerVIEw.VIEwHolder { TextVIEw tvTitle, tvDescription; headerHolder(VIEw itemVIEw) { super(itemVIEw); tvTitle = itemVIEw.findVIEwByID(R.ID.tvTitle); tvDescription = itemVIEw.findVIEwByID(R.ID.tvDescription); }}private class LoadMoreHolder extends RecyclerVIEw.VIEwHolder { Progressbar pbLoadMore; relativeLayout rlMessageRetry; TextVIEw tvTitle, tvDescription; AppCompatbutton btnRetry; LoadMoreHolder(VIEw itemVIEw) { super(itemVIEw); pbLoadMore = itemVIEw.findVIEwByID(R.ID.pbLoadMore); rlMessageRetry = itemVIEw.findVIEwByID(R.ID.rlMessageRetry); tvTitle = itemVIEw.findVIEwByID(R.ID.tvTitle); tvDescription = itemVIEw.findVIEwByID(R.ID.tvDescription); btnRetry = itemVIEw.findVIEwByID(R.ID.btnRetry); }}public voID setContract(Contract contract) { this.contract = contract;}public interface Contract { voID onDesigncategoryClick(Designcategory designcategory); voID onDesignFeedClick(DesignFeed designcategory); voID onBrandClick(Brand brand); voID onRetryLoadMoreClick(); voID onShopClick(String shoplink); voID onFavouriteClick(DesignFeed designFeed); voID onShareClick(DesignFeed designFeed); voID onDownloadClick(DesignFeed designFeed); voID onopenShoplink(String shoplink);}
}
平均图像大小为80-100 kbs.但是图像下载时间太慢.下载图像大约需要3-4秒.另一方面,从高速缓存加载图像时,显然可以立即加载图像.我从服务器获得15项内容以加载到recyclervIEw中,这意味着将并行下载15张图像.但与Instagram或Facebook提要相比,它仍然相当慢.
谁能指导我如何尽快使之正确的技术,以便当用户向下滚动列表时,应该已经加载了图像,而不是他等待图像加载?
解决方法:
实际上,我使用lib已有很长时间了,没有任何网络问题.我试图分析代码,发现以下内容:
>问题可能是转换由于lib首先转换输入的图像然后显示,请参阅此部分
Picasso.get()
.load(designFeed.getDesigncategory().getthumbnail()) .transform(new Circletransform()) .placeholder(new colorDrawable(ContextCompat.getcolor(context, R.color.colorGreylight))) .into(designItemHolder.ivcategory);
建议:如果您要以圆形视图显示图像.然后使用CircularImageVIEw,不要释放I / O.
>使用其他功能强大的设备检查性能
编辑
>检查您的手机的网络使用情况,正在消耗哪个应用程序,多少兆字节,因为某些应用程序在后台静默使用您的网络
>为什么在您的浏览器中速度很快?因为您的浏览器未对图像进行任何额外的 *** 作
以上是内存溢出为你收集整理的Android Picasso Recyclerview图像下载缓慢全部内容,希望文章能够帮你解决Android Picasso Recyclerview图像下载缓慢所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)