Google自己的下拉刷新组件SwipeRefreshLayout
这个下拉刷新组件挺漂亮的,但官方 SwipeRefreshLayout只提供下拉刷新功能,很多时候我们需要上拉刷新功能,所以下载v4源码修改 SwipeRefreshLayout,与之相关联的文件有两个分别是SwipeProgressBar,BakedBezierInterpolator把这三个文件拷贝到项目里面,修改一下包名就可以了。如何实现上拉刷新功能,可以网上查阅其他作者的文章,都有相应的实例介绍,例如:
1.安卓自带下拉刷新SwipeRefreshLayout添加上拉刷新功能
2.让Android Support V4中的SwipeRefreshLayout支持上拉加载更多(扩展一下这个组件以实现上拉下载的目的)
下拉刷新当然还可以用到一些第三方开源库,如PullToRefresh, ActionBar-PullToRefresh等,这边就简单记录一下官方SwipeRefreshLayout的使用。
API doc:http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
效果如图(网上找的,感谢原作者^_^):
在布局文件中添加:
1 <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/swipe_container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity" > 7 8 <!-- android:id="@id/android:list" 9 10 <ListView 11 android:id="@+id/list11" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:choiceMode="singleChoice" 15 android:descendantFocusability="blocksDescendants" 16 android:divider="@null" 17 android:scrollbars="none" > 18 </ListView>--> 19 20 <com.maomao.beautymovie.widget.CustomListView 21 android:id="@+id/movielistview" 22 android:layout_width="match_parent" 23 android:layout_height="match_parent" 24 android:choiceMode="singleChoice" 25 android:descendantFocusability="blocksDescendants" 26 android:divider="@null" 27 android:scrollbars="none" > 28 </com.maomao.beautymovie.widget.CustomListView> 29 30 </android.support.v4.widget.SwipeRefreshLayout>
在activity或fragment中使用即可:
1 package com.maomao.beautymovie.fragment; 2 3 4 public class AllMovieFragment extends Fragment{ 5 6 private static final String TAG = "movieFragment"; 7 private SwipeRefreshLayout swipeLayout; 8 private CustomListView movieListView; 9 private MovieListAdapter movieListAdapter; 10 private List<MovieBean> moviesLsit; 11 12 public AllMovieFragment() 13 { 14 } 15 16 public AllMovieFragment(List<MovieBean> moviesLsit) 17 { 18 this.moviesLsit = moviesLsit; 19 } 20 21 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 26 } 27 28 @SuppressLint("InlinedApi") 29 @Override 30 public View onCreateView(LayoutInflater inflater, ViewGroup container, 31 Bundle savedInstanceState) { 32 View view = inflater.inflate(R.layout.allmoviefrag, container, false); 33 movieListView = (CustomListView)view.findViewById(R.id.movielistview); 34 35 movieListAdapter = new MovieListAdapter(getActivity(), moviesLsit); 36 37 movieListView.setAdapter(movieListAdapter); 38 39 initSwipeLayout(view); 40 41 return view; 42 } 43 44 45 46 /** 47 * 初始化SwipeRefreshLayout 48 * @param view 49 */ 50 public void initSwipeLayout(View view) 51 { 52 swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container); 53 //swipeLayout.setOnRefreshListener(this); 54 //swipeLayout.setOnLoadListener(this); 55 swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 56 android.R.color.holo_green_light, android.R.color.holo_orange_light, 57 android.R.color.holo_red_light); 58 // 设置下拉刷新监听器 59 swipeLayout.setOnRefreshListener(new OnRefreshListener() { 60 61 @Override 62 public void onRefresh() { 63 64 Toast.makeText(getActivity(), "refresh", Toast.LENGTH_SHORT).show(); 65 66 swipeLayout.postDelayed(new Runnable() { 67 68 @Override 69 public void run() { 70 // 更新数据 71 //moviesLsit.addAll(MyDataSource.addMovieDataSource()); 72 //最新出現的放在前面 73 moviesLsit.addAll(0, MyDataSource.addMovieDataSource()); 74 movieListAdapter.notifyDataSetChanged(); 75 // 更新完后调用该方法结束刷新 76 swipeLayout.setRefreshing(false); 77 } 78 }, 2000); 79 } 80 }); 81 82 83 84 } 85 86 87 }