您的位置:首页 > 其它

最强大的下拉刷新框架_SmartRefreshLayout的基本使用

2017-09-27 17:24 543 查看

介绍

支持下拉刷新、上拉加载、RefreshLayout、OverScroll,Android智能下拉刷新框架,支持越界回弹,具有极强的扩展性,集成了几十种炫酷的Header和 Footer。

不只是如其它的刷新布局所说的支持所有的View,还支持多层嵌套的视图结构。 除了“聪明”之外,SmartRefreshLayout还具备了很多的特点。
它继承自ViewGroup 而不是其它的FrameLayout或者LinearLayout,提高了性能。
 https://github.com/scwang90/SmartRefreshLayout[/code] 
支持所有的 View(AbsListView、RecyclerView、WebView....View) 和多层嵌套的视图结构
支持自定义并且已经集成了很多炫酷的 Header 和 Footer (图).
支持和ListView的同步滚动 和 RecyclerView、AppBarLayout、CoordinatorLayout 的嵌套滚动 NestedScrolling.
支持在Android Studio Xml 编辑器中预览 效果(图)
支持分别在 Default(默认)、Xml、JavaCode 三个中设置 Header 和 Footer.
支持自动刷新、自动上拉加载(自动检测列表惯性滚动到底部,而不用手动上拉).
支持通用的刷新监听器 OnRefreshListener 和更详细的滚动监听 OnMultiPurposeListener.
支持自定义回弹动画的插值器,实现各种炫酷的动画效果.
支持设置主题来适配任何场景的App,不会出现炫酷但很尴尬的情况.
支持设置多种滑动方式来适配各种效果的Header和Footer:平移、拉伸、背后固定、顶层固定、全屏
支持内容尺寸自适应 Content-wrap_content
支持继承重写和扩展功能,内部实现没有 private 方法和字段,继承之后都可以重写覆盖
支持越界回弹(Listview、RecyclerView、ScrollView、WebView...View)
支持多点触摸,下拉、上拉各种手势冲突


功能方法: https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md[/code] 

导包

compile 'com.android.support:appcompat-v7:25.3.1'//版本随意
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//没有使用特殊Header,可以不加这行


下拉刷新

//可以包含任意布局和嵌套布局
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.junx.smartrefreshlayout.MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>


处理下拉刷新事件
private SmartRefreshLayout refresh;
refresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//结束加载
refresh.finishRefresh();
//加载失败的话3秒后结束加载
refreshlayout.finishRefresh(3000);
}
},3000);
}
});


上拉刷新

//用法同上
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.junx.smartrefreshlayout.MainActivity">

<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>


//为lv填充数据,只有数据填充到底部时候才会触发上拉加载更多
lv = (ListView) findViewById(R.id.lv);

4000
ArrayList<String> datas = new ArrayList<>();
for (int i = 0; i < 50; i++) {
datas.add("哈哈"+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,datas);
lv.setAdapter(adapter);
//进行加载更多的逻辑处理
refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
refresh.finishLoadmore();
}
}, 3000);
}
});


添加头布局和脚布局

优先级 三>二>一


方法一:
//设置头布局样式,全局有效
SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
//全局设置主题颜色
layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//指定为经典Header,默认是 贝塞尔雷达Header
return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
}
});
//设置脚布局样式,全局有效
SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定为经典Footer,默认是 BallPulseFooter
return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
}
});


方法二:
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.junx.smartrefreshlayout.MainActivity">
<!--在这里设置头布局-->
<com.scwang.smartrefresh.header.PhoenixHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--也可以用控件设置头布局,比例使用gifview就可以实现万能的头布局-->
<TextView
android:text="我是头布局"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<!--在这里设置脚布局-->
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>


方法三:
RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.smartLayout);
refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));


属性设置

请访问 https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: