简体   繁体   中英

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 ie 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>

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();
}
    <?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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM