导航后未附加回收站视图
Recycler view is not attached after navigation
我正在使用底部导航视图开发此应用程序。在我的主页片段上,我使用了两个回收器 views.i 正在获取一些数据和图像以显示在主屏幕片段上。在应用程序启动时,图像在两个回收器视图中都可见,但如果我旋转屏幕或导航到其他片段,图像从第二个回收器视图中消失,而第一个回收器视图仍在显示图像。
下面是代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mFavSubCatDatabase =
FirebaseDatabase.getInstance().getReference().child("FavSubCat");
mFavSubCatDatabase.keepSynced(true);
mFavCatDatabase =
FirebaseDatabase.getInstance().getReference().child("FavCat");
mFavCatDatabase.keepSynced(true);
// mLayoutManager = new LinearLayoutManager(getContext());
RecyclerView.LayoutManager mLayoutManager = new
GridLayoutManager(getContext(), 3);
mFavSubCatList = (RecyclerView)
view.findViewById(R.id.HomesubcatOpeningRecler);
mFavSubCatList.setHasFixedSize(true);
mFavSubCatList.setLayoutManager(mLayoutManager);
LinearLayoutManager mLayoutManager2
= new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
mFavCatList = (RecyclerView)
view.findViewById(R.id.HomeCatRecyclerOpening);
mFavCatList.setHasFixedSize(true);
mFavCatList.setLayoutManager(mLayoutManager2);
return view;
}
@Override
public void onStart() {
super.onStart();
startListening();
}
public void startListening()
{
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("FavCat")
.limitToLast(50);
FirebaseRecyclerOptions<HomeCatitems> options =
new FirebaseRecyclerOptions.Builder<HomeCatitems>()
.setQuery(query, HomeCatitems.class)
.build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<HomeCatitems, CategoryViewHolder>(options) {
@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_cat_layout, parent, false);
return new CategoryViewHolder(view);
}
@Override
protected void onBindViewHolder(final CategoryViewHolder holder, int
position, final HomeCatitems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
}
};
mFavCatList.setAdapter(adapter);
adapter.startListening();
Query query1 = FirebaseDatabase.getInstance()
.getReference()
.child("FavSubCat")
.limitToLast(50);
FirebaseRecyclerOptions<CategoryItems> options1 =
new FirebaseRecyclerOptions.Builder<CategoryItems>()
.setQuery(query1, CategoryItems.class)
.build();
FirebaseRecyclerAdapter adaptersubcat = new
FirebaseRecyclerAdapter<CategoryItems, SubCategoryViewHolder>(options1)
{
@Override
public SubCategoryViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_subcat_items, parent, false);
return new SubCategoryViewHolder(view);
}
@Override
protected void onBindViewHolder(final SubCategoryViewHolder holder,
int position, final CategoryItems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
}
};
mFavSubCatList.setAdapter(adaptersubcat);
adaptersubcat.startListening();
}
其中一个内部viewHolder class如下所示
public 静态 class SubCategoryViewHolder 扩展
RecyclerView.ViewHolder {
View mView;
ImageView subcategoryImageView;
TextView subcategoryName;
public SubCategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name){
subcategoryName = (TextView)
mView.findViewById(R.id.home_subcat_name);
subcategoryName.setText(name);
}
public void setUserImage(final String thumb_image, final Context ctx){
subcategoryImageView = (ImageView)
mView.findViewById(R.id.home_subcat_image);
Picasso.with(ctx).setIndicatorsEnabled(false);
Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.default_avatar).into(subcategoryImageView, new
Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar)
.into(subcategoryImageView);
}
});
}
}
尝试使用onResume
方法在旋转后再次将图像设置到图像视图中。或者只使用 onSavedInstanceState
并将图像重新分配到 imageview 中,如这个问题中所回答的:ImageView not retaining Image when Screen rotation occurs
正如我在评论中提到的,这似乎与 ViewHolder
实现或 Picasso 的缓存有关。您的代码中最可疑的一点是:
Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE);
您可能想删除那里的网络策略声明,是否有理由让您首先使用错误回调来加载图像? Picasso 会自动为您执行此操作(如果无法从网络获取,则从 memory/disk 缓存中获取)。尝试删除它并仅使用以下方式加载图像:
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(subcategoryImageView);
编辑:另外,如果您需要更多信息,这里是 NetworkPolicy
的文档:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/NetworkPolicy.html.
经过大量搜索后,我发现我正在使用 setHasFixedSize(true) 和 wrap_content 回收站视图。我删除了 setHasFixedSize(true),它现在工作得很好。
我正在使用底部导航视图开发此应用程序。在我的主页片段上,我使用了两个回收器 views.i 正在获取一些数据和图像以显示在主屏幕片段上。在应用程序启动时,图像在两个回收器视图中都可见,但如果我旋转屏幕或导航到其他片段,图像从第二个回收器视图中消失,而第一个回收器视图仍在显示图像。
下面是代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mFavSubCatDatabase =
FirebaseDatabase.getInstance().getReference().child("FavSubCat");
mFavSubCatDatabase.keepSynced(true);
mFavCatDatabase =
FirebaseDatabase.getInstance().getReference().child("FavCat");
mFavCatDatabase.keepSynced(true);
// mLayoutManager = new LinearLayoutManager(getContext());
RecyclerView.LayoutManager mLayoutManager = new
GridLayoutManager(getContext(), 3);
mFavSubCatList = (RecyclerView)
view.findViewById(R.id.HomesubcatOpeningRecler);
mFavSubCatList.setHasFixedSize(true);
mFavSubCatList.setLayoutManager(mLayoutManager);
LinearLayoutManager mLayoutManager2
= new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
mFavCatList = (RecyclerView)
view.findViewById(R.id.HomeCatRecyclerOpening);
mFavCatList.setHasFixedSize(true);
mFavCatList.setLayoutManager(mLayoutManager2);
return view;
}
@Override
public void onStart() {
super.onStart();
startListening();
}
public void startListening()
{
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("FavCat")
.limitToLast(50);
FirebaseRecyclerOptions<HomeCatitems> options =
new FirebaseRecyclerOptions.Builder<HomeCatitems>()
.setQuery(query, HomeCatitems.class)
.build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<HomeCatitems, CategoryViewHolder>(options) {
@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_cat_layout, parent, false);
return new CategoryViewHolder(view);
}
@Override
protected void onBindViewHolder(final CategoryViewHolder holder, int
position, final HomeCatitems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
}
};
mFavCatList.setAdapter(adapter);
adapter.startListening();
Query query1 = FirebaseDatabase.getInstance()
.getReference()
.child("FavSubCat")
.limitToLast(50);
FirebaseRecyclerOptions<CategoryItems> options1 =
new FirebaseRecyclerOptions.Builder<CategoryItems>()
.setQuery(query1, CategoryItems.class)
.build();
FirebaseRecyclerAdapter adaptersubcat = new
FirebaseRecyclerAdapter<CategoryItems, SubCategoryViewHolder>(options1)
{
@Override
public SubCategoryViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_subcat_items, parent, false);
return new SubCategoryViewHolder(view);
}
@Override
protected void onBindViewHolder(final SubCategoryViewHolder holder,
int position, final CategoryItems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
}
};
mFavSubCatList.setAdapter(adaptersubcat);
adaptersubcat.startListening();
}
其中一个内部viewHolder class如下所示 public 静态 class SubCategoryViewHolder 扩展 RecyclerView.ViewHolder {
View mView;
ImageView subcategoryImageView;
TextView subcategoryName;
public SubCategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name){
subcategoryName = (TextView)
mView.findViewById(R.id.home_subcat_name);
subcategoryName.setText(name);
}
public void setUserImage(final String thumb_image, final Context ctx){
subcategoryImageView = (ImageView)
mView.findViewById(R.id.home_subcat_image);
Picasso.with(ctx).setIndicatorsEnabled(false);
Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE)
.placeholder(R.drawable.default_avatar).into(subcategoryImageView, new
Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar)
.into(subcategoryImageView);
}
});
}
}
尝试使用onResume
方法在旋转后再次将图像设置到图像视图中。或者只使用 onSavedInstanceState
并将图像重新分配到 imageview 中,如这个问题中所回答的:ImageView not retaining Image when Screen rotation occurs
正如我在评论中提到的,这似乎与 ViewHolder
实现或 Picasso 的缓存有关。您的代码中最可疑的一点是:
Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE);
您可能想删除那里的网络策略声明,是否有理由让您首先使用错误回调来加载图像? Picasso 会自动为您执行此操作(如果无法从网络获取,则从 memory/disk 缓存中获取)。尝试删除它并仅使用以下方式加载图像:
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(subcategoryImageView);
编辑:另外,如果您需要更多信息,这里是 NetworkPolicy
的文档:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/NetworkPolicy.html.
经过大量搜索后,我发现我正在使用 setHasFixedSize(true) 和 wrap_content 回收站视图。我删除了 setHasFixedSize(true),它现在工作得很好。