当前位置: 编程技术>移动开发
本页文章导读:
▪任务的提交者跟执行者 任务的提交者和执行者
任务的提交者和执行者为了方便并发执行任务,出现了一种专门用来执行任务的实现,也就是Executor。由此,任务提交者不需要再创建管理线程,使用更方便,也减.........
▪ 兑现一个简单的阻塞队列(2) 实现一个简单的阻塞队列(2)
classBlockingQ{privateObject notEmpty= newObject();privateObject notFull= newObject();privateQueue<Object> linkedList= newLinkedList<Object>();privateintmaxLength= 10;publicObject take() throwsInter.........
▪ 高仿新浪微博上拉刷新 高仿新浪微博下拉刷新
自己写了个模仿新浪下拉刷新的效果。通过菜单选择 自动加载更多 或 手动加载更多。绝非修改国外那哥们写的。改进版 下拉刷新 代码:效果请访问 www.lianluoquan.co.........
[1]任务的提交者跟执行者
来源: 互联网 发布时间: 2014-02-18
任务的提交者和执行者
任务的提交者和执行者
为了方便并发执行任务,出现了一种专门用来执行任务的实现,也就是Executor。
由此,任务提交者不需要再创建管理线程,使用更方便,也减少了开销。
java.util.concurrent.Executors是Executor的工厂类,通过Executors可以创建你所需要的Executor。
任务的提交者和执行者之间的通讯手段
ExecutorServiceexecutor = Executors.newSingleThreadExecutor();
Callable<Object> task = newCallable<Object>() {
publicObject call() throwsException {
Object result = "...";
returnresult;
}
};
Future<Object> future = executor.submit(task);
future.get();
有两种任务:
Runnable
Callable
Callable是需要返回值的任务
Task Submitter
Future<Object> future = executor.submit(task);
// 等待到任务被执行完毕返回结果
// 如果任务执行出错,这里会抛ExecutionException
future.get();
//等待3秒,超时后会抛TimeoutException
future.get(3, TimeUnit.SECONDS);
Task Executor
Callable<Object> task = newCallable<Object>() {publicObject call() throwsException {Object result = …;returnresult;}};
Task Submitter把任务提交给Executor执行,他们之间需要一种通讯手段,这种手段的具体实现,通常叫做Future。Future通常包括get(阻塞至任务完成),cancel,get(timeout)(等待一段时间)等等。Future也用于异步变同步的场景。
任务的提交者和执行者
为了方便并发执行任务,出现了一种专门用来执行任务的实现,也就是Executor。
由此,任务提交者不需要再创建管理线程,使用更方便,也减少了开销。
java.util.concurrent.Executors是Executor的工厂类,通过Executors可以创建你所需要的Executor。
任务的提交者和执行者之间的通讯手段
ExecutorServiceexecutor = Executors.newSingleThreadExecutor();
Callable<Object> task = newCallable<Object>() {
publicObject call() throwsException {
Object result = "...";
returnresult;
}
};
Future<Object> future = executor.submit(task);
future.get();
有两种任务:
Runnable
Callable
Callable是需要返回值的任务
Task Submitter
Future<Object> future = executor.submit(task);
// 等待到任务被执行完毕返回结果
// 如果任务执行出错,这里会抛ExecutionException
future.get();
//等待3秒,超时后会抛TimeoutException
future.get(3, TimeUnit.SECONDS);
Task Executor
Callable<Object> task = newCallable<Object>() {publicObject call() throwsException {Object result = …;returnresult;}};
Task Submitter把任务提交给Executor执行,他们之间需要一种通讯手段,这种手段的具体实现,通常叫做Future。Future通常包括get(阻塞至任务完成),cancel,get(timeout)(等待一段时间)等等。Future也用于异步变同步的场景。
[2] 兑现一个简单的阻塞队列(2)
来源: 互联网 发布时间: 2014-02-18
实现一个简单的阻塞队列(2)
classBlockingQ{
privateObject notEmpty= newObject();
privateObject notFull= newObject();
privateQueue<Object> linkedList= newLinkedList<Object>();
privateintmaxLength= 10;
publicObject take() throwsInterruptedException{
synchronized(notEmpty) {
if(linkedList.size() == 0) {
notEmpty.wait();
}
synchronized(notFull) {分别需要对notEmpty和notFull加锁
if(linkedList.size() == maxLength) {
notFull.notifyAll();
}
returnlinkedList.poll();
}
}
}
publicvoidoffer(Object object) throwsInterruptedException{
synchronized(notEmpty) {分别需要对notEmpty和notFull加锁
if(linkedList.size() == 0) {
notEmpty.notifyAll();
}
synchronized(notFull) {
if(linkedList.size() == maxLength) {
notFull.wait();
}
linkedList.add(object);
}
}
}
}
通过实现简单的阻塞队列来学习并发知识
classBlockingQ{
privateObject notEmpty= newObject();
privateObject notFull= newObject();
privateQueue<Object> linkedList= newLinkedList<Object>();
privateintmaxLength= 10;
publicObject take() throwsInterruptedException{
synchronized(notEmpty) {
if(linkedList.size() == 0) {
notEmpty.wait();
}
synchronized(notFull) {分别需要对notEmpty和notFull加锁
if(linkedList.size() == maxLength) {
notFull.notifyAll();
}
returnlinkedList.poll();
}
}
}
publicvoidoffer(Object object) throwsInterruptedException{
synchronized(notEmpty) {分别需要对notEmpty和notFull加锁
if(linkedList.size() == 0) {
notEmpty.notifyAll();
}
synchronized(notFull) {
if(linkedList.size() == maxLength) {
notFull.wait();
}
linkedList.add(object);
}
}
}
}
通过实现简单的阻塞队列来学习并发知识
[3] 高仿新浪微博上拉刷新
来源: 互联网 发布时间: 2014-02-18
高仿新浪微博下拉刷新
自己写了个模仿新浪下拉刷新的效果。通过菜单选择 自动加载更多 或 手动加载更多。绝非修改国外那哥们写的。
改进版 下拉刷新 代码:
效果请访问 www.lianluoquan.com
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.Toast;
public class SimplePullDownView extends FrameLayout implements
GestureDetector.OnGestureListener, Animation.AnimationListener {
String tag = "SimplePullDownView";
private class CheckForLongPress implements Runnable {
public void run() {
// Log.e(tag, "长按检查方法执行");
if(mListView.getOnItemLongClickListener() == null){
/*MotionEvent e = MotionEvent.obtain(downEvent.getDownTime(), downEvent.getEventTime() + ViewConfiguration.getLongPressTimeout() + 100, MotionEvent.ACTION_CANCEL, downEvent.getX(), downEvent.getY(), downEvent.getMetaState());
SimplePullDownView.super.dispatchTouchEvent(e);*/
}
else {
postDelayed(mPendingCheckForLongPress2, 100);
}
}
}
private class CheckForLongPress2 implements Runnable {
public void run() {
// Log.e(tag, "长按检查方法执行2 ----> 延后 100 " );
mLongPressing = true;
MotionEvent e = MotionEvent.obtain(downEvent.getDownTime(), downEvent.getEventTime() + ViewConfiguration.getLongPressTimeout(), MotionEvent.ACTION_CANCEL, downEvent.getX(), downEvent.getY(), downEvent.getMetaState());
SimplePullDownView.super.dispatchTouchEvent(e);
}
}
class FlingRunnable implements Runnable {
private void startCommon() {
removeCallbacks(this);
}
public void run() {
boolean noFinish = mScroller.computeScrollOffset();
int curY = mScroller.getCurrY();
int deltaY = curY - mLastFlingY;
// Log.e("SimplePullDownView", "deltaY = " + deltaY + " , noFinish = " + noFinish);
// updateView();
if (noFinish/* && deltaY!=0*/) {
move(deltaY, true);
mLastFlingY = curY;
post(this);
} else {
removeCallbacks(this);
if(mState == SCROLL_TO_CLOSE){
mState = -1;
}
}
}
public void startUsingDistance(int distance, int duration) {
if (distance == 0)
distance--;
startCommon();
mLastFlingY = 0;
mScroller.startScroll(0, 0, 0, distance, duration);
post(this);
}
private int mLastFlingY;
private Scroller mScroller;
public FlingRunnable() {
mScroller = new Scroller(getContext());
}
}
public interface OnRefreshListioner {
public abstract void onRefresh();
public abstract void onLoadMore();
}
public SimplePullDownView(Context context) {
super(context);
mDetector = new GestureDetector(context, this);
mFlinger = new FlingRunnable();
init();
addRefreshBar();
}
public SimplePullDownView(Context context, AttributeSet att) {
super(context, att);
useempty = att.getAttributeBooleanValue(null, "useempty", true);
//Log.e(tag, "useempty = " + useempty);
mDetector = new GestureDetector(this);
mFlinger = new FlingRunnable();
init();
addRefreshBar();
}
private void addRefreshBar() {
mAnimationUp = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_up);
mAnimationUp.setAnimationListener(this);
mAnimationDown = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_down);
mAnimationDown.setAnimationListener(this);
View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_bar, null);
// view.setVisibility(View.INVISIBLE);
view.setBackgroundColor(0xfff4f4f4);
addView(view);
mFirstChild = (LinearLayout) view;
mUpdateContent = (FrameLayout) getChildAt(0).findViewById(R.id.iv_content);
mArrow = new ImageView(getContext());
mArrow.setBackgroundColor(0xfff4f4f4);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mArrow.setScaleType(ImageView.ScaleType.FIT_CENTER);
mArrow.setLayoutParams(layoutparams);
mArrow.setImageResource( R.drawable.arrow_down);
mUpdateContent.addView(mArrow);
FrameLayout.LayoutParams layoutparams1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutparams1.gravity = Gravity.CENTER;
// int defstyle = android.R.style.Widget_ProgressBar_Small_Inverse;
mProgressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleSmallInverse);
mProgressBar.setIndeterminate(false);
mProgressBar.setBackgroundColor(0xfff4f4f4);
// mProgressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_small));
int i = getResources().getDimensionPixelSize(R.dimen.updatebar_padding);
mProgressBar.setPadding(i, i, i, i);
mProgressBar.setLayoutParams(layoutparams1);
mUpdateContent.addView(mProgressBar);
mTitle = (TextView) findViewById(R.id.ref_title);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mListView = (ListView) getChildAt(1);
footer_layout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.empty_main, null);
foot = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.ref2, null);
// footer_layout.addView(foot);
more = (TextView) foot.findViewById(R.id.ref);
mProgressBar2 = (ProgressBar) foot.findViewById(R.id.refbar);
// mProgressBar2.setVisibility(View.INVISIBLE);
if(useempty){
header = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.empty_main, null);
mListView.addHeaderView(header);
}
mListView.addFooterView(footer_layout);
foot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isAutoLoadMore)
onLoadMore();
}
});
mListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Log.i("SimplePullDownView", "scrollState = " + scrollState);
if(isEnd && scrollState == SCROLL_STATE_IDLE && hasMore && isAutoLoadMore){
onLoadMore();
}
}
@Override
public void onScroll(AbsListView view, int f, int v, int t) {
// Log.i("SimplePullDownView", "first = " + f + " , visable = " + v + " , total = " + t + " , hasMore = " + hasMore);
if(isAutoLoadMore){
if(f + v >= t - 1)
isEnd = true;
else
isEnd = false;
}
}
});
}
/**
* 设置没有数据时默认图片
* @param empty
*/
public void setEmptyHeaderView(View empty){
emptyHeaderView = empty;
}
public void addEmptyHeaderView(){
header.removeAllViews();
if(emptyHeaderView != null)
header.addView(emptyHeaderView);
}
public void removeEmptyHeaderView(){
if(emptyHeaderView != null)
header.removeView(emptyHeaderView);
}
private void init() {
MAX_LENGHT = getResources().getDimensionPixelSize(R.dimen.updatebar_height );//62.0dip
setDrawingCacheEnabled(false);
setBackgroundDrawable(null);
setClipChildren(false);
mDetector.setIsLongpressEnabled(false);
mPading = -MAX_LENGHT;
// mLastPading = -MAX_LENGHT;
mLastTop = -MAX_LENGHT;
pulldowntorefresh = getContext().getText(R.string.drop_dowm).toString();
releasetorefresh = getContext().getText(R.string.release_update).toString();
loading = getContext().getText(R.string.doing_update).toString();
update_time = getContext().getText(R.string.update_time).toString();
}
/** deltaY > 0 向上 */
private boolean move(float deltaY, boolean auto) {
// //Log.e("SimplePullDownView","move 方法执行 " );
if(deltaY > 0 && mFirstChild.getTop() == -MAX_LENGHT){
mPading = -MAX_LENGHT;
return false;
}
if(auto){
// Log.i("SimplePullDownView","move 方法执行 mPading = " + mPading);
if(mFirstChild.getTop()-deltaY < mDestPading){
deltaY = mFirstChild.getTop() - mDestPading;
}
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
if(mDestPading == 0 && mFirstChild.getTop() == 0 && mState ==SCROLL_TO_REFRESH ){
//Log.e("SimplePullDownView","onRefresh 刷新方法执行 --->");
onRefresh();
} else if(mDestPading == -MAX_LENGHT){
}
invalidate();
updateView();
return true;
} else {
if(mState != STATE_REFRESH || (mState == STATE_REFRESH && deltaY>0)){
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
} else if(mState == STATE_REFRESH && deltaY<0 && mFirstChild.getTop()<=0){
if(mFirstChild.getTop() - deltaY > 0){
deltaY = mFirstChild.getTop();
}
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
}
}
if(deltaY > 0 && mFirstChild.getTop() <= -MAX_LENGHT){
mPading = -MAX_LENGHT;
deltaY = -MAX_LENGHT - mFirstChild.getTop();
mFirstChild.offsetTopAndBottom((int) deltaY);
mListView.offsetTopAndBottom((int) deltaY);
mPading = mFirstChild.getTop();
updateView();
invalidate();
return false;
}
updateView();
invalidate();
return true;
}
private void updateView() {
String s = "";
s = mDate == null? "" : update_time + mDate ;
if(mState != STATE_REFRESH){
if(mFirstChild.getTop() < 0){
mArrow.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
mTitle.setText(pulldowntorefresh + s);
if(mLastTop >= 0 && mState != SCROLL_TO_CLOSE){
mArrow.startAnimation(mAnimationUp);
//Log.i("SimplePullDownView", " mAnimationUp 上动画 " );
}
} else if(mFirstChild.getTop() > 0){
mTitle.setText(releasetorefresh + s);
mProgressBar.setVisibility(View.INVISIBLE);
mArrow.setVisibility(View.VISIBLE);
if(mLastTop <= 0 ){
mArrow.startAnimation(mAnimationDown);
//Log.i("SimplePullDownView", " mAnimationDown 下动画 " );
}
}
}
mLastTop = mFirstChild.getTop();
}
private boolean release()
{
//Log.i("SimplePullDownView", "release 方法执行 = " + " , mFirstChild.getTop() = " + mFirstChild.getTop());
if(listviewDoScroll){
listviewDoScroll = false;
return true;
}
if(mFirstChild.getTop()>0){
scrollToUpdate();
} else {
scrollToClose();
// mDestPading = -MAX_LENGHT;
}
invalidate();
return false;
}
private void scrollToClose() {
mDestPading = -MAX_LENGHT;
mFlinger.startUsingDistance(MAX_LENGHT, CLOSEDELAY);
}
private void scrollToUpdate() {
mState = SCROLL_TO_REFRESH;
//Log.e("SimplePullDownView", "scrollToUpdate 方法执行");
mDestPading = 0;
mFlinger.startUsingDistance(mFirstChild.getTop(), REFRESHDELAY);
}
private void onRefresh(){
mState = STATE_REFRESH;
String s = "";
if(mDate == null)
s = "";
else
s = update_time + mDate;
mTitle.setText(loading + s);
mProgressBar.setVisibility(View.VISIBLE);
mArrow.setVisibility(View.INVISIBLE);
if(mRefreshListioner != null){
mRefreshListioner.onRefresh();
}
}
public void onRefreshComplete(){
onRefreshComplete(null);
}
public void onRefreshComplete(String date){
//Log.e("SimplePullDownView", "onRefreshComplete 方法执行 ");
mDate = date;
mState = SCROLL_TO_CLOSE;
/* mPading = -MAX_LENGHT;
mLastPading = -MAX_LENGHT;*/
mArrow.setImageResource(R.drawable.arrow_down);
mProgressBar2.setVisibility(View.INVISIBLE);
updateCommon();
scrollToClose();
}
private void updateCommon(){
if(mListView.getCount() == (mListView.getHeaderViewsCount() + mListView.getFooterViewsCount())){
Log.e("out", "数据为空");
if(useempty)
addEmptyHeaderView();
footer_layout.removeAllViews();
mListView.setFooterDividersEnabled(false);
} else {
removeEmptyHeaderView();
if(hasMore){
mListView.setFooterDividersEnabled(true);
footer_layout.removeAllViews();
footer_layout.addView(foot);
}else {
mListView.setFooterDividersEnabled(false);
footer_layout.removeAllViews();
}
}
}
public void onFirstLoad(){
if(footer_layout.getChildCount() == 0){
footer_layout.addView(foot);
}
isFirstLoading = true;
foot.setEnabled(false);
//Log.e("SimplePullDownView", "onFirstLoad 方法执行 ");
mState = STATE_REFRESH;
mProgressBar2.setVisibility(View.VISIBLE);
more.setText(R.string.loading2);
}
public void onLoadMore(){
//Log.e("SimplePullDownView", "onLoadMore 方法执行 ");
foot.setEnabled(false);
mState = STATE_REFRESH;
mProgressBar2.setVisibility(View.VISIBLE);
more.setText(R.string.loading2);
if(mRefreshListioner != null){
mRefreshListioner.onLoadMore();
}
}
public void onLoadMoreComplete(String date){
//Log.e("SimplePullDownView", "onLoadMoreComplete 方法执行 ");
mDate = date;
mState = -1;
mProgressBar2.setVisibility(View.INVISIBLE);
more.setText(R.string.more);
updateCommon();
if(isFirstLoading)
isFirstLoading = false;
foot.setEnabled(true);
}
public void onLoadMoreComplete( ){
onLoadMoreComplete(null);
}
public boolean dispatchTouchEvent(MotionEvent e){
//Log.e("SimplePullDownView", "dispatchTouchEvent ----------> ");
if(isFirstLoading){
return false;
}
int action;
float y = e.getY();
action = e.getAction();
if(mLongPressing && action != MotionEvent.ACTION_DOWN){
return false;
}
if(e.getAction() == MotionEvent.ACTION_DOWN){
mLongPressing = true;
}
boolean handled = true;
handled = mDetector.onTouchEvent(e);
// if(!handled)
// Log.i("SimplePullDownView", " mDetector.onTouchEvent handled = " + handled + " , mPading = " + mPading);
switch (action) {
case MotionEvent.ACTION_UP:
/*mListView.getHitRect(r);
boolean f = r.contains((int)e.getX(), (int)e.getY());*/
boolean f1 = mListView.getTop() <= e.getY() && e.getY() <= mListView.getBottom();
// Log.e("SimplePullDownView", " ACTION_UP -------> handled = " + handled + " , f1 = " + f1 + " , top == -MAX = " +(mFirstChild.getTop() == -MAX_LENGHT) );
if(!handled && mFirstChild.getTop() == -MAX_LENGHT && f1 || mState == STATE_REFRESH){
super.dispatchTouchEvent(e);
}
else{
// Log.e("SimplePullDownView", "执行释放方法 listviewDoScroll = " + listviewDoScroll);
handled = release();
}
break;
case MotionEvent.ACTION_CANCEL:
// Log.e(tag, " event: ACTION_CANCEL" );
handled = release();
super.dispatchTouchEvent(e);
break;
case MotionEvent.ACTION_DOWN:
downEvent = e;
mLongPressing = false;
// Log.e(tag, " 长按的时间间隔:" + ViewConfiguration.getLongPressTimeout());
postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout()+ 100);
mPendingRemoved = false;
downPostion = mListView.pointToPosition((int)e.getX(), (int)e.getY()) - mListView.getHeaderViewsCount();
//Log.e("SimplePullDownView", "downPostion = " + downPostion);
super.dispatchTouchEvent(e);
break;
case MotionEvent.ACTION_MOVE:
float deltaY = lastY - y;
lastY = y;
if(!mPendingRemoved){
removeCallbacks(mPendingCheckForLongPress);
mPendingRemoved = true;
}
//Log.e("SimplePullDownView","handled = " + handled + " , top = " + mFirstChild.getTop());
if(!handled && mFirstChild.getTop() == -MAX_LENGHT){
try{
return super.dispatchTouchEvent(e);
} catch (Exception e2) {
e2.printStackTrace();
return true;
}
}
else if(handled && mListView.getTop() > 0 && deltaY < 0){//deltaY小于0,向下
e.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(e);
}
break;
default:
break;
}
return true;
}
public void onAnimationEnd(Animation animation) {
int top = mFirstChild.getTop();
if (top < 0)
mArrow.setImageResource(R.drawable.arrow_down);
else if (top > 0)
mArrow.setImageResource(R.drawable.arrow_up);
else {
if(top < mLastTop){
mArrow.setImageResource(R.drawable.arrow_down);
} else {
mArrow.setImageResource(R.drawable.arrow_up);
}
}
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
public boolean onDown(MotionEvent e) {
return false;
}
public boolean onFling(MotionEvent motionevent, MotionEvent e,
float f, float f1) {
return false;
}
protected void onLayout(boolean flag, int i, int j, int k, int l) {
// Log.i("SimplePullDownView", "onLayout 方法执行 ------> mPading = " + mPading + " , listTop = " + (mPading + MAX_LENGHT));
int top = mPading;
int w = getMeasuredWidth();
mFirstChild.layout(0, top, w, top + MAX_LENGHT);
int h = getMeasuredHeight() + mPading + MAX_LENGHT;
mListView.layout(0, top + MAX_LENGHT , w, h);
}
public void onLongPress(MotionEvent e) {
}
/** deltaY > 0 向上*/
public boolean onScroll(MotionEvent curdown, MotionEvent cur, float deltaX, float deltaY) {
// mListView.getHitRect(r);
// Log.i("SimplePullDownView", "父亲高度 : = " + getHeight() + " , width = " + r.width() + " , height = " + r.height() + " , top = " + r.top + " , bottom = " + r.bottom);
deltaY = (float) ((double) deltaY * SCALE);
boolean handled = false;
boolean flag = false;
if (mListView.getCount() == 0) {
flag = true;
} else {
View c = mListView.getChildAt(0);
if (mListView.getFirstVisiblePosition() == 0 && c!=null && c.getTop() == 0){
flag = true;
}
// Log.i("SimplePullDownView","onScroll 方法执行 mPading = " + mPading + ", mTop = " + adapterview.getTop() + " flag = " + flag + " , deltaY = " + deltaY);
}
if (deltaY < 0F && flag || getChildAt(0).getTop() > -MAX_LENGHT){ //deltaY < 0 向下
handled = move(deltaY, false);
// Log.i("SimplePullDownView", "满足条件 执行 move 方法, handled = " + handled);
}
else
handled = false;
// Log.i("SimplePullDownView", " onScroll handled = " + handled);
return handled;
}
public void onShowPress(MotionEvent motionevent) {
}
public boolean onSingleTapUp(MotionEvent motionevent) {
return false;
}
public void setRefreshListioner(OnRefreshListioner RefreshListioner) {
mRefreshListioner = RefreshListioner;
}
public boolean isAutoLoadMore() {
return isAutoLoadMore;
}
public void setAutoLoadMore(boolean isAutoLoadMore) {
this.isAutoLoadMore = isAutoLoadMore;
if(!isAutoLoadMore){
foot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLoadMore();
}
});
mListView.setOnScrollListener(null);
} else {
mListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Log.i("SimplePullDownView", "scrollState = " + scrollState);
if(isEnd && scrollState == SCROLL_STATE_IDLE && hasMore){
onLoadMore();
}
}
@Override
public void onScroll(AbsListView view, int f, int v, int t) {
// Log.i("SimplePullDownView", "first = " + f + " , visable = " + v + " , total = " + t + " , hasMore = " + hasMore);
if(f + v >= t - 1)
isEnd = true;
else
isEnd = false;
}
});
foot.setOnClickListener(null);
}
}
public void setHasMore(boolean hasMore) {
this.hasMore = hasMore;
}
public void removeFoot(){
footer_layout.removeAllViews();
}
public void addFoot(){
footer_layout.removeAllViews();
footer_layout.addView(foot);
}
public static int MAX_LENGHT = 0;
public static final int STATE_REFRESH = 1;
public static final int SCROLL_TO_CLOSE = 2;
public static final int SCROLL_TO_REFRESH = 3;
public static final double SCALE = 0.9d;
private static final int CLOSEDELAY = 300;
private static final int REFRESHDELAY = 300;
private Animation mAnimationDown;
private Animation mAnimationUp;
private ImageView mArrow;
private String mDate;
private View emptyHeaderView;
private ProgressBar mProgressBar;
private TextView more;
private ProgressBar mProgressBar2;
private int mState;
private TextView mTitle;
ListView mListView;
LinearLayout foot;
LinearLayout footer_layout;
LinearLayout header;
private GestureDetector mDetector;
private FlingRunnable mFlinger;
private int mPading;
private int mDestPading;
// private int mLastPading;
private int mLastTop;
private LinearLayout mFirstChild;
private FrameLayout mUpdateContent;
private OnRefreshListioner mRefreshListioner;
private boolean isAutoLoadMore = false;
private boolean hasMore = true;
private boolean isEnd = true;
private boolean listviewDoScroll = false;
private boolean isFirstLoading = false;
// private boolean mIsAutoScroller;
private boolean mLongPressing;//如果设置为true说明刚好到了执行长按的时间
private boolean mPendingRemoved = false;//
private String pulldowntorefresh;
private String releasetorefresh;
private String loading;
private String update_time;
Rect r = new Rect();
private int downPostion;
private MotionEvent downEvent;
private CheckForLongPress mPendingCheckForLongPress = new CheckForLongPress();
private CheckForLongPress2 mPendingCheckForLongPress2 = new CheckForLongPress2();
private float lastY;
private boolean useempty = true;
}
=-=表示看错了一个东西 不过也希望认识下您
如果方便的话请e_mail联系godcok@163.com,谢谢
如果方便的话请e_mail联系godcok@163.com,谢谢
问题已经解决了,不用联系了谢谢。
这个你可以自己加啊
这个你可以自己加啊
晕。你的代码也写得太高深了。看不太懂。嘿嘿
我贴的源码是最新的,但那个例子里的代码不是最新的,我这没有最新的例子
我贴的源码是最新的,但那个例子里的代码不是最新的,我这没有最新的例子
能不能把你的代码发我一份呢, 谢谢您了的啊。 麻烦了的 79144876@qq.com
Offset this view's vertical location by the specified number of pixels.
自己写了个模仿新浪下拉刷新的效果。通过菜单选择 自动加载更多 或 手动加载更多。绝非修改国外那哥们写的。
改进版 下拉刷新 代码:
效果请访问 www.lianluoquan.com
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.Toast;
public class SimplePullDownView extends FrameLayout implements
GestureDetector.OnGestureListener, Animation.AnimationListener {
String tag = "SimplePullDownView";
private class CheckForLongPress implements Runnable {
public void run() {
// Log.e(tag, "长按检查方法执行");
if(mListView.getOnItemLongClickListener() == null){
/*MotionEvent e = MotionEvent.obtain(downEvent.getDownTime(), downEvent.getEventTime() + ViewConfiguration.getLongPressTimeout() + 100, MotionEvent.ACTION_CANCEL, downEvent.getX(), downEvent.getY(), downEvent.getMetaState());
SimplePullDownView.super.dispatchTouchEvent(e);*/
}
else {
postDelayed(mPendingCheckForLongPress2, 100);
}
}
}
private class CheckForLongPress2 implements Runnable {
public void run() {
// Log.e(tag, "长按检查方法执行2 ----> 延后 100 " );
mLongPressing = true;
MotionEvent e = MotionEvent.obtain(downEvent.getDownTime(), downEvent.getEventTime() + ViewConfiguration.getLongPressTimeout(), MotionEvent.ACTION_CANCEL, downEvent.getX(), downEvent.getY(), downEvent.getMetaState());
SimplePullDownView.super.dispatchTouchEvent(e);
}
}
class FlingRunnable implements Runnable {
private void startCommon() {
removeCallbacks(this);
}
public void run() {
boolean noFinish = mScroller.computeScrollOffset();
int curY = mScroller.getCurrY();
int deltaY = curY - mLastFlingY;
// Log.e("SimplePullDownView", "deltaY = " + deltaY + " , noFinish = " + noFinish);
// updateView();
if (noFinish/* && deltaY!=0*/) {
move(deltaY, true);
mLastFlingY = curY;
post(this);
} else {
removeCallbacks(this);
if(mState == SCROLL_TO_CLOSE){
mState = -1;
}
}
}
public void startUsingDistance(int distance, int duration) {
if (distance == 0)
distance--;
startCommon();
mLastFlingY = 0;
mScroller.startScroll(0, 0, 0, distance, duration);
post(this);
}
private int mLastFlingY;
private Scroller mScroller;
public FlingRunnable() {
mScroller = new Scroller(getContext());
}
}
public interface OnRefreshListioner {
public abstract void onRefresh();
public abstract void onLoadMore();
}
public SimplePullDownView(Context context) {
super(context);
mDetector = new GestureDetector(context, this);
mFlinger = new FlingRunnable();
init();
addRefreshBar();
}
public SimplePullDownView(Context context, AttributeSet att) {
super(context, att);
useempty = att.getAttributeBooleanValue(null, "useempty", true);
//Log.e(tag, "useempty = " + useempty);
mDetector = new GestureDetector(this);
mFlinger = new FlingRunnable();
init();
addRefreshBar();
}
private void addRefreshBar() {
mAnimationUp = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_up);
mAnimationUp.setAnimationListener(this);
mAnimationDown = AnimationUtils.loadAnimation(getContext(),R.anim.rotate_down);
mAnimationDown.setAnimationListener(this);
View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_bar, null);
// view.setVisibility(View.INVISIBLE);
view.setBackgroundColor(0xfff4f4f4);
addView(view);
mFirstChild = (LinearLayout) view;
mUpdateContent = (FrameLayout) getChildAt(0).findViewById(R.id.iv_content);
mArrow = new ImageView(getContext());
mArrow.setBackgroundColor(0xfff4f4f4);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mArrow.setScaleType(ImageView.ScaleType.FIT_CENTER);
mArrow.setLayoutParams(layoutparams);
mArrow.setImageResource( R.drawable.arrow_down);
mUpdateContent.addView(mArrow);
FrameLayout.LayoutParams layoutparams1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutparams1.gravity = Gravity.CENTER;
// int defstyle = android.R.style.Widget_ProgressBar_Small_Inverse;
mProgressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleSmallInverse);
mProgressBar.setIndeterminate(false);
mProgressBar.setBackgroundColor(0xfff4f4f4);
// mProgressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_small));
int i = getResources().getDimensionPixelSize(R.dimen.updatebar_padding);
mProgressBar.setPadding(i, i, i, i);
mProgressBar.setLayoutParams(layoutparams1);
mUpdateContent.addView(mProgressBar);
mTitle = (TextView) findViewById(R.id.ref_title);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mListView = (ListView) getChildAt(1);
footer_layout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.empty_main, null);
foot = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.ref2, null);
// footer_layout.addView(foot);
more = (TextView) foot.findViewById(R.id.ref);
mProgressBar2 = (ProgressBar) foot.findViewById(R.id.refbar);
// mProgressBar2.setVisibility(View.INVISIBLE);
if(useempty){
header = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.empty_main, null);
mListView.addHeaderView(header);
}
mListView.addFooterView(footer_layout);
foot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isAutoLoadMore)
onLoadMore();
}
});
mListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Log.i("SimplePullDownView", "scrollState = " + scrollState);
if(isEnd && scrollState == SCROLL_STATE_IDLE && hasMore && isAutoLoadMore){
onLoadMore();
}
}
@Override
public void onScroll(AbsListView view, int f, int v, int t) {
// Log.i("SimplePullDownView", "first = " + f + " , visable = " + v + " , total = " + t + " , hasMore = " + hasMore);
if(isAutoLoadMore){
if(f + v >= t - 1)
isEnd = true;
else
isEnd = false;
}
}
});
}
/**
* 设置没有数据时默认图片
* @param empty
*/
public void setEmptyHeaderView(View empty){
emptyHeaderView = empty;
}
public void addEmptyHeaderView(){
header.removeAllViews();
if(emptyHeaderView != null)
header.addView(emptyHeaderView);
}
public void removeEmptyHeaderView(){
if(emptyHeaderView != null)
header.removeView(emptyHeaderView);
}
private void init() {
MAX_LENGHT = getResources().getDimensionPixelSize(R.dimen.updatebar_height );//62.0dip
setDrawingCacheEnabled(false);
setBackgroundDrawable(null);
setClipChildren(false);
mDetector.setIsLongpressEnabled(false);
mPading = -MAX_LENGHT;
// mLastPading = -MAX_LENGHT;
mLastTop = -MAX_LENGHT;
pulldowntorefresh = getContext().getText(R.string.drop_dowm).toString();
releasetorefresh = getContext().getText(R.string.release_update).toString();
loading = getContext().getText(R.string.doing_update).toString();
update_time = getContext().getText(R.string.update_time).toString();
}
/** deltaY > 0 向上 */
private boolean move(float deltaY, boolean auto) {
// //Log.e("SimplePullDownView","move 方法执行 " );
if(deltaY > 0 && mFirstChild.getTop() == -MAX_LENGHT){
mPading = -MAX_LENGHT;
return false;
}
if(auto){
// Log.i("SimplePullDownView","move 方法执行 mPading = " + mPading);
if(mFirstChild.getTop()-deltaY < mDestPading){
deltaY = mFirstChild.getTop() - mDestPading;
}
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
if(mDestPading == 0 && mFirstChild.getTop() == 0 && mState ==SCROLL_TO_REFRESH ){
//Log.e("SimplePullDownView","onRefresh 刷新方法执行 --->");
onRefresh();
} else if(mDestPading == -MAX_LENGHT){
}
invalidate();
updateView();
return true;
} else {
if(mState != STATE_REFRESH || (mState == STATE_REFRESH && deltaY>0)){
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
} else if(mState == STATE_REFRESH && deltaY<0 && mFirstChild.getTop()<=0){
if(mFirstChild.getTop() - deltaY > 0){
deltaY = mFirstChild.getTop();
}
mFirstChild.offsetTopAndBottom((int) -deltaY);
mListView.offsetTopAndBottom((int) -deltaY);
mPading = mFirstChild.getTop();
}
}
if(deltaY > 0 && mFirstChild.getTop() <= -MAX_LENGHT){
mPading = -MAX_LENGHT;
deltaY = -MAX_LENGHT - mFirstChild.getTop();
mFirstChild.offsetTopAndBottom((int) deltaY);
mListView.offsetTopAndBottom((int) deltaY);
mPading = mFirstChild.getTop();
updateView();
invalidate();
return false;
}
updateView();
invalidate();
return true;
}
private void updateView() {
String s = "";
s = mDate == null? "" : update_time + mDate ;
if(mState != STATE_REFRESH){
if(mFirstChild.getTop() < 0){
mArrow.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
mTitle.setText(pulldowntorefresh + s);
if(mLastTop >= 0 && mState != SCROLL_TO_CLOSE){
mArrow.startAnimation(mAnimationUp);
//Log.i("SimplePullDownView", " mAnimationUp 上动画 " );
}
} else if(mFirstChild.getTop() > 0){
mTitle.setText(releasetorefresh + s);
mProgressBar.setVisibility(View.INVISIBLE);
mArrow.setVisibility(View.VISIBLE);
if(mLastTop <= 0 ){
mArrow.startAnimation(mAnimationDown);
//Log.i("SimplePullDownView", " mAnimationDown 下动画 " );
}
}
}
mLastTop = mFirstChild.getTop();
}
private boolean release()
{
//Log.i("SimplePullDownView", "release 方法执行 = " + " , mFirstChild.getTop() = " + mFirstChild.getTop());
if(listviewDoScroll){
listviewDoScroll = false;
return true;
}
if(mFirstChild.getTop()>0){
scrollToUpdate();
} else {
scrollToClose();
// mDestPading = -MAX_LENGHT;
}
invalidate();
return false;
}
private void scrollToClose() {
mDestPading = -MAX_LENGHT;
mFlinger.startUsingDistance(MAX_LENGHT, CLOSEDELAY);
}
private void scrollToUpdate() {
mState = SCROLL_TO_REFRESH;
//Log.e("SimplePullDownView", "scrollToUpdate 方法执行");
mDestPading = 0;
mFlinger.startUsingDistance(mFirstChild.getTop(), REFRESHDELAY);
}
private void onRefresh(){
mState = STATE_REFRESH;
String s = "";
if(mDate == null)
s = "";
else
s = update_time + mDate;
mTitle.setText(loading + s);
mProgressBar.setVisibility(View.VISIBLE);
mArrow.setVisibility(View.INVISIBLE);
if(mRefreshListioner != null){
mRefreshListioner.onRefresh();
}
}
public void onRefreshComplete(){
onRefreshComplete(null);
}
public void onRefreshComplete(String date){
//Log.e("SimplePullDownView", "onRefreshComplete 方法执行 ");
mDate = date;
mState = SCROLL_TO_CLOSE;
/* mPading = -MAX_LENGHT;
mLastPading = -MAX_LENGHT;*/
mArrow.setImageResource(R.drawable.arrow_down);
mProgressBar2.setVisibility(View.INVISIBLE);
updateCommon();
scrollToClose();
}
private void updateCommon(){
if(mListView.getCount() == (mListView.getHeaderViewsCount() + mListView.getFooterViewsCount())){
Log.e("out", "数据为空");
if(useempty)
addEmptyHeaderView();
footer_layout.removeAllViews();
mListView.setFooterDividersEnabled(false);
} else {
removeEmptyHeaderView();
if(hasMore){
mListView.setFooterDividersEnabled(true);
footer_layout.removeAllViews();
footer_layout.addView(foot);
}else {
mListView.setFooterDividersEnabled(false);
footer_layout.removeAllViews();
}
}
}
public void onFirstLoad(){
if(footer_layout.getChildCount() == 0){
footer_layout.addView(foot);
}
isFirstLoading = true;
foot.setEnabled(false);
//Log.e("SimplePullDownView", "onFirstLoad 方法执行 ");
mState = STATE_REFRESH;
mProgressBar2.setVisibility(View.VISIBLE);
more.setText(R.string.loading2);
}
public void onLoadMore(){
//Log.e("SimplePullDownView", "onLoadMore 方法执行 ");
foot.setEnabled(false);
mState = STATE_REFRESH;
mProgressBar2.setVisibility(View.VISIBLE);
more.setText(R.string.loading2);
if(mRefreshListioner != null){
mRefreshListioner.onLoadMore();
}
}
public void onLoadMoreComplete(String date){
//Log.e("SimplePullDownView", "onLoadMoreComplete 方法执行 ");
mDate = date;
mState = -1;
mProgressBar2.setVisibility(View.INVISIBLE);
more.setText(R.string.more);
updateCommon();
if(isFirstLoading)
isFirstLoading = false;
foot.setEnabled(true);
}
public void onLoadMoreComplete( ){
onLoadMoreComplete(null);
}
public boolean dispatchTouchEvent(MotionEvent e){
//Log.e("SimplePullDownView", "dispatchTouchEvent ----------> ");
if(isFirstLoading){
return false;
}
int action;
float y = e.getY();
action = e.getAction();
if(mLongPressing && action != MotionEvent.ACTION_DOWN){
return false;
}
if(e.getAction() == MotionEvent.ACTION_DOWN){
mLongPressing = true;
}
boolean handled = true;
handled = mDetector.onTouchEvent(e);
// if(!handled)
// Log.i("SimplePullDownView", " mDetector.onTouchEvent handled = " + handled + " , mPading = " + mPading);
switch (action) {
case MotionEvent.ACTION_UP:
/*mListView.getHitRect(r);
boolean f = r.contains((int)e.getX(), (int)e.getY());*/
boolean f1 = mListView.getTop() <= e.getY() && e.getY() <= mListView.getBottom();
// Log.e("SimplePullDownView", " ACTION_UP -------> handled = " + handled + " , f1 = " + f1 + " , top == -MAX = " +(mFirstChild.getTop() == -MAX_LENGHT) );
if(!handled && mFirstChild.getTop() == -MAX_LENGHT && f1 || mState == STATE_REFRESH){
super.dispatchTouchEvent(e);
}
else{
// Log.e("SimplePullDownView", "执行释放方法 listviewDoScroll = " + listviewDoScroll);
handled = release();
}
break;
case MotionEvent.ACTION_CANCEL:
// Log.e(tag, " event: ACTION_CANCEL" );
handled = release();
super.dispatchTouchEvent(e);
break;
case MotionEvent.ACTION_DOWN:
downEvent = e;
mLongPressing = false;
// Log.e(tag, " 长按的时间间隔:" + ViewConfiguration.getLongPressTimeout());
postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout()+ 100);
mPendingRemoved = false;
downPostion = mListView.pointToPosition((int)e.getX(), (int)e.getY()) - mListView.getHeaderViewsCount();
//Log.e("SimplePullDownView", "downPostion = " + downPostion);
super.dispatchTouchEvent(e);
break;
case MotionEvent.ACTION_MOVE:
float deltaY = lastY - y;
lastY = y;
if(!mPendingRemoved){
removeCallbacks(mPendingCheckForLongPress);
mPendingRemoved = true;
}
//Log.e("SimplePullDownView","handled = " + handled + " , top = " + mFirstChild.getTop());
if(!handled && mFirstChild.getTop() == -MAX_LENGHT){
try{
return super.dispatchTouchEvent(e);
} catch (Exception e2) {
e2.printStackTrace();
return true;
}
}
else if(handled && mListView.getTop() > 0 && deltaY < 0){//deltaY小于0,向下
e.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(e);
}
break;
default:
break;
}
return true;
}
public void onAnimationEnd(Animation animation) {
int top = mFirstChild.getTop();
if (top < 0)
mArrow.setImageResource(R.drawable.arrow_down);
else if (top > 0)
mArrow.setImageResource(R.drawable.arrow_up);
else {
if(top < mLastTop){
mArrow.setImageResource(R.drawable.arrow_down);
} else {
mArrow.setImageResource(R.drawable.arrow_up);
}
}
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
public boolean onDown(MotionEvent e) {
return false;
}
public boolean onFling(MotionEvent motionevent, MotionEvent e,
float f, float f1) {
return false;
}
protected void onLayout(boolean flag, int i, int j, int k, int l) {
// Log.i("SimplePullDownView", "onLayout 方法执行 ------> mPading = " + mPading + " , listTop = " + (mPading + MAX_LENGHT));
int top = mPading;
int w = getMeasuredWidth();
mFirstChild.layout(0, top, w, top + MAX_LENGHT);
int h = getMeasuredHeight() + mPading + MAX_LENGHT;
mListView.layout(0, top + MAX_LENGHT , w, h);
}
public void onLongPress(MotionEvent e) {
}
/** deltaY > 0 向上*/
public boolean onScroll(MotionEvent curdown, MotionEvent cur, float deltaX, float deltaY) {
// mListView.getHitRect(r);
// Log.i("SimplePullDownView", "父亲高度 : = " + getHeight() + " , width = " + r.width() + " , height = " + r.height() + " , top = " + r.top + " , bottom = " + r.bottom);
deltaY = (float) ((double) deltaY * SCALE);
boolean handled = false;
boolean flag = false;
if (mListView.getCount() == 0) {
flag = true;
} else {
View c = mListView.getChildAt(0);
if (mListView.getFirstVisiblePosition() == 0 && c!=null && c.getTop() == 0){
flag = true;
}
// Log.i("SimplePullDownView","onScroll 方法执行 mPading = " + mPading + ", mTop = " + adapterview.getTop() + " flag = " + flag + " , deltaY = " + deltaY);
}
if (deltaY < 0F && flag || getChildAt(0).getTop() > -MAX_LENGHT){ //deltaY < 0 向下
handled = move(deltaY, false);
// Log.i("SimplePullDownView", "满足条件 执行 move 方法, handled = " + handled);
}
else
handled = false;
// Log.i("SimplePullDownView", " onScroll handled = " + handled);
return handled;
}
public void onShowPress(MotionEvent motionevent) {
}
public boolean onSingleTapUp(MotionEvent motionevent) {
return false;
}
public void setRefreshListioner(OnRefreshListioner RefreshListioner) {
mRefreshListioner = RefreshListioner;
}
public boolean isAutoLoadMore() {
return isAutoLoadMore;
}
public void setAutoLoadMore(boolean isAutoLoadMore) {
this.isAutoLoadMore = isAutoLoadMore;
if(!isAutoLoadMore){
foot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLoadMore();
}
});
mListView.setOnScrollListener(null);
} else {
mListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Log.i("SimplePullDownView", "scrollState = " + scrollState);
if(isEnd && scrollState == SCROLL_STATE_IDLE && hasMore){
onLoadMore();
}
}
@Override
public void onScroll(AbsListView view, int f, int v, int t) {
// Log.i("SimplePullDownView", "first = " + f + " , visable = " + v + " , total = " + t + " , hasMore = " + hasMore);
if(f + v >= t - 1)
isEnd = true;
else
isEnd = false;
}
});
foot.setOnClickListener(null);
}
}
public void setHasMore(boolean hasMore) {
this.hasMore = hasMore;
}
public void removeFoot(){
footer_layout.removeAllViews();
}
public void addFoot(){
footer_layout.removeAllViews();
footer_layout.addView(foot);
}
public static int MAX_LENGHT = 0;
public static final int STATE_REFRESH = 1;
public static final int SCROLL_TO_CLOSE = 2;
public static final int SCROLL_TO_REFRESH = 3;
public static final double SCALE = 0.9d;
private static final int CLOSEDELAY = 300;
private static final int REFRESHDELAY = 300;
private Animation mAnimationDown;
private Animation mAnimationUp;
private ImageView mArrow;
private String mDate;
private View emptyHeaderView;
private ProgressBar mProgressBar;
private TextView more;
private ProgressBar mProgressBar2;
private int mState;
private TextView mTitle;
ListView mListView;
LinearLayout foot;
LinearLayout footer_layout;
LinearLayout header;
private GestureDetector mDetector;
private FlingRunnable mFlinger;
private int mPading;
private int mDestPading;
// private int mLastPading;
private int mLastTop;
private LinearLayout mFirstChild;
private FrameLayout mUpdateContent;
private OnRefreshListioner mRefreshListioner;
private boolean isAutoLoadMore = false;
private boolean hasMore = true;
private boolean isEnd = true;
private boolean listviewDoScroll = false;
private boolean isFirstLoading = false;
// private boolean mIsAutoScroller;
private boolean mLongPressing;//如果设置为true说明刚好到了执行长按的时间
private boolean mPendingRemoved = false;//
private String pulldowntorefresh;
private String releasetorefresh;
private String loading;
private String update_time;
Rect r = new Rect();
private int downPostion;
private MotionEvent downEvent;
private CheckForLongPress mPendingCheckForLongPress = new CheckForLongPress();
private CheckForLongPress2 mPendingCheckForLongPress2 = new CheckForLongPress2();
private float lastY;
private boolean useempty = true;
}
8 楼
119568242
2012-01-10
谢谢兄弟了 最早找到 是在一个android网[忘记是哪个了 不怎么知名的] 然后 发帖人说是从外国找的。。所以我就拿来改了改用了
不好意思哈
需要我删掉我那个blog么?
求+好友Q:119568242
liuyuanbolyb@126.com
不好意思哈
需要我删掉我那个blog么?
求+好友Q:119568242
liuyuanbolyb@126.com
9 楼
119568242
2012-01-10
liuqun_567 写道
哥们儿,那个源码是我反编译新浪微博的下拉刷新然后受到一些启发自己写出了的,但是那些布局文件和资源文件都是直接拿过了的,所以我不敢把源码公开,我怕他们找我麻烦。既然你都这么说了,这两天我整理整理,给你份源码,不过我给你提个建议,这个下拉刷新就是自定义View,但是你要想看懂,你必须熟悉VieW的事件委派和处理机制,否则给你源代码你也看不懂。
=-=表示看错了一个东西 不过也希望认识下您
10 楼
godcok
2012-02-02
朋友您的下拉刷新我看过了,挺好,也在您的基础上实现了同样的功能,不过不知道怎么搞的列表会重复刷新.特别列表第一次载入和下拉的时候重复刷新特别明显.请问您遇到同样的问题了吗?
11 楼
godcok
2012-02-02
godcok 写道
朋友您的下拉刷新我看过了,挺好,也在您的基础上实现了同样的功能,不过不知道怎么搞的列表会重复刷新.特别列表第一次载入和下拉的时候重复刷新特别明显.请问您遇到同样的问题了吗?
如果方便的话请e_mail联系godcok@163.com,谢谢
12 楼
penghanying
2012-02-06
大哥 牛逼 给个源码 670166270@qq.com
13 楼
godcok
2012-02-10
godcok 写道
godcok 写道
朋友您的下拉刷新我看过了,挺好,也在您的基础上实现了同样的功能,不过不知道怎么搞的列表会重复刷新.特别列表第一次载入和下拉的时候重复刷新特别明显.请问您遇到同样的问题了吗?
如果方便的话请e_mail联系godcok@163.com,谢谢
问题已经解决了,不用联系了谢谢。
14 楼
一点触发
2012-02-15
大哥,给你发了短信。求一份源码,先谢谢啦!
15 楼
一点触发
2012-02-16
大哥,最近是不是没上线?反编译后看了还是不行呀,水平实现有限呀。能不能给小弟一份源码?!
16 楼
一点触发
2012-02-22
大哥,上线的话帮小弟一下!
17 楼
烦躁的秋千
2012-03-15
楼主给的apk运行时对的 但是上面的代码拷贝下去后,下拉出问题了
18 楼
liuqun_567
2012-03-15
apk里的是刚开始写的,现在的是改进版的,肯定需要改一下,具体资源文件区去新浪微博客户端里扒。
19 楼
wuhanliupeng
2012-03-16
求源码啊。。。。。。。。。唉。。
20 楼
qiujy
2012-03-19
新浪微博的还有一个功能,就是可以点击导航栏中的刷新来显示这个进度条。。。
21 楼
liuqun_567
2012-03-19
qiujy 写道
新浪微博的还有一个功能,就是可以点击导航栏中的刷新来显示这个进度条。。。
这个你可以自己加啊
22 楼
qiujy
2012-03-27
liuqun_567 写道
qiujy 写道
新浪微博的还有一个功能,就是可以点击导航栏中的刷新来显示这个进度条。。。
这个你可以自己加啊
晕。你的代码也写得太高深了。看不太懂。嘿嘿
23 楼
hwg-it-eye
2012-04-02
看了您写的下拉刷新,才发觉之前我自己写的弱爆了。若方便的话,希望您能发一份源码给我参考学习下,不作其他用途。472801202@qq.com ,谢谢了~
24 楼
liuqun_567
2012-04-05
hwg-it-eye 写道
看了您写的下拉刷新,才发觉之前我自己写的弱爆了。若方便的话,希望您能发一份源码给我参考学习下,不作其他用途。472801202@qq.com ,谢谢了~
我贴的源码是最新的,但那个例子里的代码不是最新的,我这没有最新的例子
25 楼
wuhanliupeng
2012-04-15
liuqun_567 写道
hwg-it-eye 写道
看了您写的下拉刷新,才发觉之前我自己写的弱爆了。若方便的话,希望您能发一份源码给我参考学习下,不作其他用途。472801202@qq.com ,谢谢了~
我贴的源码是最新的,但那个例子里的代码不是最新的,我这没有最新的例子
能不能把你的代码发我一份呢, 谢谢您了的啊。 麻烦了的 79144876@qq.com
26 楼
lwyr2010
2012-05-05
请问下这个offsetTopAndBottom起什么作用阿。
27 楼
liuqun_567
2012-05-07
lwyr2010 写道
请问下这个offsetTopAndBottom起什么作用阿。
Offset this view's vertical location by the specified number of pixels.
最新技术文章: