Android - SwipeRefreshLayout with empty textview

后端 未结 15 1401
悲哀的现实
悲哀的现实 2020-12-05 02:06

I\'ve implemented SwipeRefreshLayout into my app but it can only hold one direct child which should be the listview. I\'m trying to figure out how to add an emp

相关标签:
15条回答
  • 2020-12-05 03:04

    Here's what I did : I disabled the swipe to refresh, unless my listView is up.

    mBookedProductsListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
            }
            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,     int totalItemCount) {
                if (firstVisibleItem == 0)
                    mSwipeRefreshLayout.setEnabled(true);
                else
                    mSwipeRefreshLayout.setEnabled(false);
            }
        });
    

    My Xml :

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
    <RelativeLayout
            android:id="@+id/productListLL"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
        <EditText
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentTop="true"
                android:background="#a4aeb8"
                android:drawableRight="@drawable/search_icon"
                android:paddingRight="15dp"
                android:textColor="@android:color/white"
                android:paddingLeft="15dp"
                android:hint="Rechercher"
                android:textColorHint="@android:color/white"
                android:inputType="textCapWords|textNoSuggestions"
                />
    
        <include android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/filter_by_categories_buttons" android:id="@+id/categoriesTree" android:layout_below="@id/search" />
    
        <ListView
                android:id="@+id/productListLV"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@drawable/product_listview_divider"
                android:dividerHeight="1dp"
                android:scrollbars="none"
                android:overScrollMode="never"
                android:choiceMode="multipleChoiceModal"
                android:background="#e7eaef"
                android:visibility="gone"
                android:layout_below="@id/categoriesTree"
                />
    
    </RelativeLayout>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    Works like a charm.

    0 讨论(0)
  • 2020-12-05 03:05

    There is no need for any workaround.

    You can simply use this view hierarchy :

        <FrameLayout ...>
    
            <android.support.v4.widget.SwipeRefreshLayout ...>
    
                <ListView
                    android:id="@android:id/list" ... />
            </android.support.v4.widget.SwipeRefreshLayout>
    
            <TextView
                android:id="@android:id/empty" ...
                android:text="@string/empty_list"/>
        </FrameLayout>
    

    Then, in code, you just call:

    _listView.setEmptyView(findViewById(android.R.id.empty));
    

    That's it.


    EDIT: If you wish to be able to swipe-to-refresh even when the empty view is shown, you will have to somehow avoid hiding the ListView, so you could use a customized ListView that has this function inside:

    @Override
      public void setVisibility(final int visibility)
        {
        if(visibility!=View.GONE||getCount()!=0)
          super.setVisibility(visibility);
        }
    

    Together with the solution I wrote, the swipe-to-refresh is shown no matter how many items you are showing.

    Of course, if you really want to hide the ListView, you should change the code. Maybe add "setVisibilityForReal(...)" :)

    0 讨论(0)
  • 2020-12-05 03:05

    Why not wrap everything inside SwipeRefreshLayout

    <android.support.v4.widget.SwipeRefreshLayout 
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"/>
    
        <RelativeLayout
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            ...
        </RelativeLayout>
    </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
    
    0 讨论(0)
提交回复
热议问题