最近刚刚完成了一个项目然后顺便总结一下,项目中用RecyclerView列表比较多,同时会用到下拉刷新与上拉加载更多的功能。
主要原理还是通过SwipRefreshLayout和RecyclerView的滑动监听来实现的
先写布局文件吧
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
activity中主要的作用是做数据的加载和调配
package com.recyclerviewdemo.view.recycler;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import com.recyclerviewdemo.R;
import com.recyclerviewdemo.adapter.LoadRefreshAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* 带有下拉刷新与上拉加载的列表
*/
public class LoadRefreshActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private LoadRefreshAdapter adapter;
private int lastVisibleItem;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_refresh);
initView();
initData();
initEvent();
}
public void initView() {
recyclerView = (RecyclerView) findViewById(R.id.rv_recycler);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_refresh);
//设置默认显示加载进度条
swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
.getDisplayMetrics()));
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
//设置下拉刷新按钮的颜色
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
//设置RecyclerView的布局管理器
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
}
public void initData() {
List<String> list = new ArrayList<>();
for (int x = 0; x < 25; x++) {
list.add("当前是第" + x + "条数据");
}
adapter = new LoadRefreshAdapter(LoadRefreshActivity.this, list);
recyclerView.setAdapter(adapter);
}
public void initEvent() {
//下拉刷新功能
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<String> list = new ArrayList<>();
for (int x = 1; x < 20; x++) {
list.add("下拉刷新数据" + x);
}
adapter.addItemData(list);
swipeRefreshLayout.setRefreshing(false);
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
}
}, 500);
}
});
// 上拉加载更多数据
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(final RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
int index = i + 1;
data.add("上拉加载数据" + index);
}
adapter.addItemData(data);
recyclerView.scrollToPosition(adapter.getItemCount());
}
}, 200);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
}
});
}
}
适配器Adapter中没有做抽取,为了方便查阅,主要是通过一个viewType的方式完成view的布局显示,具体代码入下
package com.recyclerviewdemo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.recyclerviewdemo.R;
import java.util.List;
/**
* 具有下拉刷新上拉加载功能的适配器
*/
public class LoadRefreshAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_ITEM = 0; //普通Item View
private static final int TYPE_FOOTER = 1; //顶部FootView
private Context context;
private List<String> data;
/**
* 适配器是否为加载中状态:加载中不能添加数据
*/
private boolean isLoading;
/**
* 构造方法
*
* @param context
* @param data
*/
public LoadRefreshAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_ITEM:
View view = View.inflate(context, R.layout.item_load_refresh, null);
return new LoadRefreshHolder(view);
case TYPE_FOOTER:
View title = View.inflate(context, R.layout.item_load_title, null);
return new LoadTitle(title);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof LoadRefreshHolder) {
((LoadRefreshHolder) holder).tvTitle.setText(data.get(position));
}
}
@Override
public int getItemCount() {
if (data != null) {
// 加入一个viewType
return data.size() + 1;
}
return 0;
}
@Override
public int getItemViewType(int position) {
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
class LoadRefreshHolder extends RecyclerView.ViewHolder {
private TextView tvTitle;
public LoadRefreshHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
}
}
class LoadTitle extends RecyclerView.ViewHolder {
public LoadTitle(View itemView) {
super(itemView);
}
}
/**
* 给数据源添加数据
*
* @param list 数据
*/
public void addItemData(List<String> list) {
data.addAll(list);
notifyDataSetChanged();
}
/**
* 设置当前的状态
*
* @param isLoading
*/
public void setLoading(boolean isLoading) {
this.isLoading = isLoading;
}
public boolean getLoading() {
return isLoading;
}
}