package com.byl.recyclerview.view;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.byl.recyclerview.R;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author dinus
*/
public abstract class PullBaseView<T extends RecyclerView> extends LinearLayout {
protected T mRecyclerView;
private boolean isCanScrollAtRereshing = false;//刷新时是否可滑动
private boolean isCanPullDown = true;//是否可下拉
private boolean isCanPullUp = true;//是否可上拉
// pull state
private static final int PULL_UP_STATE = 0;
private static final int PULL_DOWN_STATE = 1;
// refresh states
private static final int PULL_TO_REFRESH = 2;
private static final int RELEASE_TO_REFRESH = 3;
private static final int REFRESHING = 4;
private int mLastMotionY;
//headerview
private View mHeaderView;
private int mHeaderViewHeight;
private ImageView mHeaderImageView;
private TextView mHeaderTextView;
private TextView mHeaderUpdateTextView;
private ProgressBar mHeaderProgressBar;
//footerview
private View mFooterView;
private int mFooterViewHeight;
private TextView mFooterTextView;
private ProgressBar mFooterProgressBar;
private LayoutInflater mInflater;
private int mHeaderState;
private int mFooterState;
private int mPullState;
private RotateAnimation mFlipAnimation;//变为向下的箭头,改变箭头方向
private RotateAnimation mReverseFlipAnimation;//变为逆向的箭头,旋转
private OnRefreshListener refreshListener;
public PullBaseView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public PullBaseView(Context context) {
super(context);
}
/**
* init
*
* @description hylin 2012-7-26上午10:08:33
*/
private void init(Context context, AttributeSet attrs) {
// Load all of the animations we need in code rather than through XML
mFlipAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
mFlipAnimation.setInterpolator(new LinearInterpolator());
mFlipAnimation.setDuration(250);
mFlipAnimation.setFillAfter(true);
mReverseFlipAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
mReverseFlipAnimation.setDuration(250);
mReverseFlipAnimation.setFillAfter(true);
mInflater = LayoutInflater.from(getContext());
// header view 在此添加,保证是第一个添加到linearlayout的最上端
addHeaderView();
mRecyclerView = createRecyclerView(context, attrs);
mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
addView(mRecyclerView);
}
private void addHeaderView() {
// header view
mHeaderView = mInflater.inflate(R.layout.refresh_header, this, false);
mHeaderImageView = (ImageView) mHeaderView.findViewById(R.id.pull_to_refresh_image);
mHeaderTextView = (TextView) mHeaderView.findViewById(R.id.pull_to_refresh_text);
mHeaderUpdateTextView = (TextView) mHeaderView.findViewById(R.id.pull_to_refresh_updated_at);
mHeaderProgressBar = (ProgressBar) mHeaderView.findViewById(R.id.pull_to_refresh_progress);
mHeaderUpdateTextView.setText("最近更新:" + getFormatDateString("MM-dd HH:mm"));
// header layout
measureView(mHeaderView);
mHeaderViewHeight = mHeaderView.getMeasuredHeight();
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mHeaderViewHeight);
// 设置topMargin的值为负的header View高度,即将其隐藏在最上方
params.topMargin = -(mHeaderViewHeight);
// mHeaderView.setLayoutParams(params1);
addView(mHeaderView, params);
}
public static final String getFormatDateString(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
private void addFooterView() {
// footer view
mFooterView = mInflater.inflate(R.layout.refresh_footer, this, false);
mFooterTextView = (TextView) mFooterView.findViewById(R.id.pull_to_load_text);
mFooterProgressBar = (ProgressBar) mFooterView.findViewById(R.id.pull_to_load_progress);
measureView(mFooterView);
mFooterViewHeight = mFooterView.getMeasuredHeight();
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mFooterViewHeight);
addView(mFooterView, params);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
addFooterView();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {//刷新时禁止滑动
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (!isCanScrollAtRereshing) {
if (mHeaderState == REFRESHING || mFooterState == REFRESHING) {
return true;
}
}
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int y = (int) e.getRawY();
int x = (int) e.getRawX();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
// 首先拦截down事件,记录y坐标
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
// deltaY > 0 是向下运动,< 0是向上运动
int deltaY = y - mLastMotionY;
if (isRefreshViewScroll(deltaY)) {
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return false;
}
/*
* 如果在onInterceptTouchEvent()方法中没有拦截(即onInterceptTouchEvent()方法中 return
* false)PullBaseView 的子View来处理;否则由下面的方法来处理(即由PullToRefreshView自己来处理)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
int y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int deltaY = y - mLastMotionY;
if (isCanPullDown && mPullState == PULL_DOWN_STATE) {
headerPrepareToRefresh(deltaY);
} else if (isCanPullUp && mPullState == PULL_UP_STATE) {
footerPrepareToRefresh(deltaY);
}
mLastMotionY = y;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
int topMargin = getHeaderTopMargin();
if (isCanPullDown && mPullState == PULL_DOWN_STATE) {
if (topMargin >= 0) {
// 开始刷新
headerRefreshing();
} else {
// 还没有执行刷新,重新隐藏
setHeaderTopMargin(-mHeaderViewHeight);
}
} else
没有合适的资源?快使用搜索试试~ 我知道了~
Recyclerview下拉刷新上拉加载及adapter封装
共67个文件
xml:28个
java:9个
png:9个
5星 · 超过95%的资源 需积分: 33 138 下载量 43 浏览量
2016-05-27
14:53:04
上传
评论 3
收藏 187KB RAR 举报
温馨提示
Recyclerview下拉刷新上拉加载及adapter封装
资源推荐
资源详情
资源评论
收起资源包目录
Recyclerview.rar (67个子文件)
Recyclerview
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
Recyclerview.iml 943B
.gradle
2.10
taskArtifacts
fileHashes.bin 125KB
cache.properties.lock 17B
taskArtifacts.bin 33KB
fileSnapshots.bin 405KB
cache.properties 31B
outputFileStates.bin 19KB
app
proguard-rules.pro 691B
src
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable
item_selector.xml 249B
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 302B
strings.xml 554B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_pulltorefresh_arrow.png 823B
fop.png 405B
ic_pulltorefresh_arrow_up.png 912B
fox.png 247B
layout
activity_2.xml 499B
activity_main.xml 823B
activity_1.xml 465B
refresh_footer.xml 799B
item_text.xml 623B
refresh_header.xml 2KB
item_info.xml 2KB
values-w820dp
dimens.xml 364B
java
com
byl
recyclerview
bean
InfoBean.java 294B
view
BaseAdapter.java 4KB
PullRecyclerView.java 897B
PullBaseView.java 19KB
MainActivity.java 1KB
ui
Activity1.java 3KB
Activity2.java 4KB
adapter
TextAdapter.java 1KB
InfoAdapter.java 3KB
AndroidManifest.xml 781B
app.iml 9KB
.gitignore 7B
build.gradle 669B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
.idea
gradle.xml 739B
runConfigurations.xml 564B
misc.xml 882B
.name 12B
copyright
profiles_settings.xml 76B
modules.xml 361B
encodings.xml 159B
compiler.xml 686B
workspace.xml 137KB
libraries
appcompat_v7_23_3_0.xml 608B
support_vector_drawable_23_3_0.xml 663B
support_annotations_23_3_0.xml 541B
recyclerview_v7_23_3_0.xml 623B
support_v4_23_3_0.xml 752B
animated_vector_drawable_23_3_0.xml 668B
.gitignore 97B
local.properties 461B
settings.gradle 16B
build
build.gradle 521B
共 67 条
- 1
资源评论
- well_do2017-04-06还不错,值得学习借鉴!
- qq_280235292017-11-10还不错,可以看看
- 八零年代大表哥2017-11-11还不错,值得学习借鉴!
白玉梁
- 粉丝: 3978
- 资源: 81
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功