Android Lollipop - Pull to refresh
Asked Answered
P

5

26

I am trying to implement pull-to-refresh in Android. I know there is SwipeRefreshLayout but with all the newly designed Google apps like Drive (see attached) for Lollipop, I have noticed there is a new refresh icon that comes in the view when pulled. I tried looking it online but in vain. Has Android released this as a part of the Material Design? Any ideas about how to implement it?

EDIT: Some people have pointed out how this is a duplicate of How to implement a Pull-to-refresh. It is not the same question. You'll see it if you read the question properly.

enter image description here

Progressionist answered 7/11, 2014 at 18:31 Comment(3)
it is released. SwipeRefreshLayout has this fancy button out of the boxUnkenned
possible duplicate of How to implement Android Pull-to-RefreshAlliaceous
@MarianPaździoch if you look closely, its not.Progressionist
C
60

This is SwipeRefreshLayout . Version 21 of the support library includes it replacing the old style.

Cistaceous answered 7/11, 2014 at 18:50 Comment(0)
L
19
  1. Download the latest Lollipop SDK and Extras/Android support library
  2. Set Project's Build Target to Android 5.0 (otherwise support package can have errors with resources)
  3. Update your libs/android-support-v4.jar to 21st version
  4. Use android.support.v4.widget.SwipeRefreshLayout plus android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener

Detailed guide could be found here: http://antonioleiva.com/swiperefreshlayout/

Plus for ListView I recommend to read about canChildScrollUp() in the comments ;)

Leffen answered 19/11, 2014 at 20:32 Comment(0)
R
7

I like this guide the best and its really easy to understand: https://www.bignerdranch.com/blog/implementing-swipe-to-refresh/

  1. Add the following to gradle:

    compile 'com.android.support:support-v4:22.2.0'

  2. Add the swipe to refresh to your layout - put in listview or recyclerview in the middle of the swiperefreshlayout:

        <ListView
            android:id="@+id/activity_main_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ListView>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    
  3. Add in your code for the mainactivity:

    public class MainActivity extends Activity {
    
    ListView mListView;
    SwipeRefreshLayout mSwipeRefreshLayout;
    Adapter mAdapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.acivity_main);
      SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
      mListView = findViewById(R.id.activity_main_list_view);
      mListView.setAdapter(new ArrayAdapter<String>(){
      String[] fakeTweets = getResources().getStringArray(R.array.fake_tweets);
      mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fakeTweets)
      listView.setAdapter(mAdapter);
    });
    }
    

    }

  4. Don't forget to call mSwipeRefreshLayout.setRefreshing(false); once your refreshing ends.

Roo answered 26/6, 2015 at 17:31 Comment(1)
Add some code form that link to avoid error if in future link is not reachableFoehn
P
4

hi If you wan't to develop such a kind of Layout then please follow this url, i was used it it's an awesome.

https://github.com/stormzhang/SwipeRefreshLayoutDemo

Pressroom answered 10/4, 2015 at 9:28 Comment(1)
Link is broken. It is usually not a good idea to only provide links to solve an issue.Gimcrackery

© 2022 - 2024 — McMap. All rights reserved.