代码之家  ›  专栏  ›  技术社区  ›  Andromeda

如果Recyclerview为空,则隐藏视图

  •  2
  • Andromeda  · 技术社区  · 7 年前

    我有一个 RecyclerView 使用此类型布局中的其他小部件:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.joey.joseph.aapnidukan.activities.CheckOutActivity">
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:id="@+id/backIV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/back_image"
                android:padding="16dp"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"/>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="CheckOut"
                android:textSize="18sp"
                android:fontFamily="casual"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:textColor="@android:color/white"/>
    
        </RelativeLayout>
    
    </android.support.v7.widget.Toolbar>
    
    <RelativeLayout
        android:id="@+id/nonEmptyState"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:visibility="visible">
    
        <in.joey.joseph.aapnidukan.utils.EmptyRecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/checkOutRV"
            />
    
        <RelativeLayout
            android:id="@+id/cartTotalLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/checkOutRV"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:layout_marginRight="20dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="right"
                android:layout_marginRight="20dp"
                android:padding="10dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Total: "
                    android:textSize="18sp"
                    android:textColor="@android:color/black"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="&#8377;"
                    android:textSize="18sp"
                    android:textColor="@android:color/black"
                    android:layout_marginLeft="10dp"/>
    
                <TextView
                    android:id="@+id/totalPriceTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="8999"
                    android:textSize="18sp"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginLeft="5dp"/>
    
            </LinearLayout>
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/billingBtn"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:background="@color/colorAccent"
            android:text="Proceed to Billing and Address"
            android:textColor="@android:color/white"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:layout_alignParentBottom="true"/>
    
    </RelativeLayout>
    
    <TextView
        android:id="@+id/emptyState"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Items to Cart Before CheckOut"
        android:textSize="18sp"
        android:fontFamily="casual"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:visibility="visible"/>
    

    我创建了一个自定义 回收视图 处理空状态,但它只适用于 回收视图 。当列表为空时,其他视图(如按钮)仍然可见。我试着在我的习惯中隐藏它 回收视图 类如下:

    private void checkNotEmpty(){
        if (nonEmptyView != null && getAdapter() != null){
            boolean nonEmptyViewVisible = getAdapter().getItemCount() > 0;
            nonEmptyView.setVisibility(nonEmptyViewVisible ? VISIBLE : GONE);
            setVisibility(nonEmptyViewVisible ? GONE : VISIBLE);
        }
    }
    

    下面是 回收视图 :

    public class EmptyRecyclerView extends RecyclerView {
    
    private View emptyView, nonEmptyView;
    
    public EmptyRecyclerView(Context context) {
        super(context);
    }
    
    public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    private final AdapterDataObserver dataObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkEmpty();
        }
    
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkEmpty();
        }
    
        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkEmpty();
        }
    };
    
    private void checkEmpty() {
        if (emptyView != null && getAdapter() != null){
            boolean emptyViewVisible = getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }
    
    private void checkNotEmpty(){
        if (nonEmptyView != null && getAdapter() != null){
            boolean nonEmptyViewVisible = getAdapter().getItemCount() > 0;
            nonEmptyView.setVisibility(nonEmptyViewVisible ? VISIBLE : GONE);
            setVisibility(nonEmptyViewVisible ? GONE : VISIBLE);
        }
    }
    
    public void setButtonVisibility(Button button){ // to toggle the button visibility
        if (getAdapter().getItemCount() > 0){
            button.setVisibility(View.VISIBLE);
        } else {
            button.setVisibility(GONE);
        }
    }
    
    @Override
    public void setAdapter(Adapter adapter) {
        Adapter oldAdapter = getAdapter();
        if (oldAdapter != null){
            oldAdapter.unregisterAdapterDataObserver(dataObserver);
        }
        super.setAdapter(adapter);
        if (adapter != null){
            adapter.registerAdapterDataObserver(dataObserver);
        }
    
        checkEmpty();
    }
    
    public void setEmptyView(View view){
        this.emptyView = view;
        checkEmpty();
    }
    
    public void setNonEmptyView(View view2){
        this.nonEmptyView = view2;
        checkNotEmpty();
    }
    
    }
    

    在我的活动中,我尝试过:

        init();
        checkOutRV.setEmptyView(emptyState);
        checkOutRV.setNonEmptyView(nonEmptyState);
    
        private void init() {
        nonEmptyState = findViewById(R.id.nonEmptyState);
        emptyState = findViewById(R.id.emptyState);
        checkOutRV = findViewById(R.id.checkOutRV);
    
        checkOutRV.setHasFixedSize(true);
        checkOutRV.setLayoutManager(new LinearLayoutManager(this));
    
    }
    

    列表现在没有数据,因为我还没有进行API调用,但emptyStateTextView和按钮仍然可见。如何修复此问题?

    3 回复  |  直到 7 年前
        1
  •  0
  •   Abhilash Maurya    7 年前

    默认情况下,保留布局 INVISIBLE 并显示一个布局,该布局显示 Recyclerview 没有数据或其他信息,然后在进行API调用并接收数据时,隐藏布局,显示recyclerview为空,并将数据布局视图设置为 VISIBLE 你想展示的。

    更新的答案

    您需要注册Data observer以侦听来自同步适配器的数据更改。

    mRecyclerViewAdapter.registerAdapterDataObserver(myObserver);
    

    RecyclerView.AdapterDataObserver 是您调用的notify方法的结果。例如,如果你打电话 notifyItemInserted() 将项目添加到适配器后 onItemRangeInserted() 将被呼叫

    更详细的示例

    protected void setupRecyclerView() {
        mAdapter = new MyAdapter(mItemList);
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                checkAdapterIsEmpty();
            }
        });
    
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
    
        mRecyclerView.setAdapter(mAdapter);
        checkAdapterIsEmpty();
    }
    

    也不要忘记注销观察家。

        2
  •  0
  •   Shubham    7 年前

    在您调用的checkNonEmpty()方法中 getAdapter() 它返回null,因为您尚未在回收器视图上使用setAdapter()。

    为回收器视图创建一个适配器,并用空列表对其进行初始化,然后在视图上设置适配器()。

    然后你可以使用这些语句

      checkOutRV.setEmptyView(emptyState);
      checkOutRV.setNonEmptyView(nonEmptyState); 
    

    希望有帮助!!

        3
  •  -1
  •   Faraz    7 年前

    尝试以下操作:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout >
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            >
    
            ...
    
        </android.support.v7.widget.Toolbar>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:visibility="visible">
    
            <RelativeLayout
                android:id="@+id/nonEmptyState"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <!-- Existing layout like recyclerview, button -->
            </RelativeLayout>
    
    
            <RelativeLayout
                android:id="@+id/emptyStateLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/emptyState"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:fontFamily="casual"
                    android:gravity="center"
                    android:text="Add Items to Cart Before CheckOut"
                    android:textColor="@android:color/black"
                    android:textSize="18sp"
                    android:visibility="visible"/>
            </RelativeLayout>
    
        </RelativeLayout>
    </RelativeLayout>
    

    刚刚设置好 清空卫星布局 VISIBLE 什么时候 RecyclerView 为空,否则 GONE