开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,之前大家最常用的应该是pull to refresh或它的变种版吧,google官方在最新的android.support.v4包中增加了一个新类SwipeRefreshLayout,地址 这个类的作用就是提供官方的下拉刷新,并且效果相当不错,而上拉加载更多则用我们自定义的listview,也是相当简单。
下拉刷新
简单的介绍下:
首先它是一个viewgroup,但是它只允许有一个子控件,子控件能是任何view,使用的时候,所在类实现OnRefreshListener接口,在onRefresh()方法中实现所要完成的任务,
findviewbyid得到SwipeRefreshLayout后,显示刷新动画用SwipeRefreshLayout.setRefreshing(true);取消刷新动画用SwipeRefreshLayout.setRefreshing(false);相当简单。
另外有一些其他的常用方法比如判断时候正在显示刷新动画,用SwipeRefreshLayout.isShown()。
布局:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swip_index"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.listviewloadmore.LoadMoreListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" >
</com.example.listviewloadmore.LoadMoreListView>
</android.support.v4.widget.SwipeRefreshLayout>
上拉加载更多
思路:监听滑动事件并判断是否到达底部,到达底部的时候,回调我们定义好的一个方法,从而达到上拉加载更多的目的。
但是具体怎么监听和回调呢,我们不妨看看Button控件的点击事件的执行流程,Button继承textView,textView继承View
1.实现OnClickListener接口,