700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能

使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能

时间:2019-11-15 01:02:46

相关推荐

使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能

之前做东西的时候,经常会用到下拉刷新的功能,之前大家都在使用Github上的一个很著名的开源项目 PullToRefresh

但是,现在好消息来了,google在19.1版本的support-v4兼容包下面正式提供了官方的下拉刷新组件——SwipeRefreshLayout

注意,你要使用这个组件必须要把你的支持库升级到19.1版本以上

我们只需要在需要进行刷新的控件外面加上SwipeRefreshLayout 就可以了 ,但是 ,这个控件的child必须是可滑动的View,比如说ScrollerView或者ListView等

不多说,直接上代码,布局文件

1 <LinearLayout xmlns:android="/apk/res/android" 2xmlns:tools="/tools" 3android:layout_width="fill_parent" 4android:layout_height="fill_parent" 5android:orientation="vertical" 6tools:context="com.android.teypullrepfesh.MainActivity" > 7 8<TextView 9 android:layout_width="fill_parent"10 android:layout_height="64dp"11 android:text="@string/hello_world" />12 13<android.support.v4.widget.SwipeRefreshLayout14 android:id="@+id/refresh_layout"15 android:layout_width="fill_parent"16 android:layout_height="match_parent" >17 18 <ListView19 android:id="@+id/list"20 android:layout_width="fill_parent"21 android:layout_height="match_parent" />22</android.support.v4.widget.SwipeRefreshLayout>23 24 </LinearLayout>

activity_main.xml

然后在代码中的使用也很简单

1 public class MainActivity extends Activity implements OnRefreshListener { 2 3public ListView listView; 4public SwipeRefreshLayout refreshLayout; 5private String[] mListStr = { "姓名:菜菜", "性别:男", "年龄:20", "居住地:大连", 6 "邮箱:cwr941012@" }; 7private String[] mListStr_ = { "姓名:翠翠", "性别:男", "年龄:23", "居住地:北京", 8 "邮箱:cwr941012@" }; 9//定义两个不同的数据源10@Override11protected void onCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 setContentView(R.layout.activity_main);14 listView = (ListView) findViewById(R.id.list);15 listView.setAdapter(new ArrayAdapter<String>(this,16 android.R.layout.simple_list_item_1, mListStr));17 refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);18 refreshLayout.setOnRefreshListener(this);19 //设置一个监听器20 refreshLayout.setColorSchemeColors(android.R.color.holo_orange_light,21 android.R.color.holo_purple, android.R.color.holo_blue_dark,22 android.R.color.holo_red_light);23 //设置刷新条的颜色24 25}26 27@Override28public void onRefresh() {29 // TODO Auto-generated method stub30 new Handler().postDelayed(new Runnable() {31 public void run() {32 listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,33android.R.layout.simple_list_item_1, mListStr_));34 refreshLayout.setRefreshing(false);35 }36 }, 5000);37 38}39 }

MainActivity.java

在这里面,重写了onRefresh() 函数进行刷新之中的操作

基本上就是这样了,希望google能尽快把下拉加载更多的功能也添加进去就更加完美了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。