使用SwipeRefreshLayout实现RecylerView、ListView 下拉刷新 上拉加载

2018-02-08 10:24:59来源:oschina作者:bluecoffee人点击

分享

使用SwipeRefreshLayout实现RecylerView、ListView 下拉刷新


首先,自定义view继承SwipeRefreshLayout,代码如下:


import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.zqsy.keepsample.R;
import com.zqsy.keepsample.activity.OperateSampleListActivity;/**
* 自定义View继承SwipeRefreshLayout,添加上拉加载更多的布局属性,添加对RecyclerView的支持
* Created by zhangda on 2016/9/26.
*/
public class SwipeRefreshView extends SwipeRefreshLayout {
private static final String TAG = SwipeRefreshView.class.getSimpleName();
private final int mScaledTouchSlop;
private final View mFooterView;
private final View mFooterView_nomore;
private ListView mListView;
private OnLoadMoreListener mListener;
/**
* 正在加载状态
*/
private boolean isLoading;
private RecyclerView mRecyclerView;
private int mItemCount;
private boolean isRecyclerView = false;
private RecyclerView.LayoutManager layoutManager;
private LinearLayoutManager linearManager;
public SwipeRefreshView(Context context, AttributeSet attrs) {
super(context, attrs);
// 填充底部加载布局
mFooterView = View.inflate(context, R.layout.view_footer, null);
mFooterView_nomore = View.inflate(context, R.layout.view_footer_nomore, null);
// 表示控件移动的最小距离,手移动的距离大于这个距离才能拖动控件
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// 获取ListView,设置ListView的布局位置
if (mListView == null || mRecyclerView == null) {
// 判断容器有多少个孩子
if (getChildCount() > 1) {
// 判断第一个孩子是不是ListView
if (getChildAt(1) instanceof ListView) {
// 创建ListView对象
mListView = (ListView) getChildAt(1);
// 设置ListView的滑动监听
setListViewOnScroll();
} else if (getChildAt(1) instanceof RecyclerView) {
// 创建ListView对象
mRecyclerView = (RecyclerView) getChildAt(1);
layoutManager = mRecyclerView.getLayoutManager();
linearManager = (LinearLayoutManager) layoutManager;
isRecyclerView = true;
// 设置RecyclerView的滑动监听
setRecyclerViewOnScroll();
}
}
}
}/**
* 在分发事件的时候处理子控件的触摸事件
*/
private float mDownY, mUpY;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 移动的起点
mDownY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
break;
case MotionEvent.ACTION_UP:
// 移动的终点
mUpY = getY();
break;
}
return super.dispatchTouchEvent(ev);
}
/**
* 判断是否满足加载更多条件
*/
private boolean canLoadMore() {
// 1. 是上拉状态
boolean condition1 = (mDownY - mUpY) >= mScaledTouchSlop;
if (condition1) {
Log.d(TAG, "------->是上拉状态");
}
// 2. 当前页面可见的item是最后一个条目,一般最后一个条目位置需要大于第一页的数据长度
boolean condition2 = false;
int mListCount = 0;
int mLastVisiblePosition = 0;
if(isRecyclerView){
mListCount = linearManager.getItemCount();
//获取最后一个可见view的位置
mLastVisiblePosition = linearManager.findLastVisibleItemPosition();
}else{
mListCount = mListView.getAdapter().getCount();
mLastVisiblePosition = mListView.getLastVisiblePosition();
}
if (mItemCount > 0) {
if (mListCount < mItemCount) {
// 第一页未满,禁止下拉
condition2 = false;
}else {
condition2 = mLastVisiblePosition == (mListCount - 1);
}
} else {
// 未设置数据长度,则默认第一页数据不满时也可以上拉
condition2 = mLastVisiblePosition == (mListCount - 1);
} if (condition2) {
Log.d(TAG, "------->是最后一个条目");
}
// 3. 正在加载状态
boolean condition3 = !isLoading;
if (condition3) {
Log.d(TAG, "------->不是正在加载状态");
}
return condition1 && condition2 && condition3;
}
public void setItemCount(int itemCount) {
this.mItemCount = itemCount;
}
/**
* 处理加载数据的逻辑
*/
private void loadData() {
System.out.println("加载数据...");
if (mListener != null) {
// 设置加载状态,让布局显示出来
setLoading(true);
if(!isRecyclerView){
if(mListView.getFooterViewsCount()>0){
mListView.removeFooterView(mFooterView_nomore);
}
}
mListener.onLoadMore();
}
}
/**
* 设置加载状态,是否加载传入boolean值进行判断
*
* @param loading
*/
public void setLoading(boolean loading) {
// 修改当前的状态
isLoading = loading;
if (isLoading) {
// 显示布局
if(isRecyclerView){
//OperateSampleListActivity.MyApr myApr = (OperateSampleListActivity.MyApr)mRecyclerView.getAdapter();
//myApr.setFooterView(mFooterView);
}else{
mListView.addFooterView(mFooterView);
}
} else {
// 隐藏布局
if(isRecyclerView){
//mRecyclerView.removeView(mFooterView);
}else{
mListView.removeFooterView(mFooterView);
}
// 重置滑动的坐标
mDownY = 0;
mUpY = 0;
}
}
public void showNoMoreText(){
if(!isRecyclerView){
mListView.addFooterView(mFooterView_nomore);
}
}/**
* 设置ListView的滑动监听
*/
private void setListViewOnScroll() {
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}/**
* 设置RecyclerView的滑动监听
*/
private void setRecyclerViewOnScroll() {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// 移动过程中判断时候能下拉加载更多
if (canLoadMore()) {
// 加载数据
loadData();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}/**
* 上拉加载的接口回调
*/
public interface OnLoadMoreListener {
void onLoadMore();
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
this.mListener = listener;
}
}

Listview布局代码:


android:id="@+id/swipeRefreshView"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:fadingEdge="none"
android:dividerHeight="0.5dp"
android:divider="@color/colorGrey"
android:id="@+id/listview_record"
android:layout_width="match_parent"
android:layout_height="match_parent" />

footer布局代码:


<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="48dp">

android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="正在努力加载中..."
android:textColor="@android:color/black"
android:textSize="16sp"/>


<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="48dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="没有更多记录了"
android:textColor="@color/colorFontColor"
android:textSize="16sp"/>


ListView实现刷新和翻页:
设定如下变量


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.zqsy.keepsample.R;
import com.zqsy.keepsample.component.SwipeRefreshView;
/**
* Created by zhangda on 2018/1/30.
*/
public class RecordActivity extends AppCompatActivity {
private TextView title_nav;
private TextView back_nav;
private ListView listView;
private SwipeRefreshView swipeRefreshView;
private BaseAdapter myAdapter;
private int pageSize = 20;//设定每页条数
private int currentPage = 1;//当前请求的页数
private int totalCount = 32;//总条数,实际是请求回来赋值@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
listView = findViewById(R.id.listview_record);
swipeRefreshView = findViewById(R.id.swipeRefreshView);
title_nav = findViewById(R.id.title_nav);
back_nav = findViewById(R.id.back_nav);
title_nav.setText("记录");
back_nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
initListView();
}
private void initListView() {
swipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
// 设置下拉进度的主题颜色
swipeRefreshView.setColorSchemeResources(R.color.colorAccent,
android.R.color.holo_blue_bright, R.color.colorPrimaryDark,
android.R.color.holo_orange_dark, android.R.color.holo_red_dark, android.R.color.holo_purple);
// 下拉时触发SwipeRefreshLayout的下拉动画,动画完毕之后就会回调这个方法
swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initData();
}
});
// 设置下拉加载更多
swipeRefreshView.setOnLoadMoreListener(new SwipeRefreshView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
loadMoreData();
}
});
myAdapter = new BaseAdapter() {
@Override
public int getCount() {
return pageSize * currentPage >= totalCount? totalCount: pageSize * currentPage;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
if(convertView == null){
convertView = LayoutInflater.from(RecordActivity.this).inflate(R.layout.item_record_list, null, false);
}
TextView tv2 = convertView.findViewById(R.id.text2);
tv2.setText(i + 1 + "");
return convertView;
}
};
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent();
intent.setClass(RecordActivity.this, RecordDetailActivity.class);
startActivity(intent);
}
});
}
//上拉加载
private void loadMoreData() {
//TODO 写逻辑时把定时器去掉即可
//如果记录加载完毕,调用显示没有更多的方法
if(pageSize * currentPage >= totalCount){
swipeRefreshView.showNoMoreText();
swipeRefreshView.setLoading(false);
}else{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
currentPage ++;
myAdapter.notifyDataSetChanged();
//Toast.makeText(RecordActivity.this, "加载了" + 20 + "条数据", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不加载状态,将加载进度收起来
swipeRefreshView.setLoading(false);
}
}, 2000);
}
}
//下拉刷新
private void initData() {
//TODO 写逻辑时把定时器去掉即可
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
currentPage = 1;
myAdapter.notifyDataSetChanged();
//Toast.makeText(RecordActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不刷新状态,将下拉进度收起来
if (swipeRefreshView.isRefreshing()) {
swipeRefreshView.setRefreshing(false);
}
}
}, 2000);
}
}

RecylerView实现刷新和加载:


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.zqsy.keepsample.R;
import com.zqsy.keepsample.component.SwipeRefreshView;
/**
* Created by zhangda on 2018/1/27.
*/
public class OperateSampleListActivity extends AppCompatActivity {
private RecyclerView recyclerView_chuli;
private int screenWidth;
private TextView titleNav;
private TextView backNav;
private Spinner spinner_chuli;
private Button piliangchuli;
private SwipeRefreshView layout_swipe_refresh;
private MyApr myAdapter;
private LinearLayoutManager mLinearLayoutManager;
private int pageSize = 20;//设定每页条数
private int currentPage = 1;//当前请求的页数
private int totalCount = 12;//总条数,实际是请求回来赋值
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operate_sample_list);
spinner_chuli = findViewById(R.id.spinner_chuli);
piliangchuli = findViewById(R.id.piliangchuli);
layout_swipe_refresh = findViewById(R.id.layout_swipe_refresh);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
screenWidth = wm.getDefaultDisplay().getWidth();
recyclerView_chuli = findViewById(R.id.recyclerView_chuli);
titleNav = findViewById(R.id.title_nav);
titleNav.setText("处理");
backNav = findViewById(R.id.back_nav);
backNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
initRecyclerView();

}private void initRecyclerView() {
recyclerView_chuli.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyApr();
recyclerView_chuli.setAdapter(myAdapter);
layout_swipe_refresh.setProgressBackgroundColorSchemeResource(android.R.color.white);
// 设置下拉进度的主题颜色
layout_swipe_refresh.setColorSchemeResources(R.color.colorAccent,
android.R.color.holo_blue_bright, R.color.colorPrimaryDark,
android.R.color.holo_orange_dark, android.R.color.holo_red_dark, android.R.color.holo_purple);
// 下拉时触发SwipeRefreshLayout的下拉动画,动画完毕之后就会回调这个方法
layout_swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initData();
}
});
// 设置下拉加载更多
layout_swipe_refresh.setOnLoadMoreListener(new SwipeRefreshView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
loadMoreData();
}
});
}//上拉加载
private void loadMoreData() {
//如果记录加载完毕,调用显示没有更多的方法
if(pageSize * currentPage >= totalCount){
layout_swipe_refresh.setLoading(false);
}else{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
currentPage ++;
myAdapter.notifyDataSetChanged();
Toast.makeText(OperateSampleListActivity.this, "加载了" + 20 + "条数据", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不加载状态,将加载进度收起来
layout_swipe_refresh.setLoading(false);
}
}, 2000);
}
}
//下拉刷新
private void initData() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
currentPage = 1;
myAdapter.notifyDataSetChanged();
Toast.makeText(OperateSampleListActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
// 加载完数据设置为不刷新状态,将下拉进度收起来
if (layout_swipe_refresh.isRefreshing()) {
layout_swipe_refresh.setRefreshing(false);
}
}
}, 2000);
}public class Hodler extends RecyclerView.ViewHolder{
public TextView item1;
public TextView item2;
public LinearLayout content_layout;
public int viewType;
public Hodler(View itemView, int viewType) {
super(itemView);
this.viewType = viewType;
if(viewType == MyApr.FOOTER_TYPE){
item1 = itemView.findViewById(R.id.item1);
return;
}else{
content_layout = itemView.findViewById(R.id.content_layout);
item1 = itemView.findViewById(R.id.item1);
item2 = itemView.findViewById(R.id.item2);
}
}
}
public class MyApr extends RecyclerView.Adapter{
public final static int NORMAL_TYPE = 0;
public final static int FOOTER_TYPE = 1;
@Override
public Hodler onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == FOOTER_TYPE){
return new Hodler(LayoutInflater.from(OperateSampleListActivity.this).inflate(R.layout.view_footer, null, false), viewType);
}else{
return new Hodler(LayoutInflater.from(OperateSampleListActivity.this).inflate(R.layout.item_samplelist_withoutdelete, null, false), viewType);
}
}
@Override
public void onBindViewHolder(final Hodler holder, final int position) {
if(holder.viewType == NORMAL_TYPE){
holder.item1.setWidth(screenWidth/2);
holder.item2.setWidth(screenWidth/2);
holder.item2.setText(position + 1 + "");
holder.content_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.setClass(OperateSampleListActivity.this, OperateDetailActivity.class);
startActivity(i);
}
});
}else{
if(pageSize * currentPage >= totalCount){
holder.item1.setText("没有更多记录了");
}else{
holder.item1.setText("正在努力加载中...");
}
}
}
//返回View中Item的个数,这个时候,总的个数应该是ListView中Item的个数加上HeaderView和FooterView
@Override
public int getItemCount() {
int count = pageSize * currentPage >= totalCount? totalCount: pageSize * currentPage;
return count+1;
}
@Override
public int getItemViewType(int position) {
if(position == (getItemCount()-1)){
return FOOTER_TYPE;
}else{
return NORMAL_TYPE;
}
}
}
}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台