Hide recyclerview when there is no item to show with condition

三世轮回 提交于 2021-02-15 07:48:30

问题


I am facing a problem to hide RecyclerView. From Last 2 night's I am looking for the solution but I have failed to find it.

I am using firebase recycleradapter to show value in recyclerview. When ever someone click the viewholderitem i save his Uid under the datasnapshot key.

So to show the value if datasnapshot don't have the uid key show the value in recyclerview. If the datasnapshot have the uid key don't show his data . everything works fine . But i want to hide the recyclerview and show a textview when every datasnapshot have the uid key and there is nothing to show in the recyclerview. My problem is in the last line . I can't hide the recyclerview and how the textview.

FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<ViewsAdapter>()
                .setQuery(ViewsRef,ViewsAdapter.class)
                .build();


        adapter = new FirebaseRecyclerAdapter<ViewsAdapter, ViewsAdapterHolder>(options) {

            @Override
            protected void onBindViewHolder(@NonNull final ViewsAdapterHolder holder, int position, @NonNull ViewsAdapter model) {
                String userIds = getRef(position).getKey();
                assert userIds != null;
                ViewsRef.child(userIds).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                        if (dataSnapshot.hasChild(Uid)){
                            Long date = dataSnapshot.child(Uid).getValue(Long.class);

                            assert date != null;
                            int dat = date.intValue();

                            if (dat!=Date){
                                recyclerView.removeAllViews();
                                dataSnapshot.child(Uid).getRef().setValue(null);
                                adapter.startListening();


                            }

                            ViewGroup.LayoutParams layoutParams =holder.itemView.getLayoutParams();
                            layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
                            layoutParams.height= 0;
                            holder.itemView.setLayoutParams(layoutParams);







                        }else {

                            //get the data from child and show in recyclerview

                            holder.button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                        }



                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }

            @NonNull
            @Override
            public ViewsAdapterHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.viewsview,viewGroup,false);
                return new ViewsAdapterHolder(view);
            }



        };

        recyclerView.setAdapter(adapter);

回答1:


i have used adapter.registerAdapterDataObserver(); method but it doesn't works .

I don't know how you used registerAdapterDataObserver() but here is the correct approch of using it. So to know the number of items that are returned by the query, you need to use getItemCount() method that exist in your adapter class. Because the data from Firebase realtime database is loaded asynchronously, you cannot simply call getItemCount() directly in your adapter class, as it will always be zero. So in order to get the total number of items, you need to register an observer like in the following lines of code:

adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
    public void onItemRangeInserted(int positionStart, int itemCount) {
        int totalNumberOfItems = adapter.getItemCount();
        Log.d(TAG, String.valueOf(totalNumberOfItems));
        if(totalNumberOfItems == 0) {
            recyclerView.setVisibility(View.GONE);
            emptyInfoTextView.setVisibility(View.VISIBLE);
        }
    }
});



回答2:


In some cases changing only visibility, the attribute might still end up as allocated blank space (because of parent view's padding, margins, inner elements, etc). Then changing the height of the parent view helps:

holder.itemView.setVisibility(View.GONE); 
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

Then be sure that in the condition that it should be visible, also set:

holder.itemView.setVisibility(View.VISIBLE);
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

You need to do that because the viewHolder is recycled as you scroll, if you change properties like this and never return them to their natural state, other elements will be already hidden in the event they reuse the same view.




回答3:


So you want to hide when there is no child?

if (dataSnapshot.hasChild(Uid)){
    //your stuff...
}else {
    //set data in viewholder . it works fine
    //Here you hide it.
    emptyInfoTextView.setVisibility(View.VISIBLE);
    recyclerView.setVisibility(View.GONE);
        holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}


来源:https://stackoverflow.com/questions/57587615/hide-recyclerview-when-there-is-no-item-to-show-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!