<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_refresh" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/swipe_target" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" /> </android.support.v4.widget.SwipeRefreshLayout>
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
/**
* 获取布局参数
*/
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//如果为null,第一次运行,确定布局类型
if (mLayoutManagerType == null) {
if (layoutManager instanceof LinearLayoutManager) {
mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
} else if (layoutManager instanceof GridLayoutManager) {
mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
} else {
throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
}
}
//对于不太能够的布局参数,不同的方法获取到当前显示的最后一个条目数
switch (mLayoutManagerType) {
case LINEAR_LAYOUT:
mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case GRID_LAYOUT:
mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case STAGGERED_GRID_LAYOUT:
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
if (mLastPostions == null) {
mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
}
staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
mLastVisibleItemPosition = findMax(mLastPostions);
break;
default:
break;
}
}
/**
*
* RecycleView的布局管理器的类型
* Created by Alex_MaHao on 2016/5/10.
*/
public enum LayoutManagerType {
LINEAR_LAYOUT,
GRID_LAYOUT,
STAGGERED_GRID_LAYOUT
}
/**
* 当是瀑布流时,获取到的是每一个瀑布最下方显示的条目,通过条目进行对比
*/
private int findMax(int[] lastPositions) {
int max = lastPositions[0];
for (int value : lastPositions) {
if (value > max) {
max = value;
}
}
return max;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//RecycleView 显示的条目数
int visibleCount = layoutManager.getChildCount();
//显示数据总数
int totalCount = layoutManager.getItemCount();
// 四个条件,分别是是否有数据,状态是否是滑动停止状态,显示的最大条目是否大于整个数据(注意偏移量),是否正在加载数据
if(visibleCount>0
&&newState==RecyclerView.SCROLL_STATE_IDLE
&&mLastVisibleItemPosition>=totalCount-1
&&!isLoadData){
//可以加载数据
isLoadData = true;
}
}
public void setLoadDataStatus(boolean isLoadData){
this.isLoadData = isLoadData;
}
/**
* 实现上拉加载的监听:加载条件:滑动到最后,且是停止状态,则开始加载数据
* Created by Alex_MaHao on 2016/5/10.
*/
public class LoadDataScrollController extends RecyclerView.OnScrollListener implements SwipeRefreshLayout.OnRefreshListener {
/**
* 当前布局管理器的类型
*/
private LayoutManagerType mLayoutManagerType;
/**
* 当前RecycleView显示的最大条目
*/
private int mLastVisibleItemPosition;
/**
* 每列的最后一个条目
*/
private int[] mLastPostions;
/**
* 是否正在加载数据 包括刷新和向上加载更多
*/
private boolean isLoadData = false;
/**
* 回调接口
*/
private OnRecycleRefreshListener mListener;
public LoadDataScrollController(OnRecycleRefreshListener onRecycleRefreshListener) {
this.mListener = onRecycleRefreshListener;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
/**
* 获取布局参数
*/
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//如果为null,第一次运行,确定布局类型
if (mLayoutManagerType == null) {
if (layoutManager instanceof LinearLayoutManager) {
mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
} else if (layoutManager instanceof GridLayoutManager) {
mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
} else {
throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
}
}
//对于不太能够的布局参数,不同的方法获取到当前显示的最后一个条目数
switch (mLayoutManagerType) {
case LINEAR_LAYOUT:
mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case GRID_LAYOUT:
mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case STAGGERED_GRID_LAYOUT:
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
if (mLastPostions == null) {
mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
}
staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
mLastVisibleItemPosition = findMax(mLastPostions);
break;
default:
break;
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//RecycleView 显示的条目数
int visibleCount = layoutManager.getChildCount();
//显示数据总数
int totalCount = layoutManager.getItemCount();
// 四个条件,分别是是否有数据,状态是否是滑动停止状态,显示的最大条目是否大于整个数据(注意偏移量),是否正在加载数据
if(visibleCount>0
&&newState==RecyclerView.SCROLL_STATE_IDLE
&&mLastVisibleItemPosition>=totalCount-1
&&!isLoadData){
//可以加载数据
if(mListener!=null){
isLoadData = true;
mListener.loadMore();
}
}
}
/**
* 当是瀑布流时,获取到的是每一个瀑布最下方显示的条目,通过条目进行对比
*/
private int findMax(int[] lastPositions) {
int max = lastPositions[0];
for (int value : lastPositions) {
if (value > max) {
max = value;
}
}
return max;
}
public void setLoadDataStatus(boolean isLoadData){
this.isLoadData = isLoadData;
}
@Override
public void onRefresh() {
//刷新数据的方法
if(mListener!=null){
isLoadData = true;
mListener.refresh();
}
}
/**
* 数据加载接口回调
*/
interface OnRecycleRefreshListener{
void refresh();
void loadMore();
}
}
/**
* 使用原生的SwipeRefreshLayout和代码判断
* 实现RecyclewView 的刷新和加载更多
*
* Created by Alex_MaHao on 2016/5/10.
*/
public class SwipeRefreshActivity extends AppCompatActivity implements LoadDataScrollController.OnRecycleRefreshListener {
private SwipeRefreshLayout mSwipeRefresh;
private RecyclerView mRecycle;
private HomeAdapter mAdapter;
private LoadDataScrollController mController;
private ProgressDialog pd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_swiperefresh);
mRecycle = ((RecyclerView) findViewById(R.id.swipe_target));
mSwipeRefresh = ((SwipeRefreshLayout) findViewById(R.id.swipe_refresh));
mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
/**
* 创建控制器,同时使当前activity实现数据监听回调接口
*/
mController = new LoadDataScrollController(this);
mAdapter = new HomeAdapter();
//设置垂直的线性布局管理器,Orientation --> VERTICAL:垂直 HORIZONTAL:水平
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//添加分割线
mRecycle.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));
mRecycle.setLayoutManager(layoutManager);
mRecycle.setItemAnimator(new DefaultItemAnimator());
mRecycle.setAdapter(mAdapter);
mAdapter.refresh();
/**
* 设置监听
*/
mRecycle.addOnScrollListener(mController);
mSwipeRefresh.setOnRefreshListener(mController);
}
@Override
public void refresh() {
//刷新的接口调
mSwipeRefresh.postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.refresh();
mSwipeRefresh.setRefreshing(false);
mController.setLoadDataStatus(false);
}
},2000);
}
@Override
public void loadMore() {
//加载更多的接口回调
pd = new ProgressDialog(this);
pd.show();
mSwipeRefresh.postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.add();
//设置数据加载结束的监听状态
mController.setLoadDataStatus(false);
pd.dismiss();
}
},2000);
}
}
repositories {
maven { url "https://jitpack.io" }
}
compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.3'
/**
* 基础的refreshHeadView
*/
public class RefreshHeaderView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {
public RefreshHeaderView(Context context) {
super(context);
}
public RefreshHeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onRefresh() {
//下拉到一定位置松开之后,调用此方法
setText("refresh");
Log.i("info","onRefresh");
}
@Override
public void onPrepare() {
//下拉之前调用此方法
Log.i("info","onPrepare");
}
@Override
public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
if (!isComplete) {
//当前Y轴偏移量大于控件高度时,标识下拉到界限,显示“松开已刷新”
if (yScrolled >= getHeight()) {
} else {
//未达到偏移量
}
}
Log.i("info","onMove");
}
@Override
public void onRelease() {
//达到一定滑动距离,松开刷新时调用
setText("onRelease");
Log.i("info","onRelease");
}
@Override
public void onComplete() {
//加载完成之后调用此方法
setText("complete");
Log.i("info","onComplete");
}
@Override
public void onReset() {
//重置
setText("onReset");
Log.i("info","onReset");
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ECEDF0" > <com.aspsine.swipetoloadlayout.SwipeToLoadLayout android:id="@+id/swipeToLoadLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.RefreshHeaderView android:id="@+id/swipe_refresh_header" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/swipe_target" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.LoaderMoreView android:id="@+id/swipe_load_more_footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="20dp" /> </com.aspsine.swipetoloadlayout.SwipeToLoadLayout> </RelativeLayout>
/**
* CircleView 圆盘控件,可以旋转
* Created by Alex_MaHao on 2016/5/10.
*/
public class CircleView extends View {
/**
* 控件的半径
*/
private int mRadius;
/**
* 绘制弧形的画笔
*/
private Paint mArcPaint;
/**
* 绘制弧形的区域
*/
private RectF mRange;
private int[] colors = {Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN};
public CircleView(Context context) {
this(context, null, 0);
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mArcPaint = new Paint();
mArcPaint.setAntiAlias(true);
mArcPaint.setDither(true);
mArcPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = 0;
int height = 0;
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
}
//获取半径
mRadius = Math.min(width, height) / 2;
/**
* 设置宽高为固定值
*/
setMeasuredDimension(mRadius * 2, mRadius * 2);
mRange = new RectF(0, 0, mRadius * 2, mRadius * 2);
}
@Override
protected void onDraw(Canvas canvas) {
float degree = 360/colors.length/2f;
for (int i = 0; i < 8; i++) {
mArcPaint.setColor(colors[i%4]);
canvas.drawArc(mRange,-90f+degree*i,degree,true,mArcPaint);
}
}
}
**
* 自定义的下拉刷新控件 头部
* Created by Alex_MaHao on 2016/5/10.
*/
public class CircleRefreshHeaderView extends RelativeLayout implements SwipeTrigger, SwipeRefreshTrigger {
CircleView mCircleView;
TextView mDescText;
private ObjectAnimator anim;
private boolean isRelease;
public CircleRefreshHeaderView(Context context) {
this(context, null, 0);
}
public CircleRefreshHeaderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleRefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化布局
*/
private void initView() {
int circlewidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());
mCircleView = new CircleView(getContext());
LinearLayout.LayoutParams circleParams = new LinearLayout.LayoutParams(circlewidth,circlewidth);
mCircleView.setLayoutParams(circleParams);
mDescText = new TextView(getContext());
LinearLayout.LayoutParams descParams = new LinearLayout.LayoutParams(circlewidth*3, ViewGroup.LayoutParams.WRAP_CONTENT);
descParams.gravity = Gravity.CENTER;
descParams.setMargins(circlewidth/2,0,0,0);
mDescText.setLayoutParams(descParams);
mDescText.setTextSize(12);
mDescText.setTextColor(Color.GRAY);
mDescText.setText("下拉刷新");
//添加线性的父布局
LinearLayout ll = new LinearLayout(getContext());
RelativeLayout.LayoutParams llParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llParams.addRule(CENTER_IN_PARENT);
ll.setLayoutParams(llParams);
ll.setPadding(10,10,10,10);
ll.addView(mCircleView);
ll.addView(mDescText);
addView(ll);
}
@Override
public void onRefresh() {
//开始刷新,启动动画
anim = ObjectAnimator.ofFloat(mCircleView, "rotation", mCircleView.getRotation(), mCircleView.getRotation()+360f)
.setDuration(500);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.RESTART);
anim.start();
mDescText.setText("正在加载数据");
}
@Override
public void onPrepare() {
isRelease = false;
}
@Override
public void onMove(int yScroll, boolean isComplete, boolean b1) {
if (!isComplete) {
if (yScroll < getHeight()) {
mDescText.setText("下拉刷新");
} else {
mDescText.setText("松开刷新更多");
}
//如果是仍在下拉状态,则圆环跟随滑动进行滚动
if (!isRelease)
mCircleView.setRotation(((float) yScroll) / getHeight() * 360f);
}
}
@Override
public void onRelease() {
isRelease = true;
}
@Override
public void onComplete() {
anim.cancel();
mDescText.setText("加载完成");
}
@Override
public void onReset() {
//重置时,将动画置为初始状态
mCircleView.setRotation(0f);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ECEDF0" > <com.aspsine.swipetoloadlayout.SwipeToLoadLayout android:id="@+id/swipeToLoadLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.CircleRefreshHeaderView android:id="@+id/swipe_refresh_header" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/swipe_target" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.LoaderMoreView android:id="@+id/swipe_load_more_footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="20dp" /> </com.aspsine.swipetoloadlayout.SwipeToLoadLayout> </RelativeLayout>
public class SwipeToLayoutActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
private RecyclerView mRecycleView;
SwipeToLoadLayout swipeToLoadLayout;
private HomeAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_swipetolayout);
swipeToLoadLayout = ((SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout));
mRecycleView = ((RecyclerView) findViewById(R.id.swipe_target));
adapter = new HomeAdapter();
//设置垂直的线性布局管理器,Orientation --> VERTICAL:垂直 HORIZONTAL:水平
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//添加分割线
mRecycleView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));
mRecycleView.setLayoutManager(layoutManager);
mRecycleView.setItemAnimator(new DefaultItemAnimator());
mRecycleView.setAdapter(adapter);
adapter.refresh();
/**
* 设置下拉刷新和上拉加载监听
*/
swipeToLoadLayout.setOnRefreshListener(this);
swipeToLoadLayout.setOnLoadMoreListener(this);
}
@Override
public void onRefresh() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
adapter.refresh();
swipeToLoadLayout.setRefreshing(false);
}
},2000);
}
@Override
public void onLoadMore() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
adapter.add();
swipeToLoadLayout.setLoadingMore(false);
}
},2000);
}
}
this.mHeaderView = this.findViewById(id.swipe_refresh_header); this.mTargetView = this.findViewById(id.swipe_target); this.mFooterView = this.findViewById(id.swipe_load_more_footer);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有