Android SmartRefreshLayout 使用

SmartRefreshLayout是一款实现上拉加载、下拉刷新的控件,网络上相关内容也很多,在这里简单总结下我的使用

使用SmartRefreshLayout需导入依赖:
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'//使用特殊的Header(不是必须)

xml布局文件
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:srlPrimaryColor="@android:color/white"
app:srlAccentColor="@android:color/darker_gray"
app:srlEnablePreviewInEditMode="true">
<!--srlAccentColor srlPrimaryColor 控制 Header 和 Footer 的背景颜色-->
<!--srcAccentColor 控制 Header 和 Footer 字体颜色-->
<!--srlEnablePreviewInEditMode 开启和关闭预览功能-->
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
android:background="@color/white">
<ListView
      android:id="@+id/listview"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>

//声明
private SmartRefreshLayout refreshLayout;
int current_page = 1;//当前页,默认第一页
int pages = 1;//总页数,获取服务端数据
int rows = 20;//每页显示行数

//初始化及事件
refreshLayout = findViewById(R.id.refreshLayout);

//刷新
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
getData();//
refreshlayout.finishRefresh();
}
});

//加载更多
mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
getData();//
refreshlayout.finishLoadmore();
if(current_page >= pages){//判断当前页是否最后一页
  refreshlayout.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据
}
}
});

//获取数据方法,可改为获取服务器数据
public void getTable(){
if(current_page <= 1){
current_page = 1;
}
if(current_page >= pages){
current_page = pages;
}
    int satrt = (current_page - 1) * count;
int end = current_page * count;
for (int i = satrt; i < end; i++) {
Map<String, String> item = new HashMap<>();
item.put("name","value"+i);
table_list.add(item);
}
//后续步骤为listview的使用方式,在此省略
...
}


 
 


posted @ 2019-08-23 17:44  LEON D  阅读(4497)  评论(0编辑  收藏  举报