在Android的开发过程中最常用的功能就是刷新功能。在it界里有一句话就是不要重复造轮子,当然不必重复造轮子并不意味着你可以不懂造轮子的原理。呵呵~有点跑题了哈。
今天我们就来使用别人造好的轮子。开源的刷新控件在github上有很多,我用的是这个哥们写的附上地址哈https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide。将代码从github上download下来。
将下载下来的控件导入到你的工程中。
第一步:右击你项目选址properties出现如图,根据提示走就能将开源控件导入到你的工程当中。
第二步:将这个开源控件包导入到你的工程当中,接下来就是使用它了。怎么使用呢?
首先就是在xml文件申明这个控件了
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:background="@color/white"
android:id="@+id/pull_refresh_homepage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#ffffff"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
android:layout_below="@+id/ll_head_homepage"
android:layout_above="@+id/ll_bottom_homeoage"
/>
在activity中初始化这个控件这里我就不多说了,初始化就是findViewById.相信这个大家都会了。
PullToRefreshListView PullRefreshListView_homepage=(PullToRefreshListView)findViewById(R.id.pull_refresh_homepage);
控件申明和初始化也都完成了,接下来就是添加上拉和下拉的监听器了。还是直接贴代码吧
PullRefreshListView_homepage.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
RefreshTask task=new RefreshTask();
task.execute("DownToRefresh");
}
@Override
public void onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
RefreshTask task=new RefreshTask();
task.execute("UpToRefresh");
}
});
可能大家添加的监听器Eclipse提示不出来
new OnRefreshListener2<ListView>()
可能是你导入的包不对,导入的包应该有
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
这样上拉和下拉就完成了吗???
没有,从github下载下来的只有默认的下拉功能,要想添加上拉功能就要修改一下它的源码,其实也没多少难度就是一句代码的事。
找到com.handmark.pulltorefresh.library包下的PullToRefreshBase类将
private Mode mMode = Mode.getDefault();
改成private Mode mMode = Mode.BOTH;就行了
到这里才算是完成真正的上拉和下拉刷新了。
我的Android开发经验分享才这一篇博客开始。。。嘿嘿