android 仿饿了吗下拉刷新

2023-05-05 综合 22阅读
代码部分如下:

package com.jefry;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class NetNewDemo extends Activity {
public static ImageView fresh = null;
View mRefreshView;
int top = 0;
int first = 0;
TextView mRefreshViewText;
ImageView mRefreshViewImage;
ImageView mImageView;
ProgressBar mRefreshViewProgress;
TextView mRefreshViewLastUpdated;

Handler hand = new Handler(){
public void handleMessage(android.os.Message msg) {
mRefreshViewImage.setVisibility(View.GONE);
mImageView.setVisibility(View.GONE);
mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRefreshView = (View)findViewById(R.id.layout);
mRefreshViewImage = (ImageView) findViewById(R.id.pull_to_refresh_image);
mImageView = (ImageView)findViewById(R.id.going);
ScrollView sc = (ScrollView)findViewById(R.id.scroll);
sc.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent event) {
Log.i("TAG", "=============onTouchEvent================="帆神历);
//当前手瞎李指的态搜Y值
final int y = (int) event.getY();
// mRefreshViewImage.setImageResource(R.drawable.fresh_going);
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
try{
first = 0;
mRefreshViewImage.setImageResource(R.drawable.fresh_going);
mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
hand.sendEmptyMessage(1);
}
}).start();
}catch (Exception e) {}
break;
case MotionEvent.ACTION_DOWN:
first = y;
break;

case MotionEvent.ACTION_MOVE:
mImageView.setVisibility(View.GONE);
int instance = y-first;
if(instance>0 && instance < 30) {
mRefreshViewImage.setVisibility(View.VISIBLE);
mRefreshViewImage.setImageResource(R.drawable.down);
} else if(instance >=30){
mRefreshViewImage.setVisibility(View.VISIBLE);
mRefreshViewImage.setImageResource(R.drawable.fresh_before);
}

mRefreshView.setPadding(mRefreshView.getPaddingLeft(),instance,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
//mRefreshViewImage.setImageResource(R.drawable.fresh_going);
break;
}
return true;
}
});

}

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("TAG", "=============onTouchEvent=================");
//当前手指的Y值
final int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
first = 0;
prepareForRefresh(); //准备刷新
break;
case MotionEvent.ACTION_DOWN:
first = y;
break;

case MotionEvent.ACTION_MOVE:
//更行头视图的toppadding 属性
Log.i("TAG", "=============ACTION_MOVE============" + (y - first));
mRefreshView.setPadding(mRefreshView.getPaddingLeft(),(y - first),mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
//applyHeaderPadding(event);
break;
}
return super.onTouchEvent(event);
}

public void prepareForRefresh() {
resetHeaderPadding(); //初始化,头部文件

mRefreshViewImage.setVisibility(View.GONE);
mRefreshViewImage.setImageDrawable(null);
mRefreshViewProgress.setVisibility(View.VISIBLE);

mRefreshViewText.setText("Jefry ");

}

private void resetHeaderPadding() {
mRefreshView.setPadding(
mRefreshView.getPaddingLeft(),
0,
mRefreshView.getPaddingRight(),
mRefreshView.getPaddingBottom());
}

}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com