Android Lollipop - Pull to refresh

半城伤御伤魂 提交于 2019-11-28 16:19:49

问题


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.


回答1:


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




回答2:


  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 ;)




回答3:


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.




回答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




回答5:


And the literal icon is in here:

http://material-design.storage.googleapis.com/publish/v_1/quantumexternal/0B08MbvYZK1iNZ19ldS1aNzdTQkU/material-design-icons-1.0.0.zip

source



来源:https://stackoverflow.com/questions/26807763/android-lollipop-pull-to-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!