离线时Recyclerview不调用onCreateViewHolder
Posted
技术标签:
【中文标题】离线时Recyclerview不调用onCreateViewHolder【英文标题】:Recyclerview not call onCreateViewHolder when offline 【发布时间】:2018-02-14 16:22:03 【问题描述】:当我的设备在线时,RecyclerView 适配器按预期工作。离线时,不会调用 onCreateViewHolder。
首先,我知道这一点:Recyclerview not call onCreateViewHolder 这不是问题所在。 getItemCount() 返回一个 >0 的数字,并且我在调用活动中正确设置了适配器和布局管理器的顺序:
mAdapter = new MovieAdapter(this, this, movies, mScreenWidthPx, mIsFavorites);
mMovieRecyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
mMovieRecyclerView.setAdapter(mAdapter);
所以,它似乎与在线/离线状态有关,但我不知道如何。这是适配器:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
private static final String TAG = MovieAdapter.class.getSimpleName();
private ArrayList<Movie> mMovies;
private Context mContext;
private MovieClickListener mClickListener;
private int mScreenWidthPx;
private boolean mIsFavorite;
public interface MovieClickListener
void onMovieClick(Movie movie);
public MovieAdapter(Context context, MovieClickListener movieClickListener, ArrayList<Movie> movies, int screenWidthPx, boolean isFavorite)
Log.d(TAG,"creating MovieAdapter");
this.mContext = context;
this.mMovies = movies;
this.mClickListener = movieClickListener;
this.mScreenWidthPx = screenWidthPx;
this.mIsFavorite = isFavorite;
Log.d(TAG,"mMovies size constructor:" + mMovies.size());
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
Log.d(TAG,"onCreateViewHolder");
View view = LayoutInflater.from(mContext).inflate(R.layout.item_movie, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(ViewHolder holder, int position)
Log.d(TAG,"onBindViewHolder");
if (mIsFavorite)
Log.d(TAG,"loading local image file");
String fileName = ImageUtils.getMoviePosterFileName(mMovies.get(position).getTitle());
File imageFile = ImageUtils.getImageFile(mContext,fileName);
Picasso.with(mContext)
.load(imageFile)
.into(holder.mImagePoster);
else
Log.d(TAG,"loading remote image file");
String posterUrl = NetworkUtils.buildMoviePosterUrl(mMovies.get(position).getPosterPath(), mScreenWidthPx);
Picasso.with(mContext)
.load(posterUrl)
.into(holder.mImagePoster);
@Override
public int getItemCount()
Log.d(TAG,"mMovies size getCount:" + mMovies.size());
return mMovies.size();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
@BindView(R.id.image_movie_poster) ImageView mImagePoster;
ViewHolder(View itemView)
super(itemView);
Log.d(TAG,"creating ViewHolder");
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
int adapterPosition = getAdapterPosition();
mClickListener.onMovieClick(mMovies.get(adapterPosition));
【问题讨论】:
您是否为毕加索设置了任何网络政策? 【参考方案1】:这与网络无关,它告诉我当您的网络状态发生变化时,您传入的名为movies
的数组可能正在从另一个类更改。祝你好运。
【讨论】:
【参考方案2】:在离线模式下,毕加索库无法下载图像,在这种情况下,图像结果将为空。您可能需要为 Picasso 设置 NetworkPolicy,如下所示:- 如果有网络或不尝试先从缓存下载图像,如果失败则从网络下载。这是代码sn-p:-
Picasso.with(context)
.load(Uri.parse(getItem(position).getStoryBigThumbUrl()))
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.storyBigThumb, new Callback()
@Override
public void onSuccess()
@Override
public void onError()
// Try again online if cache failed
Picasso.with(context)
.load(Uri.parse(getItem(position)
.getStoryBigThumbUrl()))
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(holder.storyBigThumb);
);
【讨论】:
以上是关于离线时Recyclerview不调用onCreateViewHolder的主要内容,如果未能解决你的问题,请参考以下文章