[英]Binding adapter used to display images from URL using Picasso
我在Github有一个项目: https : //github.com/Ali-Rezaei/News-Cache
它包括以下Binding适配器:
@BindingAdapter("imageUrl")
fun bindImage(cardView: CardView, url: String) {
val imageView: ImageView = cardView.findViewById(R.id.article_poster)
Picasso.with(cardView.context).load(url).into(
imageView,
PicassoPalette.with(url, imageView)
.use(PicassoPalette.Profile.VIBRANT)
.intoBackground(cardView.findViewById(R.id.article_background), PicassoPalette.Swatch.RGB)
.intoTextColor(cardView.findViewById(R.id.article_name), PicassoPalette.Swatch.BODY_TEXT_COLOR)
)
}
这是我的NewsAdapter:
class NewsAdapter : ListAdapter<Article, NewsAdapter.NewsViewHolder>(DiffCallback) {
/**
* Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
* an item.
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = NewsViewHolder.from(parent)
/**
* Called by RecyclerView to display the data at the specified position. This method should
* update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
* position.
*/
override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
holder.bind(getItem(position))
}
/**
* ViewHolder for News items. All work is done by data binding.
*/
class NewsViewHolder(val binding: NewsItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(news: Article) {
with(binding) {
article = news
executePendingBindings()
}
}
companion object {
fun from(parent: ViewGroup): NewsViewHolder {
val binding = NewsItemBinding.inflate(
parent.context.layoutInflater,
parent,
false
)
return NewsViewHolder(binding)
}
}
}
/**
* Allows the RecyclerView to determine which items have changed when the [List] of [Article]
* has been updated.
*/
companion object DiffCallback : DiffUtil.ItemCallback<Article>() {
override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem === newItem
}
override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem.url == newItem.url
}
}
}
这是我在Fragment中观察到的LiveData:
viewModel.news.observe(viewLifecycleOwner, Observer<List<Article>> { articles ->
articles?.apply {
viewModelAdapter?.submitList(articles)
}
})
以下是我如何设置recyclerView适配器:
viewModelAdapter = NewsAdapter()
with(binding) {
recyclerView.apply {
addItemDecoration(MarginDecoration(context))
setHasFixedSize(true)
adapter = viewModelAdapter
}
}
出于某种原因,当我将recyclerView滚动到end并返回到开始时,它开始再次加载图像。 我希望它能被缓存。 我的错是什么?
用它修改它
//10MB Cache
Picasso picasso = new Picasso
.Builder(this)
.downloader(new OkHttpDownloader(getCacheDir(), 10000000))
.build();
在应用程序启动时创建此实例,并在不重新创建的情况下使用此实例。
picasso.with(cardView.context).load(url).networkPolicy(NetworkPolicy.OFFLINE).into(
imageView,
PicassoPalette.with(url, imageView)
.use(PicassoPalette.Profile.VIBRANT)
.intoBackground(cardView.findViewById(R.id.article_background), PicassoPalette.Swatch.RGB)
.intoTextColor(cardView.findViewById(R.id.article_name), PicassoPalette.Swatch.BODY_TEXT_COLOR)
)
我认为你应该指定缓存机制如下:
Picasso.with(cardView.context)
.networkPolicy(OFFLINE, NO_CACHE)
.load(url)
.into(view)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.