Android support v4 SwipeRefreshLayout empty view issue

后端 未结 9 1137
耶瑟儿~
耶瑟儿~ 2021-01-01 18:01

SwipeRefresh is not working after setting an empty view for listview which is the only child of a SwipeRefresh layout. How to solve this issue?

相关标签:
9条回答
  • 2021-01-01 18:57

    I had this issue too, and solved it without any additional code in the activity by using the layout below.

    If you are using a ListActivity or ListFragment it handles showing/hiding the empty view for you, and refreshing works with an empty list as well with this layout structure.

    No hacks needed, everything is in the SwipeRefreshLayout, as it should be.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Refresher"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@null" />
            <ScrollView
                android:id="@android:id/empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:text="Geen formulieren gevonden"
                    style="@style/text.EmptyView" />
            </ScrollView>
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
    

    Hope this helps you. If so, don't forget to accept the answer.

    Note: this is a duplicate of my answer to this question, but that question is pretty much a duplicate of this question... :)

    UPDATE
    If the SwipeRefreshLayout is activated too early, you can implement ListView.OnScroll with:

    _refresher.Enabled = e.FirstVisibleItem == 0;
    

    This disables the refresher until you scrolled to the top. This is only needed when using a ListView, the new RecyclerView works as expected.

    0 讨论(0)
  • 2021-01-01 18:58

    I fixed this issue using two SwipeToRefreshLayouts.

    • One for wrapping the ListView
    • The other as an emptyView

    I posted my code on GitHub.

    0 讨论(0)
  • 2021-01-01 19:00

    As @Dinesh written above, i have used clickable="true" like below with empty RecyclerView:

    mRecyclerView.setVisibility(View.VISIBLE);
    mRecyclerView.setClickable(true);
    myText.setVisibility(View.VISIBLE);
    

    xml layout:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshKartvizitKisilerim"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </android.support.v4.widget.SwipeRefreshLayout>
    
    
        <TextView
            android:id="@+id/myText"
            android:visibility="gone"
            android:text="@string/noListItem"
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    RecyclerView has height match_parent and SwipeRefresh has wrap_content. When there is item in list, don't forget to make text gone.

    0 讨论(0)
提交回复
热议问题