当我的设备在线时,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));
}
}
}
答案 0 :(得分:1)
这与网络没有任何关系,它告诉我,当您的网络状态发生变化时,您传入的名为movies
的数组可能正在从另一个类中更改。祝你好运。
答案 1 :(得分:0)
在离线模式下,Picasso库无法下载图像,在这种情况下,Image结果将为null。您可能需要为Picasso设置NetworkPolicy,如下所示: - 如果有网络或者首先尝试从缓存中下载映像,如果失败则从网络下载。以下是代码段: -
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);
}
});