简体   繁体   English

当没有条件显示的项目时隐藏 recyclerview

[英]Hide recyclerview when there is no item to show with condition

I am facing a problem to hide RecyclerView .我面临隐藏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 .我正在使用 firebase recycleradapterrecyclerview显示价值。 When ever someone click the viewholderitem i save his Uid under the datasnapshot key.当有人单击viewholderitem我将他的Uid保存在datasnapshot键下。

So to show the value if datasnapshot don't have the uid key show the value in recyclerview .因此,如果datasnapshot没有uid键, datasnapshot显示该值,请在recyclerview显示该值。 If the datasnapshot have the uid key don't show his data .如果数据datasnapshotuid键,则不显示他的数据。 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 .但我想隐藏recyclerview和显示textview当每一个datasnapshot有UID键并没有什么在显示recyclerview My problem is in the last line .我的问题在最后一行。 I can't hide the recyclerview and how the textview .我无法隐藏recyclerview以及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);

i have used adapter.registerAdapterDataObserver();我用过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.我不知道你是如何使用registerAdapterDataObserver()但这里是使用它的正确方法。 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.因此,要了解查询返回的项目数,您需要使用适配器类中存在的getItemCount()方法。 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 .由于来自 Firebase 实时数据库的数据是异步加载的,因此您不能直接在适配器类中直接调用getItemCount() ,因为它始终为零 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);
        }
    }
});

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

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.您需要这样做,因为 viewHolder 在您滚动时会被回收,如果您像这样更改属性并且永远不会将它们恢复到其自然状态,则其他元素将在它们重用相同视图的情况下已经隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM