当我的设备在线时,RecyClerView适配器可以按预期工作。当离线时,未调用onCreateviewHolder。
首先,我知道这一点:recyclerview不调用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));
}
}
}
这些都与网络无关,这告诉我,当您的网络状态更改时,在称为movies
中传递的数组可能正在从另一个类中更改。祝你好运。
在离线模式下,毕加索库无法下载图像,在这种情况下,图像结果将无效。您可能需要为毕加索设置网络电池,类似于以下内容: - 如果有网络或不尝试从高速缓存下载图像,如果失败,则从网络下载。这是代码段: -
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);
}
});