Android - Pull down to refresh the whole page
Asked Answered
A

2

5

I am making a feature that will let users to pull down a page anywhere in the application to check if they have a connection to the database server. I have followed this website, but it needs to Scrollview or ListView to function. All of my layouts are in a RelativeLayout only. Is there a way to do this without converting my whole layout to a ScrollView?

<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"
    tools:context="com.infomaninc.appinlisv2.NewClaim">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/rlHeader" >
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#1BB52D"
    android:gravity="center"
    android:padding="3dp"
    android:id="@+id/rlFooter" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/footer_value"
        android:id="@+id/tvFooter"
        android:textColor="#fff"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/app_name"
        android:id="@+id/tvLoggedOn"
        android:textColor="#fff"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>

Don't mind the contents of the code. It's just a snippet of a page. How will I implement the pull down to refresh if my page looks like this?

So far I have followed this tutorial:https://guides.codepath.com/android/Implementing-Pull-to-Refresh-Guide#step-2-setup-swiperefreshlayout

Algonquin answered 15/9, 2015 at 8:5 Comment(0)
D
7

You can also look for SwipeRefreshLayout. Although it needs a ScrollView/ListView as a child, but it wouldn't matter since all your data is being displayed in a single page. Just remember to set android:fillViewport="true" in your ScrollView and you'll do fine. You can go ahead as follows:

    SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.your_id_for_swiperefreshlayout);
    swipeRefreshLayout.setOnRefreshListener(this);
    @Override
    public void onRefresh() {
        //reload the data
    }
Dominicdominica answered 15/9, 2015 at 8:36 Comment(2)
I actually did this but it just screwed up my app. It consumed too much frames for who knows what the reason is. My app kept crashing after I did this. Sad.Algonquin
It's quite understandable. May I have a look at the logcat output for the same?Dominicdominica
I
3

I not sure if use RelativeLayout is your mandatory requirement, but you can put all your RelativeLayout in ScrollView. Then add OnScrollListener to refresh.

<ScrollView>   

    <RelativeLayout> 
           // all your relativelayout
    </RelativeLayout> 

 </ScrollView> 

in the Java code

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

  @Override
  public void onScrollChanged() {
    //example
    View view = (View) scrollView.getChildAt(0);
    int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
    if(diff == 0 || scrollView.getScrollY() == 0 ){
        Toast.makeText(getApplicationContext(),"Refresh",Toast.LENGTH_SHORT).show();
    }
  }
});

UPDATE

Example given detect ScrollView reached bottom when scroll down or top when scroll up. Can use scrollview and child properties to create refresh logic.

Including answered 15/9, 2015 at 8:17 Comment(4)
Will the onScrollChanged run only if you pull down from the top? or it will run so long as you scroll it?Algonquin
Is there anything you can add to share how to do the refresh logic, detect that you're scrolling off the top?Pantaloons
@Algonquin It will run when you scroll up or down.Including
@o0rebelious0o Scrollview will give information about top, bottom, width and height. From there you can add your refresh logic. I will update my answer later with refresh logic.Including

© 2022 - 2024 — McMap. All rights reserved.