导航后未附加Recycler视图

时间:2018-05-19 12:01:10

标签: android android-recyclerview fragment cardview

我正在开发这个应用程序,我使用了底部导航视图。在我的家庭片段我已经使用了两个回收者视图。我正在获取一些数据和图像以显示在主屏幕片段上。在应用程序启动时,图像在两个回收站视图中都可见,但如果我旋转屏幕或导航到其他片段,则第一个回收站视图仍在显示图像时,图像将从第二个回收站视图中消失。

下面是代码

  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类         公共静态类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);

            }
        });

    }



}

3 个答案:

答案 0 :(得分:0)

尝试使用onResume方法在旋转后再次将图像设置为imageview。或者只使用onSavedInstanceState并将图片重新分配到imageview中,如以下问题所示:ImageView not retaining Image when Screen rotation occurs

答案 1 :(得分:0)

正如我在评论中提到的,这似乎与ViewHolder实现或Picasso的缓存有关。你的代码中最可疑的是:

Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE);

您可能希望在那里删除网络策略声明,您是否有理由首先使用该错误回调来加载您的图像? Picasso会自动为您完成此操作(从内存/磁盘缓存中获取,如果网络不可用)。尝试删除它并仅使用以下方式加载图像:

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

答案 2 :(得分:0)

经过大量搜索后,我发现我正在使用setHasFixedSize(true)和recycler视图的wrap_content。我删除了setHasFixedSize(true),它现在完全正常。