親自動(dòng)手編寫Android通用刷新控件
項(xiàng)目中我們經(jīng)常有上拉、下拉刷新的需求,幾乎所有的listView、RecyclerView都會(huì)伴隨著上拉、下拉刷新的需求,如果我們使用一些開源控件,換了控件我們就要更新,現(xiàn)在我們自己擼起袖子寫一個(gè)通用的刷新控件
項(xiàng)目地址:https://git.oschina.net/qiangshen/commentview.git
思路:
- 寫一個(gè)繼承RelativeLayout的RefreshLayout
- 添加頭尾控件作為刷新控件
- 通過事件分發(fā)來進(jìn)行刷新操作
- 通過動(dòng)畫來控制控件移動(dòng)
目的:讓他的所有子控件都可以使用,哪怕是一個(gè)TextView
public class RefreshLayout extends RelativeLayout {
/**
* 滑動(dòng)控件時(shí)拉去的速度比例
*/
private final int V_REFRESH = 2;
/**
* 是否是刷新過程
* true 是
* false 不是
* 為false的時(shí)候才可以進(jìn)行刷新
*/
private boolean mIsRefreshDuring;
/**
* 可以進(jìn)下拉刷新
*/
private boolean mCanDownPull;
/**
* 可以進(jìn)行上拉刷新
*/
private boolean mCanUpPull;
/**
* 判斷觸摸后是否是初次移動(dòng)
*/
private boolean mIsFirstMove;
/**
* y軸呢平移的距離
*/
private int mDistanceY;
/**
* 刷新接口對(duì)象
*/
private OnRefresh mOnRefresh;
/**
* 用于控制事件攔截的變量
*/
private boolean mCanIntercept;
private int mTouchSlop;
private int mDistance;
private LayoutParams mHeaderParams;
private View mHeaderView;
private View mFootView;
private int mHeaderMaxHeight;
private int mStartY;
private LayoutParams mFootParams;
private int mFootMaxHeight;
private PullCallBack mCallBack;
private View mChildView;
private ObjectAnimator mAnimator;
public RefreshLayout(Context context) {
super(context);
initData();
}
public RefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initData();
}
public RefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
/**
* 必須讓頭尾控件實(shí)現(xiàn)的接口
*/
public interface HeadAndFootCallBack {
//設(shè)置屬性
void setAttribute();
//開始刷新
void startPull();
//停止刷新
void stopPull();
}
/**
* 必須讓被拖動(dòng)的控件子類實(shí)現(xiàn)
*/
public interface PullCallBack {
boolean canDownPull();
boolean canUpPull();
}
private void initData() {
//不調(diào)用該方法不能進(jìn)行繪制
setWillNotDraw(false);
}
/**
* 下拉刷新完成后必須使用該方法
*/
public void downPullFinish() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
((HeadAndFootCallBack) mHeaderView).stopPull();
}
/**
* 上拉完成后必須調(diào)用該方法
*/
public void upPullFinish() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
((HeadAndFootCallBack) mFootView).stopPull();
}
/**
* 自動(dòng)下拉刷新
*/
public void autoDownPullForHead() {
postDelayed(new Runnable() {
@Override
public void run() {
mCanDownPull = true;
mCanUpPull = false;
mAnimator.setFloatValues(10, mHeaderMaxHeight);
mAnimator.start();
((HeadAndFootCallBack) mHeaderView).startPull();
mOnRefresh.onDownPullRefresh();
}
}, 500);
}
/**
* 自動(dòng)下拉刷新
*/
public void autoUpPullForHead() {
postDelayed(new Runnable() {
@Override
public void run() {
mCanDownPull = false;
mCanUpPull = true;
mAnimator.setFloatValues(0, mFootMaxHeight);
mAnimator.start();
((HeadAndFootCallBack) mFootView).startPull();
mOnRefresh.onUpPullRefresh();
}
}, 500);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mCanIntercept;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e("shen", "mIsRefreshDuring=" + mIsRefreshDuring);
if (mIsRefreshDuring)
/**如果正在進(jìn)行刷新將不會(huì)獲取MotionEvent*/
{
return super.dispatchTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartY = (int) event.getY();
initPull();
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
int moveY = (int) event.getY();
mDistanceY = (moveY - mStartY) / V_REFRESH;
if (!mIsFirstMove && mDistanceY != 0 && mDistanceY < mTouchSlop) {
mCanDownPull = mDistanceY > 0;
mCanUpPull = !mCanDownPull;
mIsFirstMove = true;
}
if (mCanDownPull && mCallBack.canDownPull()) {
upDataForDownPull();//下拉刷新
mChildView.setEnabled(false);
mCanIntercept = true;
}
if (mCanUpPull && mCallBack.canUpPull()) {
upDataForUpPull();//上拉加載
mChildView.setEnabled(false);
mCanIntercept = true;
}
mStartY = moveY;
}
break;
case MotionEvent.ACTION_UP:
mIsRefreshDuring = true;
mIsFirstMove = false;
if (mHeaderParams.height >= mHeaderMaxHeight)
/**可以下拉刷新**/
{
((HeadAndFootCallBack) mHeaderView).startPull();
mOnRefresh.onDownPullRefresh();
} else if (mFootParams.height >= mFootMaxHeight)
/**可以上拉刷新**/
{
((HeadAndFootCallBack) mFootView).startPull();
mOnRefresh.onUpPullRefresh();
} else if (mHeaderParams.height > 0 && mHeaderParams.height < mHeaderMaxHeight)
/**不能進(jìn)行下拉刷新,收回**/
{
releaseForDownFinished();
} else if (mFootParams.height > 0 && mFootParams.height < mFootMaxHeight)
/**不能進(jìn)行下拉刷新,收回**/
{
releaseForUpFinished();
} else {
mIsRefreshDuring = false;
mCanIntercept = false;
}
break;
}
super.dispatchTouchEvent(event);
return true;
}
/**
* 每次進(jìn)行觸摸都需要進(jìn)行初始化
*/
private void initPull() {
mCanDownPull = false;
mCanUpPull = false;
}
/**
* 不需要進(jìn)行上拉刷新
*/
private void releaseForUpFinished() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
}
/**
* 不需要進(jìn)行下拉刷新
*/
private void releaseForDownFinished() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
}
/**
* 上拉時(shí)處理手勢(shì)
*/
private void upDataForUpPull() {
if (mDistanceY != 0) {
mFootParams.height -= mDistanceY;
if (mFootParams.height <= 0) {
mFootParams.height = 0;
}
if (mFootParams.height >= mFootMaxHeight) {
mFootParams.height = mFootMaxHeight;
}
mChildView.setTranslationY(-mFootParams.height);
mFootView.requestLayout();
}
}
/**
* 下拉時(shí)處理手勢(shì)
*/
private void upDataForDownPull() {
if (mDistanceY != 0) {
mHeaderParams.height += mDistanceY;
if (mHeaderParams.height >= mHeaderMaxHeight) { //最大
mHeaderParams.height = mHeaderMaxHeight;
}
if (mHeaderParams.height <= 0) { //最小
mHeaderParams.height = 0;
}
mChildView.setTranslationY(mHeaderParams.height);
mHeaderView.requestLayout();
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//加載頭
mHeaderView = getChildAt(0);
if (!(mHeaderView instanceof HeadAndFootCallBack)) {
new IllegalStateException("HeaderView必須實(shí)現(xiàn)HeadAndFootCallBack接口");
}
((HeadAndFootCallBack) mHeaderView).setAttribute();
mHeaderParams = (LayoutParams) mHeaderView.getLayoutParams();
mHeaderParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//加載尾
mFootView = getChildAt(2);
if (!(mFootView instanceof HeadAndFootCallBack)) {
new IllegalStateException("FootView必須實(shí)現(xiàn)HeadAndFootCallBack接口");
}
((HeadAndFootCallBack) mFootView).setAttribute();
mFootParams = (LayoutParams) mFootView.getLayoutParams();
mFootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mChildView = getChildAt(1);
if (!(mChildView instanceof HeadAndFootCallBack)) {
new IllegalStateException("ChildView必須實(shí)現(xiàn)PullCallBack接口");
}
mCallBack = (PullCallBack) getChildAt(1);
//設(shè)置動(dòng)畫
mAnimator = ObjectAnimator.ofFloat(mChildView, "translationY", 0);
mAnimator.setInterpolator(new DecelerateInterpolator());
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int translationY = (int) mChildView.getTranslationY();
if (mCanUpPull) { //從移動(dòng)到的位置往下滑
mFootParams.height = Math.abs(translationY);
mFootView.requestLayout();
} else if (mCanDownPull) {
mHeaderParams.height = Math.abs(translationY);
mHeaderView.requestLayout();
}
Log.e("shen", "translationY=" + translationY);
Log.e("shen", "mHeaderParams.height=" + mHeaderParams.height);
if (translationY == 0) {
mChildView.setEnabled(true);
mDistanceY = 0; //重置
mIsRefreshDuring = false; //重置
mCanIntercept = false;
} else {
mIsRefreshDuring = true;
}
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mDistance = mTouchSlop * 5;
//設(shè)置下拉頭初始屬性
mHeaderMaxHeight = mHeaderParams.height;
mHeaderParams.height = 0;
mHeaderView.requestLayout();
//設(shè)置上拉尾初始屬性
mFootMaxHeight = mFootParams.height;
mFootParams.height = 0;
mFootView.requestLayout();
}
/**
* 下拉/上拉事件監(jiān)聽
*/
public interface OnRefresh {
/**
* 下拉刷新
*/
void onDownPullRefresh();
/**
* 上拉加載
*/
void onUpPullRefresh();
}
public void setOnRefresh(OnRefresh onRefresh) {
mOnRefresh = onRefresh;
}
}
給他添加三個(gè)控件,頭尾就是刷新頭、尾,第二個(gè)就是正常顯示的控件。必須讓頭尾實(shí)現(xiàn)HeadAndFootCallBack接口,來設(shè)置屬性,通知開始刷新、結(jié)束刷新
難點(diǎn): 現(xiàn)在來說下開發(fā)時(shí)遇到的難點(diǎn)
- 由于判斷在dispatchTouchEvent中,導(dǎo)致如果該控件以及子控件都不消費(fèi)該事件的話,就會(huì)造成事件不會(huì)發(fā)送到它,因?yàn)槿绻幌M(fèi)DOWN事件的話,之后所有的事件都不會(huì)在進(jìn)行接收。解決方式,讓該控件onTouchEvent方法消返回true,當(dāng)子控件不進(jìn)行事件消費(fèi)的話,就會(huì)返回由該控件消費(fèi),不會(huì)造成因DOWN事件不消費(fèi)而無法接收到事件,導(dǎo)致dispatchTouchEvent也不消費(fèi)事件
- 動(dòng)畫,動(dòng)畫就是我的傷痛,最近在學(xué)習(xí)估值器
這個(gè)控件自認(rèn)為寫的不錯(cuò),通過他可以幫我們學(xué)習(xí)事件分發(fā)、動(dòng)畫、接口回調(diào),也是有一定的學(xué)習(xí)意義
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android集成GreenDao數(shù)據(jù)庫的操作步驟
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫,使用數(shù)據(jù)庫存儲(chǔ)時(shí)候,一般都會(huì)使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫的方法,需要的朋友可以參考下2022-10-10
MacBook M1 Flutter環(huán)境搭建的實(shí)現(xiàn)步驟
本文主要介紹了MacBook M1 Flutter環(huán)境搭建,F(xiàn)lutter官方和各項(xiàng)配套的軟件環(huán)境也還沒有成熟,導(dǎo)致搭建環(huán)境時(shí)碰到了不少坑,本文就詳細(xì)的介紹一下2021-08-08
Android實(shí)現(xiàn)跑馬燈效果的代碼詳解
Android中實(shí)現(xiàn)跑馬燈效果有多種方式,本文給大家介紹了Android實(shí)現(xiàn)跑馬燈效果的簡(jiǎn)單示例,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的朋友可以參考下2018-05-05
RecyclerView實(shí)現(xiàn)側(cè)滑拖拽功能
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)側(cè)滑拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround效果,較為詳細(xì)的分析了執(zhí)行l(wèi)oadUrl時(shí)隱藏鍵盤的workround具體步驟與兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android自定義view實(shí)現(xiàn)拖動(dòng)小球移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)拖動(dòng)小球移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

