Android 如果Recyclerview为空,则隐藏视图

Android 如果Recyclerview为空,则隐藏视图,android,android-recyclerview,Android,Android Recyclerview,我有一个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/too

我有一个
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"/>
以下是
RecyclerView
的完整类代码:

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和按钮仍然可见。如何修复此问题?

尝试以下方法:

<?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>

...
回收视图
为空时,只需设置清空卫星布局
可见
,否则
消失
尝试以下操作:

<?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>

...
只要在
RecyclerView
为空时设置emptyStateLayout
可见
,否则
在调用的checkNoneEmpty()方法getAdapter()中消失,该方法返回null,因为您没有在recycler视图上使用setAdapter()

为recycler视图创建一个适配器,并用空列表初始化它,然后在视图上设置adapter()

然后你可以使用这些语句

  checkOutRV.setEmptyView(emptyState);
  checkOutRV.setNonEmptyView(nonEmptyState); 
希望能有帮助

在checkNoneEmpty()方法中,您调用了getAdapter(),该方法返回null,因为您没有在recycler视图上使用setAdapter()

为recycler视图创建一个适配器,并用空列表初始化它,然后在视图上设置adapter()

然后你可以使用这些语句

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

希望能有帮助

默认情况下,保持布局
不可见
并显示显示
Recyclerview
没有数据或其他内容的布局,然后在进行API调用并接收数据时,隐藏显示Recyclerview为空的布局,并将数据布局视图设置为要显示的
可见

更新的答案

您需要注册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();
}

另外,不要忘记注销观察者。

默认情况下,保持布局
不可见
,并显示一个布局,显示
Recyclerview
没有数据或其他内容,然后,当您进行API调用并接收数据时,隐藏显示recyclerview为空的布局,并将数据布局视图设置为要显示的
可见

更新的答案

您需要注册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();
}

另外,不要忘记注销观察者。

这与问题无关。我想根据recyclerview是否有数据显示/隐藏按钮。您的布局不正确。应该只有一个根布局。您想显示/隐藏
按钮
清除状态
文本视图
?不工作。布局与此问题无关,与此问题无关。我想根据recyclerview是否有数据显示/隐藏按钮。您的布局不正确。应该只有一个根布局。您想显示/隐藏
按钮
清除状态
文本视图
?不工作。布局与此问题无关这是自定义类通过检查适配器大小是否为0或>0所做的。我也尝试了adapter.getItemCount,然后切换视图可见性,但这似乎不起作用。有什么想法吗?你有没有像这样注册你的
AdapterDataObserver
mRecyclerViewAdapter.registerAdapterDataObserver(myObserver)您还需要注销它。我的EmptyRecyclService类中已经有一个数据观察器。这里的区别是什么?我假设我应该在CheckAdapterSempty方法中检查适配器项计数?请解释,这可能是一个很好的解决方案这是自定义类通过检查适配器大小是否为0或>0来完成的。我也尝试了adapter.getItemCount,然后切换视图可见性,但这似乎不起作用。有什么想法吗?你有没有像这样注册你的
AdapterDataObserver
mRecyclerViewAdapter.registerAdapterDataObserver(myObserver)您还需要注销它。我的EmptyRecyclService类中已经有一个数据观察器。这里的区别是什么?我假设我应该在CheckAdapterSempty方法中检查适配器项计数?请解释一下,这可能是个好办法