Android Lollipop - Pull to refresh

只谈情不闲聊 提交于 2019-11-29 20:19:32

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

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

Simon

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.

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

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