没有项目可显示时隐藏recyclerview [英] Hide recyclerview when there is no item to show with condition

查看:65
本文介绍了没有项目可显示时隐藏recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到隐藏 RecyclerView 的问题.从最后两晚开始,我正在寻找解决方案,但是找不到.

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.

我正在使用Firebase recycleradapter recyclerview 中显示值.每当有人单击 viewholderitem 时,我会将其 Uid 保存在 datasnapshot 键下.

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

因此,如果 datasnapshot 没有 uid 键,则显示该值.如果 datasnapshot 具有 uid 键,则不显示其数据.一切正常.但是我想隐藏 recyclerview 并在每个 datasnapshot 都具有uid键时显示一个 textview ,并且中没有任何内容可显示recyclerview .我的问题在最后一行.我无法隐藏 recyclerview textview .

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);

推荐答案

我使用了adapter.registerAdapterDataObserver();方法,但不起作用.

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

我不知道您如何使用 registerAdapterDataObserver(),但这是使用它的正确方法.因此,要知道查询返回的项目数,您需要使用适配器类中存在的 getItemCount()方法.由于Firebase实时数据库中的数据是异步加载的,因此您不能简单地直接在适配器类中调用 getItemCount(),因为它始终为.因此,为了获得项目总数,您需要像下面的代码行一样注册观察者:

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);
        }
    }
});

这篇关于没有项目可显示时隐藏recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆