Android 刷新框架SmartRefreshLayout
依赖
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'//没有使用特殊Header,可以不加这行
implementation 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必须)
实例
xml
<LinearLayout 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=".MainActivity">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:id="@+id/head"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.scwang.smartrefresh.layout.header.ClassicsHeader>
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.scwang.smartrefresh.layout.footer.ClassicsFooter>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
activity
public class MainActivity extends AppCompatActivity {
private ListView lv1;
private List<String> list = new ArrayList<>();
private ArrayAdapter arrayAdapter;
private ClassicsHeader head;
private SmartRefreshLayout refreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
for (int i = 0; i < 100; i++) {
list.add("item " + i);
}
arrayAdapter.notifyDataSetChanged();
}
private void initView() {
lv1 = (ListView) findViewById(R.id.lv1);
head = (ClassicsHeader) findViewById(R.id.head);
arrayAdapter = new ArrayAdapter(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, list);
lv1.setAdapter(arrayAdapter);
refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
super.onLoadMore(refreshLayout);
Log.e("######加载",2+"");
for (int i = 500; i < 600; i++) {
list.add("item " + i);
}
arrayAdapter.notifyDataSetChanged();
refreshLayout.finishLoadMore();
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
super.onRefresh(refreshLayout);
Log.e("######刷新",1+"");
for (int i = 100; i < 200; i++) {
list.add("item " + i);
}
arrayAdapter.notifyDataSetChanged();
refreshLayout.finishRefresh();
}
});
}
}
属性表格
Attributes
name | format | description |
---|---|---|
srlPrimaryColor | color | 主题颜色 |
srlAccentColor | color | 强调颜色 |
srlReboundDuration | integer | 释放后回弹动画时长(默认250毫秒) |
srlFooterHeight | dimension | Footer的标准高度(dp) |
srlHeaderInsetStart | dimension | Header的起始偏移量(dp)V1.0.5 |
srlFooterInsetStart | dimension | Footer的起始偏移量(dp)V1.0.5 |
srlDragRate | float | 显示拖动高度/真实拖动高度(默认0.5,阻尼效果) |
srlHeaderMaxDragRate | float | Header最大拖动高度/Header标准高度(默认2,要求>=1) |
srlFooterMaxDragRate | float | Footer最大拖动高度/Footer标准高度(默认2,要求>=1) |
srlHeaderTriggerRate | float | Header触发刷新距离 与 HeaderHeight 的比率(默认1) |
srlFooterTriggerRate | float | Footer触发加载距离 与 FooterHeight 的比率(默认1) |
srlEnableRefresh | boolean | 是否开启下拉刷新功能(默认true) |
srlEnableLoadMore | boolean | 是否开启加上拉加载功能(默认false-智能开启) |
srlEnableAutoLoadMore | boolean | 是否监听列表惯性滚动到底部时触发加载事件(默认true) |
srlEnableHeaderTranslationContent | boolean | 拖动Header的时候是否同时拖动内容(默认true) |
srlEnableFooterTranslationContent | boolean | 拖动Footer的时候是否同时拖动内容(默认true) |