效果图
用法
我也是先参考https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh,然后将其中的只有TextView的下拉刷新效果实现了。
建议您先下载此项目,用androidstudio打开,验证是可以正确的运行的。然后,先看看这里面的样例。因为是封装好的库,我们调用其实是非常简单的。
然后,再写一个Demo,看看自己能不能在自己的项目的添加成功。但是,我当时因为各种原因,搞了一个星期才搞明白是怎么回事,才将这个在自己的Demo中跑起来。虽然参考的地方有使用方法,但是你不一定能跑起来,试试。
其实用法弄明白后,非常简单:
(1)app/build.gradle
//add for pull to refresh 2
compile 'in.srain.cube:clog:1.0.2'
compile 'in.srain.cube:cube-sdk:1.0.44.39-SNAPSHOT@aar'
compile 'in.srain.cube:ultra-ptr:1.0.11'
(2)build.gradle
allprojects {
repositories {
jcenter()
//add for pull to refresh 2
mavenCentral();
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
至下,你可以在自己的项目中自由的使用此功能了。
(3)在布局文件中:
<in.srain.cube.views.ptr.PtrFrameLayout
android:id="@+id/store_house_ptr_frame"
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
cube_ptr:ptr_resistance="1.7"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_duration_to_close="300"
cube_ptr:ptr_duration_to_close_header="2000"
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false" >
<LinearLayout
android:id="@+id/store_house_ptr_image_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cube_mints_333333"
android:clickable="true"
android:padding="10dp">
<in.srain.cube.image.CubeImageView
android:id="@+id/store_house_ptr_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</in.srain.cube.views.ptr.PtrFrameLayout>
(4)在java中使用
// the following are default settings
mPtrFrame.setResistance(1.7f);
mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
mPtrFrame.setDurationToClose(200);
mPtrFrame.setDurationToCloseHeader(1000);
// default is false
mPtrFrame.setPullToRefresh(false);
// default is true
mPtrFrame.setKeepHeaderWhenRefresh(true);
(5)配置头文件的字符串
// header
final StoreHouseHeader header = new StoreHouseHeader(getContext());
header.setPadding(0, LocalDisplay.dp2px(15), 0, 0);
/**
* using a string, support: A-Z 0-9 - .
* you can add more letters by {@link in.srain.cube.views.ptr.header.StoreHousePath#addChar}
*/
header.initWithString('Alibaba');
(6)配置头文件的字符串数组
header.initWithStringArray(R.array.storehouse);
And in res/values/arrays.xml:
<resources>
<string-array name="storehouse">
<item>0,35,12,42,</item>
<item>12,42,24,35,</item>
<item>24,35,12,28,</item>
<item>0,35,12,28,</item>
<item>0,21,12,28,</item>
<item>12,28,24,21,</item>
<item>24,35,24,21,</item>
<item>24,21,12,14,</item>
<item>0,21,12,14,</item>
<item>0,21,0,7,</item>
<item>12,14,0,7,</item>
<item>12,14,24,7,</item>
<item>24,7,12,0,</item>
<item>0,7,12,0,</item>
</string-array>
</resources>
(7)处理刷新
There is a PtrHandler, by which you can refresh the data.
public interface PtrHandler {
/**
* Check can do refresh or not. For example the content is empty or the first child is in view.
* <p/>
* {@link in.srain.cube.views.ptr.PtrDefaultHandler#checkContentCanBePulledDown}
*/
public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header);
/**
* When refresh begin
*
* @param frame
*/
public void onRefreshBegin(final PtrFrameLayout frame);
}
例如:
ptrFrame.setPtrHandler(new PtrHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
frame.postDelayed(new Runnable() {
@Override
public void run() {
ptrFrame.refreshComplete();
}
}, 1800);
}
@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
}
});
参考资料
1.android-Ultra-Pull-To-Refresh
https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh
2.AndroidDemo
https://github.com/hfreeman2008/AndroidDemo