Sagar Mehta
Sagar Mehta

Reputation: 455

Android Swipe refresh layout issue

I have two textviews and one floating action button, and a listview. Currently I am able to use swiperefreshlayout for listview but I want it to use for whole view i.e. when i swipe down from the top of the activity, the listview should be updated in background. Can anyone help me please. This is my current xml file.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/view_tickets_activity_text_view_note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView android:id="@+id/view_tickets_activity_text_view_customer_name"
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/view_tickets_activity_text_view_note"
            android:layout_below="@+id/view_tickets_activity_text_view_note"
            android:layout_marginTop="10dp" />

        <android.support.design.widget.FloatingActionButton android:id="@+id/view_tickets_activity_floating_btn_add_ticket"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentRight="true"  android:layout_alignTop="@+id/view_tickets_activity_text_view_customer_name" />                             


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/view_tickets_activity_floating_btn_add_ticket"
            android:layout_marginTop="15dp">

            <ListView
                android:id="@+id/view_tickets_activity_list_view_tickets"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

Upvotes: 1

Views: 904

Answers (3)

tarasmorskyi
tarasmorskyi

Reputation: 295

You do not have to create layout with ListView like this, you have for ex. to add TextViews to header of ListView, cause RelativeLayout is not scrollable, that's why you can't have swipeToRefresh on it

Upvotes: 0

Mayuresh Gawande
Mayuresh Gawande

Reputation: 197

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/view_tickets_activity_text_view_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/view_tickets_activity_text_view_customer_name"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/view_tickets_activity_text_view_note"
        android:layout_below="@+id/view_tickets_activity_text_view_note"
        android:layout_marginTop="10dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/view_tickets_activity_floating_btn_add_ticket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/view_tickets_activity_text_view_customer_name" />

    <ListView
        android:id="@+id/view_tickets_activity_list_view_tickets"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_tickets_activity_floating_btn_add_ticket"
        android:layout_marginTop="15dp" />

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

Upvotes: 0

xUser
xUser

Reputation: 81

Use SwipeRefreshLayout on whole layout:

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_height="match_parent"
android:layout_width="match_parent">

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/view_tickets_activity_text_view_note"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content" />

    <TextView
        android:id="@+id/view_tickets_activity_text_view_customer_name"
        android:layout_alignLeft="@+id/view_tickets_activity_text_view_note"
        android:layout_below="@+id/view_tickets_activity_text_view_note"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_width="220dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/view_tickets_activity_floating_btn_add_ticket"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/view_tickets_activity_text_view_customer_name"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <ListView
        android:id="@+id/view_tickets_activity_list_view_tickets"
        android:layout_below="@+id/view_tickets_activity_floating_btn_add_ticket"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

And update your ListView in :

    @Override
public void onRefresh() {
    // Update Adapter for ListView here
    adapter.notifyDataSetChanged();
}

Upvotes: 1

Related Questions