4

I am using a ListView and refreshing it when swiping down with SwipeRefresh, it works fine till here. The problem occurs when I try to set a TextView on mListview.setEmptyView() the Refresh Icon is not shown while swiping.

It looks like it is under the TextView. However if I don't put that TextView on the same XML it works fine, but I need to show a message when a list is empty. What is causing this behaviour?

Here is the layout I use:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

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

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

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

    <TextView
        android:id="@+id/swipeToRefreshTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp"
        android:gravity="center_horizontal"
        android:text="@string/msg_swipe_to_refresh"
        android:visibility="gone" />

</RelativeLayout>

Here is the Refresh listener:

//Listeners
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {

        //Here I do some job
        beaconScanSwipeRefresh.setRefreshing(false);
    }
});

Here is where I set the adapter of ListView and onEmptyView:

//UI
mListLv = (ListView) view.findViewById(R.id.mListView);
mSwipeRefresh=(SwipeRefreshLayout)view.findViewById(R.id.swipeRefreshLayout);
swipeToRefreshTv = (TextView)view.findViewById(R.id.swipeToRefreshTextView);

//Set Adapter
mListLv.setAdapter(mBaseAdapter);

//Set Message when list is empty
mListLv.setEmptyView(swipeToRefreshTv);

I have tried to move my TextView inside <android.support.v4.widget.SwipeRefreshLayout tags or above them. Still same result.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hrskrs
  • 4,447
  • 5
  • 38
  • 52

3 Answers3

1
   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

<TextView
        android:id="@+id/swipeToRefreshTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"        
        android:gravity="center_horizontal"
        android:text="@string/msg_swipe_to_refresh"
        android:visibility="gone" />

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

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

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



</RelativeLayout>

please update your layout in this way and check.

Pavya
  • 6,015
  • 4
  • 29
  • 42
  • As i have written i tried it above `SwipeRefreshLayout` with `below` on ` – hrskrs Jan 23 '15 at 09:40
  • please show me your screen: Refresh Icon is not shown while swipping – Pavya Jan 23 '15 at 09:42
  • Yeah it is not shown while the list is empty and trying to swipe. But when i release the touch the icon comes afterward, Icon should come from `ActionBar` down based on the swipe then when released will be gone – hrskrs Jan 23 '15 at 09:44
0

As long as your SwipeRefreshLayout and your TextView are under same RelativeLayout one of them will be on top of each other.

I would try to put it at the same level as your ListView, then both of them inside a container, inside SwipeRefreshLayout. And since SwipeRefreshLayout is the single item in the layout, you can drop the outer RelativeLayout.

<?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"
    android:paddingLeft="@dimen/padding_light"
    android:paddingRight="@dimen/padding_light">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/swipeToRefreshTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dp"
            android:gravity="center_horizontal"
            android:text="@string/msg_swipe_to_refresh"
            android:visibility="gone" />
    </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>
hidro
  • 12,333
  • 6
  • 53
  • 53
  • nope same behaviour. THe thing is that while i swipe it is not shown but after i release touching the screen, the icon is shown. When the `listView` is populated with data the icon comes from `ActionBar` while i swipe down. I want this behaviour to work same also when the list is empty – hrskrs Jan 23 '15 at 09:48
  • What version of support-v4 library are you using? – hidro Jan 23 '15 at 09:52
  • `compile 'com.android.support:support-v4:21.0.3'` – hrskrs Jan 23 '15 at 09:53
0

You can try to put a ListView or RecyclerView into a ScrollView. For me it did the trick.

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/mListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>
mat
  • 337
  • 1
  • 9