缩略图在 RecyclerView (Android) 中保持空白。我正确使用 Glide 吗?
Posted
技术标签:
【中文标题】缩略图在 RecyclerView (Android) 中保持空白。我正确使用 Glide 吗?【英文标题】:Thumbnail stays blank in RecyclerView (Android). Am I using Glide correctly? 【发布时间】:2021-04-27 07:25:07 【问题描述】:我正在开发一个 android 应用程序,该应用程序显示与搜索词匹配的书籍列表。我正在使用 Google 图书 API。从 API 中,我得到了以下项目的列表:标题、作者、出版日期和要显示的小缩略图的 URI。
我能够在 RecyclerView 中显示除缩略图之外的信息。为此,我正在尝试使用 Glide 库。它始终保持空白,如果我使用占位符选项,它始终显示占位符。 (我也在 Gradle 文件中添加了依赖项)。
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
我已经尝试调试,并且确实变量mThumbnail
不为空并且具有有效地址。这是使用 Glide 制作缩略图的正确方法吗?
我将在 RecyclerView 项和适配器代码的布局下方发布。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/book_thumbnail"
android:layout_
android:layout_
<!--app:layout_constraintDimensionRatio = "4:5.5"-->
/>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="@+id/title"
android:layout_
android:layout_
tools:text="Le petit prince" />
<TextView
android:id="@+id/authors"
android:layout_
android:layout_
tools:text="Antoine-Saint Exupery" />
<TextView
android:id="@+id/publishedDate"
android:layout_
android:layout_
tools:text="1943" />
</LinearLayout>
</LinearLayout>
以及适配器代码:
package com.example.booksearch;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Adapter used for RecyclerView
*/
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.BookViewHolder>
private LayoutInflater mInflater;
private final List<Book> mBookList;
Context context;
/**
* BookListAdapter constructor.
* @param context Takes context of applications
* @param bookList A list of books.
*/
public BookListAdapter(Context context, ArrayList<Book> bookList)
mInflater = LayoutInflater.from(context);
this.mBookList = bookList;
this.context = context;
/**
* Empties adapter.
*/
public void clear()
mBookList.clear();
/**
* Adds all books to adapter.
* @param books List of books to add.
*/
public void addAll(ArrayList<Book> books)
mBookList.addAll(books);
/**
* Used on creating of BookViewHolder
* @param parent BookViewHolder
* @param viewType
* @return A BookViewHolder object
*/
@NonNull
@Override
public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View mItemView = mInflater.inflate(R.layout.book_item,parent, false);
return new BookViewHolder(mItemView, this);
/**
* Binds (sets content of ViewHolder)
* @param holder Holder to work on
* @param position Position of the book that belongs to the ViewHolder
*/
@Override
public void onBindViewHolder(@NonNull BookListAdapter.BookViewHolder holder, int position)
String mTitle = mBookList.get(position).getTitle();
String mAuthor = mBookList.get(position).getAuthor();
String mPublishedDate = mBookList.get(position).getPublishedYear();
Uri mThumbnailUri = mBookList.get(position).getThumbnailUri();
holder.titleView.setText(mTitle);
holder.authorView.setText(mAuthor);
holder.publishedYearView.setText(mPublishedDate);
if (!mThumbnailUri.toString().isEmpty())
Glide.with(context)
.load(mThumbnailUri.toString())
.override(100,200)
//.placeholder(R.drawable.ic_baseline_book_24)
.into(holder.thumbnailView);
else
Glide.with(context)
.load(R.drawable.ic_baseline_book_24)
.into(holder.thumbnailView);
holder.thumbnailView.setImageAlpha(50);
/**
* Returns size of book list.
* @return An integer with size of the list.
*/
@Override
public int getItemCount()
return mBookList.size();
/**
* Inner class of BookListAdapter, for ViewHolder of book object.
*/
class BookViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
public final TextView titleView;
public final TextView authorView;
public final TextView publishedYearView;
public final ImageView thumbnailView;
final BookListAdapter mAdapter;
/**
* Constructor of BookViewHolder object.
* @param itemView The itemView for one book object.
* @param adapter The adapter that is used for book objects.
*/
public BookViewHolder(View itemView, BookListAdapter adapter)
super(itemView);
titleView = itemView.findViewById(R.id.title);
authorView = itemView.findViewById(R.id.authors);
publishedYearView = itemView.findViewById(R.id.publishedDate);
thumbnailView = itemView.findViewById(R.id.book_thumbnail);
//thumbnailView.setScaleType(ImageView.ScaleType.CENTER);
this.mAdapter = adapter;
itemView.setOnClickListener(this);
@Override
public void onClick(View v)
【问题讨论】:
您需要允许 HTTP 明文流量。有关更多详细信息,您可以参考此线程/答案***.com/a/50834600/4491576 【参考方案1】:我还没有足够的声誉来发表评论,所以我可以询问您的 api 结果吗?和缩略图的日志?
我现在假设你的 json 或任何你的 api 结果,返回的缩略图是字符串,如果它是一个字符串,那么你只需要将它加载到滑翔,但当然首先将你的模型重构为字符串 所以你的代码
Uri mThumbnailUri = mBookList.get(position).getThumbnailUri()
成为
String mThumbnail = mBookList.get(position).getThumbnail()
Glide.with(context).load(mThumbnail)
因为 glide 可以将 String 作为参数
【讨论】:
【参考方案2】:由于在 Android(9.0 及更高版本)中默认禁用明文支持,图像无法正确加载。在查看日志并发现以下内容后:
2021-01-22 20:45:02.702 25417-25417/com.example.booksearch W/Glide: Load failed for http://books.google.com/books/content?id=EJuKSkd5K94C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api with size [60x150]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.io.IOException(Cleartext HTTP traffic to books.google.com not permitted)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
There was 1 cause:
java.io.IOException(Cleartext HTTP traffic to books.google.com not permitted)
call GlideException#logRootCauses(String) for more detail
我将以下内容添加到 Android Manifest.xml 文件(在应用程序部分):
<applicaton
...
android:usesCleartextTraffic="true"
...>
</application>
【讨论】:
以上是关于缩略图在 RecyclerView (Android) 中保持空白。我正确使用 Glide 吗?的主要内容,如果未能解决你的问题,请参考以下文章
markdown 使用Timmy时,缩略图在媒体页面上加载速度慢
C#listView控件如何获得缩略图的URL,并把缩略图在另个窗体中显示为大图?跪求啊!!!!