Android RecyclerView实现下拉刷新和上拉加载更多
作者:kklxb 时间:2022-03-16 09:02:40
使用官方的刷新控件SwipeRefreshLayout来实现下拉刷新,当RecyclerView滑到底部实现下拉加载(进度条效果用RecyclerView加载一个布局实现)
需要完成控件的下拉监听和上拉监听,其中,下拉监听通过SwipRefreshLayout的setOnRefreshListener()方法监听,而上拉刷新,需要通过监听列表的滚动,当列表滚动到底部时触发事件,具体代码如下
主布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout refreshLayout;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private RecyclerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
recyclerView = (RecyclerView) findViewById(R.id.recycler_list);
layoutManager = new LinearLayoutManager(this);
refreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary);//设置刷新时进度条
颜色,最多四种
refreshLayout.setOnRefreshListener(this);
mAdapter = new RecyclerAdapter();//自定义的适配器
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new OnRecyclerScrollListener());
}
/**
* 用于下拉刷新
*/
@Override
public void onRefresh() {
}
/**
* 用于上拉加载更多
*/
public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {
int lastVisibleItem = 0;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (mAdapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 ==
mAdapter.getItemCount()) {
//滚动到底部了,可以进行数据加载等操作
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = layoutManager.findLastVisibleItemPosition();
}
}
}
下面是实现上拉时进度条转动的效果
item_list_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_item_footer_load_more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center"
android:text="上拉加载更多"
/>
<ProgressBar
android:id="@+id/pb_item_footer_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:visibility="gone"/>
</RelativeLayout>
适配器
public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {
private static final int TYPE_CONTENT = 0;
private static final int TYPE_FOOTER = 1;
private ArrayList<DataBean> dataList;
private ProgressBar pbLoading;
private TextView tvLoadMore;
public RecyclerAdapter() {
dataList = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_CONTENT) {
return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate
(R.layout.item_list_content, parent, false));
} else if (viewType == TYPE_FOOTER) {//加载进度条的布局
return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate
(R.layout.item_list_footer, parent, false));
}
return null;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int type = getItemViewType(position);
if (type == TYPE_CONTENT) {
DataBean bean = dataList.get(position);
((ContentViewHolder) holder).tvId.setText("" + bean.getId());
((ContentViewHolder) holder).tvName.setText(bean.getName());
} else if (type == TYPE_FOOTER) {
pbLoading = ((FooterViewHolder) holder).pbLoading;
tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;
}
}
/**
* 获取数据集加上一个footer的数量
*/
@Override
public int getItemCount() {
return dataList.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
} else {
return TYPE_CONTENT;
}
}
/**
* 获取数据集的大小
*/
public int getListSize() {
return dataList.size();
}
/**
* 内容的ViewHolder
*/
public static class ContentViewHolder extends ViewHolder {
private TextView tvId, tvName;
public ContentViewHolder(View itemView) {
super(itemView);
tvId = (TextView) itemView.findViewById(R.id.tv_item_id);
tvName = (TextView) itemView.findViewById(R.id.tv_item_name);
}
}
/**
* footer的ViewHolder
*/
public static class FooterViewHolder extends ViewHolder {
private TextView tvLoadMore;
private ProgressBar pbLoading;
public FooterViewHolder(View itemView) {
super(itemView);
tvLoadMore = (TextView) itemView.findViewById(R.id.tv_item_footer_load_more);
pbLoading = (ProgressBar) itemView.findViewById(R.id.pb_item_footer_loading);
}
}
/**
* 显示正在加载的进度条,滑动到底部时,调用该方法,上拉就显示进度条,隐藏"上拉加载更多"
*/
public void showLoading() {
if (pbLoading != null && tvLoadMore != null) {
pbLoading.setVisibility(View.VISIBLE);
tvLoadMore.setVisibility(View.GONE);
}
}
/**
* 显示上拉加载的文字,当数据加载完毕,调用该方法,隐藏进度条,显示“上拉加载更多”
*/
public void showLoadMore() {
if (pbLoading != null && tvLoadMore != null) {
pbLoading.setVisibility(View.GONE);
tvLoadMore.setVisibility(View.VISIBLE);
}
}
}
标签:RecyclerView,下拉刷新,上拉加载
0
投稿
猜你喜欢
C#缩略图多路径多格式保存的实例
2021-10-29 18:12:36
Spring Boot配置线程池拒绝策略的场景分析(妥善处理好溢出的任务)
2022-08-05 07:12:24
Spring-Bean创建对象的步骤方式详解
2023-04-12 22:00:47
Android编程之自定义锁屏实例分析
2022-02-02 04:01:50
java中找不到符号的解决方案
2023-09-01 17:50:11
冒泡排序的原理及java代码实现
2021-12-26 18:45:38
Java的优先队列PriorityQueue原理及实例分析
2023-03-14 08:11:00
Android App在线程中创建handler的方法讲解
2021-06-24 17:00:33
C#实现洗牌游戏实例
2021-07-07 00:45:29
Android实现指针刻度转盘
2022-06-08 07:41:42
Mybatis实现自定义类型转换器TypeHandler的方法
2023-09-28 14:52:22
Android开发实战之漂亮的ViewPager引导页
2023-04-28 01:36:38
Android如何获取QQ与微信的聊天记录并保存到数据库详解
2023-04-12 06:54:03
详解springmvc控制登录用户session失效后跳转登录页面
2021-12-08 19:36:40
Unity Shader实现黑幕过场效果
2022-01-13 00:18:10
C#中事件处理的个人体会
2023-11-02 02:14:39
C语言 auto和register关键字
2021-11-03 02:40:54
Java基于Socket实现HTTP下载客户端
2022-07-06 01:02:30
JAVA Integer类常用方法解析
2021-09-01 06:51:08
在Android系统中使用WebViewClient处理跳转URL的方法
2021-08-03 14:24:59