Recylerview 加载更多功能实现(分页加载)

2017.3.20更新

更新说明:之前看评论里的童鞋们给我反映问题,很感谢大家指出,针对出现的问题我进行了一些改进。同时将加载更多的这个功能从主体的adapter中分离出来,使用了装饰者模式,降低了代码的耦合,这样便于维护和修改。

装饰者模式是常用的Java设计模式之一,不熟悉的童鞋可以自行查阅资料,先了解了装饰者模式看以下内容会容易懂一点~这个设计模式在Android里也是广泛应用,最典型的就是Context的应用了。

……………………….我是分割线…………………………………………

Recyclerview是 Listview 的升级版本,在项目中使用较为广泛,官方也推荐我们使用 Recyclerview 来代替 Listview,在此就不多说 Recyclerview 的优势特点 balala了。。。

在实际项目中,列表通常是分页的,请求服务器也只会一次请求若干条,按需加载,这样比较节省流量,这样就有了我们很常见的上拉加载更多的功能,具体的实现效果如下图:

这里写图片描述

该activity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="io.geek.myapplication.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>

底部加载更多的view布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:gravity="center"
              android:layout_height="wrap_content">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="正在加载..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

底部提示到底的view布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  • 11
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 35
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值