PullToRefresh 之应用
- PullToRefresh是一套实现非常好的下拉刷新库,它支持:
1.ListView
2.ExpandableListView
3.GridView
4.WebView
等多种常用的需要刷新的View类型,而且使用起来也十分方便。
- 原理:在View中有一个scrollTo方法,可以将整个View移动到指定的位置,PullToRefresh就是重写了onTouchEvent方法来检测用户滑动的偏移距离,然后用滑动距离调用scrollTo方法来实现整个View的上下左右移动的。
点击这里下载 - 下载完成,将它导入到eclipse中,作为一个library导入到你的工程中就好了。
我们用下载下来工具中的library包就可以了。
其实library怎么用,很简单,就是你新建个Android项目,在其他项目中复制.classpath和.project文件到library下面,使之成为一个Android项目(只是这个项目是被引用的项目),如果你不怕麻烦也可以新建一个Android项目,把有用的文件对应拷贝到新建的Android项目中。
xml中GridView的布局,需要注意的是 ptr:ptrMode=”both”模式设定
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="@drawable/android" />
xml中ListView的布局
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
xml中的ScrollVie 它也有个设置模式:
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ptr:ptrAnimationStyle="flip"
ptr:ptrMode="both" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:text="@string/filler_text"
android:textSize="16sp" />
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
下面说一下代码:
listview 的刷新加载:
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
// Add an end-of-list listener
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(PullToRefreshListActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
}
});
ListView actualListView = mPullRefreshListView.getRefreshableView();
GridView的刷新加载:
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
mGridView = mPullRefreshGridView.getRefreshableView();
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(PullToRefreshGridActivity.this, "Pull Down!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
Toast.makeText(PullToRefreshGridActivity.this, "Pull Up!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}
});
//******************************************************
//停止刷新
mPullRefreshGridView.onRefreshComplete();
ScrollView的代码:
mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
@Override
public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
new GetDataTask().execute();
}
});
mScrollView = mPullRefreshScrollView.getRefreshableView();
//记得适配器之后停止刷新
mPullRefreshScrollView.onRefreshComplete();
dome中有具体的示例大家可以看看!