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的使用方式,在此省略
...
}


 
 


转载于:https://www.cnblogs.com/LEON-D/p/11401651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值