白衣染霜花
2018-10-31 20:58:55浏览 1717
package com.example.customclass;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.Scroller;
import com.example.test.R;
/**
* Created by LI on 2016/1/5.
*/
public class RefershLinearLayout extends LinearLayout {
ImageView direction;
ProgressBar bar;
Scroller scroller;
View header;
Context mContext;
int headerHeightPix;
int lastY;
FreshListener freshListener;
enum Status{
refreshing,normal,pulldown
}
public int status;
public RefershLinearLayout(Context context) {
super(context);
}
public RefershLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
scroller = new Scroller(mContext);
init();
}
private void init() {
header = LayoutInflater.from(mContext).inflate(R.layout.header,null);
bar = (ProgressBar) header.findViewById(R.id.bar);
direction = (ImageView) header.findViewById(R.id.direction);
headerHeightPix = (int) (50*mContext.getResources().getDisplayMetrics().density);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,headerHeightPix);
params.topMargin = (0-headerHeightPix);
addView(header, params);
status = 0;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int y= (int) ev.getRawY();
Log.i("LIJILONG", "onInterceptTouchEvent()");
switch(ev.getAction()){
case MotionEvent.ACTION_DOWN:
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int m = y - lastY;
lastY = y;
if(m>=2.0 &&canScroll()) {
Log.i("LIJILONG" ,"canScroll() true");
return true;
}
break;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(status == 2){
Log.i("lijilong", "onTouchEvent status = "+status);
return false;
}
int y = (int) event.getRawY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
int m = y - lastY;
lastY = y;
moveLayout(m);
break;
case MotionEvent.ACTION_UP:
flying();
break;
}
return true;
}
private void flying() {
LayoutParams params = (LayoutParams) header.getLayoutParams();
int marginTop = params.topMargin;
if(marginTop >= 0){
//refresh();
bar.setVisibility(View.VISIBLE);
direction.setVisibility(View.GONE);
scroller.startScroll(0, marginTop, 0, 0 - marginTop);
invalidate();
// params.topMargin = 0;
// header.setLayoutParams(params);
// header.invalidate();
// invalidate();
// invalidate();
status = 2;
if(freshListener !=null){
freshListener.onFresh();
}
}else{
//returnInit();
scroller.startScroll(0, marginTop, 0 ,0-(headerHeightPix+marginTop));
invalidate();
status = 0;
}
}
public void finishFresh(){
status = 0;
bar.setVisibility(View.GONE);
scroller.startScroll(0, 0, 0, (0 - headerHeightPix));
// LayoutParams params = (LayoutParams) header.getLayoutParams();
// int marginTop = params.topMargin;
// params.topMargin = (0-headerHeightPix);
// header.setLayoutParams(params);
// header.invalidate();
}
private void moveLayout(int m) {
Log.i("lijilong","moveLayout");
LayoutParams params = (LayoutParams) header.getLayoutParams();
int margintop = params.topMargin;
direction.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
if(margintop >= 0){
direction.setImageResource(R.drawable.refresh_arrow_up);
}else{
direction.setImageResource(R.drawable.refresh_arrow_down);
}
int position_y = (int) (margintop + m*0.3);
params.topMargin = position_y;
header.setLayoutParams(params);
// header.invalidate();
//invalidate();
status = 1;
}
@Override
public void computeScroll() {
super.computeScroll();
if (scroller.computeScrollOffset()) {//scroll 动作还未结束
int i = this.scroller.getCurrY();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.header
.getLayoutParams();
int k = Math.max(i, (0-headerHeightPix));
lp.topMargin = k;
this.header.setLayoutParams(lp);
postInvalidate();
}
}
private boolean canScroll() {
// TODO Auto-generated method stub
View childView;
if (getChildCount() > 1) {
Log.e("lijilong","getChildCount="+getChildCount());
for(int i=0;i<getChildCount();i++){
Log.e("lijilong",getChildAt(i).toString());
}
childView = this.getChildAt(1);
if (childView instanceof ListView) {
int top = ((ListView) childView).getChildAt(0).getTop();
int pad = ((ListView) childView).getListPaddingTop();
if ((Math.abs(top - pad)) < 3
&& ((ListView) childView).getFirstVisiblePosition() == 0) {
return true;
} else {
return false;
}
} else if (childView instanceof ScrollView) {
if (((ScrollView) childView).getScrollY() == 0) {
return true;
} else {
return false;
}
}
}
Log.i("LIJILONG","canScroll false");
return false;
}
public void setListener(FreshListener listener){
freshListener = listener;
}
public interface FreshListener{
public void onFresh();
}
}
原文链接:http://www.apkbus.com/blog-653697-59927.html