zoukankan      html  css  js  c++  java
  • 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的使用方式,在此省略
    ...
    }


     
     


  • 相关阅读:
    js 判断图片是否加载完成
    js检测密码强度
    javascript 的MD5代码备份,跟java互通
    JavaScript实现限时抢购实例
    JS日期比较大小 给定时间和持续时间计算最终时间
    jquery与json的结合
    div内容过长自动省略号
    高并发大流量专题---11、Web服务器的负载均衡
    如何利用nginx实现负载均衡(总结)
    高并发大流量专题---10、MySQL数据库层的优化
  • 原文地址:https://www.cnblogs.com/LEON-D/p/11401651.html
Copyright © 2011-2022 走看看