您的位置:首页 > 移动开发

WebView 下拉刷新

2016-08-24 09:32 183 查看
下拉刷新已经是非常常见的效果了,那么也有很多优秀的控件。

这里分享的是用refreshlayout 思想开发自定义控件。

优点代码更加简洁,修改起来更加方便。

源码:

public class WebViewRefreshLayout extends LinearLayout implements
OnTouchListener {

RelativeLayout header;
TextView description;
boolean loadOnce;
int hideHeaderHeight;
MarginLayoutParams headerLayoutParams;
WebView webView;
Context context;
boolean ableToPull;
float yDown;
int touchSlop;
int PULL =0;
int RELEASA=1;
int REFRESHING=2;
int END=3;
int currentStatus = END;
int lastStatus = currentStatus;
Runnable mListener;
int pullTime = 200;

public WebViewRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);

if (changed && !loadOnce) {
loadOnce = true;
header = (RelativeLayout) getChildAt(0);
description = (TextView)header.getChildAt(0);
webView = (WebView) getChildAt(1);
webView.setOnTouchListener(this);
hideHeaderHeight = -header.getHeight();
headerLayoutParams = (MarginLayoutParams) header.getLayoutParams();
headerLayoutParams.topMargin = hideHeaderHeight;
header.setLayoutParams(headerLayoutParams);
}
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
setIsAbleToPull(arg1);
if (ableToPull) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
yDown = arg1.getRawY();
break;

case MotionEvent.ACTION_MOVE:
int distance=(int)(arg1.getRawY()-yDown);
if (distance<=0 && headerLayoutParams.topMargin <= hideHeaderHeight) {
return false;
}
if (distance<touchSlop) {
return false;
}
if (currentStatus!=REFRESHING) {
if (headerLayoutParams.topMargin>0) {
currentStatus = RELEASA;
}else {
currentStatus = PULL;
}
headerLayoutParams.topMargin = (distance/2)+hideHeaderHeight;
header.setLayoutParams(headerLayoutParams);
}
break;

case MotionEvent.ACTION_UP:
default:
if (currentStatus==RELEASA) {
RefreshingTask();
}
if (currentStatus==PULL) {
HideHeaderTask();
}
break;

}

if (currentStatus==RELEASA|| currentStatus==PULL) {
updateHeaderView();
webView.setPressed(false);
webView.setFocusable(false);
webView.setFocusableInTouchMode(true);
lastStatus=currentStatus;
return true;
}
}
return false;
}

private void HideHeaderTask() {
// TODO Auto-generated method stub
ValueAnimator valueAnimator=ValueAnimator.ofInt(headerLayoutParams.topMargin, hideHeaderHeight);
valueAnimator.setDuration(pullTime);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator arg0) {
// TODO Auto-generated method stub
int integer=((Integer)arg0.getAnimatedValue()).intValue();
headerLayoutParams.topMargin = integer;
header.setLayoutParams(headerLayoutParams);
if (integer==hideHeaderHeight) {
currentStatus = END;
}
updateHeaderView();
}
});
valueAnimator.start();
}

private void RefreshingTask() {
ValueAnimator valueAnimator = ValueAnimator.ofInt(headerLayoutParams.topMargin, 0);
valueAnimator.setDuration(pullTime);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator arg0) {
int integer=((Integer)arg0.getAnimatedValue()).intValue();
headerLayoutParams.topMargin = integer;
header.setLayoutParams(headerLayoutParams);
if (integer == 0) {
currentStatus = REFRESHING;
if (mListener!=null) {
mListener.run();
}
}
updateHeaderView();
}
});
valueAnimator.start();

}

public void setOnRefreshListener(Runnable pullToRefreshListener){
mListener = pullToRefreshListener;
}

private void updateHeaderView() {
if (lastStatus!=currentStatus) {
if (currentStatus==RELEASA) {
description.setText("放手回到上面");
}else if (currentStatus==PULL) {
description.setText("下拉回到上面");
}else if (currentStatus==REFRESHING) {
description.setText("正在加载");
}
}
}

private void setIsAbleToPull(MotionEvent arg1) {
if (webView.getScrollY() <= 0) {
if (!ableToPull) {
yDown = arg1.getRawY();
}
ableToPull = true;
} else {
if (headerLayoutParams.topMargin != hideHeaderHeight) {
headerLayoutParams.topMargin = hideHeaderHeight;
header.setLayoutParams(headerLayoutParams);
}
ableToPull = false;
}
}

public void finishRefreshing(){
currentStatus = END;
HideHeaderTask();
}

}
xml布局:

<com.example.mydome.WebViewRefreshLayout
android:id="@+id/webview_shop_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible" >

<!-- 下拉刷新头部开始 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="17dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="下拉回到上面" />
</RelativeLayout>
<!-- 下拉刷新头部结束 -->

<WebView
android:id="@+id/shop_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.mydome.WebViewRefreshLayout>
使用代码:

webViewRefreshLayout.setOnRefreshListener(new Runnable() {
@Override
public void run() {
//PrevPage();
//刷新执行代码
handler.postDelayed(new Runnable() {

@Override
public void run() {
// 数据加载完毕后执行的回收头部操作
webViewRefreshLayout.finishRefreshing();
}
}, 3000);

}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: