4

I've implemented a swipe refresh layout onto one of my List Views however the view is not sliding down when I swipe and the icon is not visible. Even though it seems it doesn't work, it does actually fire off the OnRefresh listener.

Also I think its worth mentioning that I'm using the ListView on a fragment so i'm initializing the listener in the fragment activity which is displayed below.

Swipe Refresh XML

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal"
        android:dividerHeight="1sp"
        android:translationZ="5dp"
        android:background="@color/background_gray"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:clickable="true"
        />

</android.support.v4.widget.SwipeRefreshLayout>

Fragment Activity

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

    final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout);
    ListView lView = (ListView) getActivity().findViewById(R.id.listView);

    mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mSwipeRefreshLayout.setRefreshing(true);
            ((MainActivity) getActivity()).refresh();
            new Handler().postDelayed(new Runnable() {
                @Override public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 5000);




        }
    });
    lView.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);
        }
    });

}
Errol Green
  • 1,367
  • 5
  • 19
  • 32
  • maybe this will help : http://stackoverflow.com/questions/26858692/swiperefreshlayout-setrefreshing-not-showing-indicator-initially – Karan Jul 11 '15 at 02:31
  • Thanks @Kay but that does not resolve any of my issues. The solution on that thread is as I currently have it. – Errol Green Jul 11 '15 at 02:39

2 Answers2

4

I solved it, it appears that this is a current bug with the SwipeRefreshLayout. It will only work when Listview is wrapped inside a FrameLayout.

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal"
        android:dividerHeight="1sp"
        android:translationZ="5dp"
        android:background="@color/background_gray"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:clickable="true"
        />
    </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>
Errol Green
  • 1,367
  • 5
  • 19
  • 32
2

Simply set the property [android:clickable="true"] for the Immediate clild layout of SwipeRefreshLayout which occupies complete screen area.i.e. whose width and height is match_parent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srlayout_Container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true">

        <RelativeLayout
            android:id="@+id/rl_MainContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true">
            <!-- Placeholder for Other View or ViewGroup as Clild -->
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>