版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
封装含有上拉加载功能的RecyclerView,然后搭配SwipeRefreshLayout实现下拉刷新、上拉加载功能。
在项目中将原有的RecyclerView替换成WRecyclerView即可,不改动原有的adapter!
本Demo中演示了下拉刷新和分页功能,所以在将RecyclerView替换成WRecyclerView之后还需要添加其他代码(比如下拉刷新控件、无数据布局区域、分页相关代码),具体见Demo。
效果图
代码分析
WRecyclerView:自定义RecyclerView子类,在不改动 RecyclerView 原有 adapter 的情况下,使其拥有加载更多功能和自定义底部视图;
WRecyclerViewAdapter:自定义RecyclerView适配器;
WRecyclerViewFooter:自定义RecyclerView的底部上拉加载区域。
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
(1)在build.gradle中引用recyclerview【版本号和appcompat保持一致】
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.why.project.recyclerviewloadmoredemo"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//RecyclerView
compile "com.android.support:recyclerview-v7:27.1.1"
}
(2)在项目中实现Recyclerview基本数据展现
1、创建Bean类
package com.why.project.recyclerviewloadmoredemo.bean; /** * Created by HaiyuKing * Used 列表项的bean类 */ public class NewsBean { private String newsId;//id值 private String newsTitle;//标题 public String getNewsId() { return newsId; } public void setNewsId(String newsId) { this.newsId = newsId; } public String getNewsTitle() { return newsTitle; } public void setNewsTitle(String newsTitle) { this.newsTitle = newsTitle; } }