当RecyclerView不为空时,相对布局未显示

时间:2018-04-01 20:18:50

标签: android android-recyclerview

我有一个firebaseRecyclerAdapter,如果recyclerview为空,我试图隐藏相对布局。问题是相对布局已经消失,但是当我将项目添加到回收站视图时,它不再显示。我尝试了很多东西而无法让它发挥作用。

mModelList = new ArrayList<>();
        mDairyDatabase = FirebaseDatabase.getInstance().getReference().child("ListFragment").child("Dairy");
        Query query = mDairyDatabase;
        mDairyDatabase.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                HashMap<String, String> value = (HashMap<String, String>) dataSnapshot.getValue();
                if(value != null){
                    String name = value.get("Name");
                    String key = value.get("Key");
                    mModelList.add(new Model(name, key));
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        mRecyclerView = view.findViewById(R.id.recycler_view1);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(manager);

//skipping random code

mRecyclerView.setAdapter(mDairyAdapter);
    mDairyAdapter.notifyDataSetChanged();

//random code again

relativeLayout = view.findViewById(R.id.Dairy_RelativeLayout);
if (mDairyAdapter.getItemCount() == 0){
        relativeLayout.setVisibility(View.GONE);
    }else {
        relativeLayout.setVisibility(View.VISIBLE);
    }

这是我在片段中的InOnCreateView中编写的内容。此外,我在添加项目时尝试了notifyDataSetChange,但视图仍然无法返回。

0 个答案:

没有答案