判断ListView或者是ScrollView滚动到底部的方法。

ListView

OnScrollListener分析

ListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
Log.v("已经停止:SCROLL_STATE_IDLE");
break;
case OnScrollListener.SCROLL_STATE_FLING:
Log.v("开始滚动:SCROLL_STATE_FLING");
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
Log.v("正在滚动:SCROLL_STATE_TOUCH_SCROLL");
break;
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});


// 监听listview滚到最底部
mIndexList.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
// 当不滚动时
case OnScrollListener.SCROLL_STATE_IDLE:
// 判断滚动到底部
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
isLastisNext++;
}
break;
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});



ScrollView 需要覆盖一个新的类LazyScrollView


package com.dodowaterfall;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

public class LazyScrollView extends ScrollView {
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
onScrollListener.onAutoScroll(l, t, oldl, oldt);
}

private static final String tag = "LazyScrollView";
private Handler handler;
private View view;

public LazyScrollView(Context context) {
super(context);

}

public LazyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);

}

public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}

// 这个获得总的高度
public int computeVerticalScrollRange() {
return super.computeHorizontalScrollRange();
}

public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}

private void init() {

this.setOnTouchListener(onTouchListener);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {

super.handleMessage(msg);
switch (msg.what) {
case 1:
if (view.getMeasuredHeight() - 60 <= getScrollY()
+ getHeight()) {
if (onScrollListener != null) {
onScrollListener.onBottom();
}

} else if (getScrollY() == 0) {
if (onScrollListener != null) {
onScrollListener.onTop();
}
} else {
if (onScrollListener != null) {
onScrollListener.onScroll();
}
}
break;
default:
break;
}
}
};

}

OnTouchListener onTouchListener = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
if (view != null && onScrollListener != null) {
handler.sendMessageDelayed(handler.obtainMessage(1), 200);
}
break;
default:
break;
}
return false;
}

};

/**
* 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。
*/
public void getView() {
this.view = getChildAt(0);
if (view != null) {
init();
}
}

/**
* 定义接口
*
* @author admin
*/
public interface OnScrollListener {
void onBottom();

void onTop();

void onScroll();

void onAutoScroll(int l, int t, int oldl, int oldt);
}

private OnScrollListener onScrollListener;

public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
}


      waterfall_scroll.setOnScrollListener(new LazyScrollView.OnScrollListener() {

@Override
public void onTop() {
// 滚动到最顶端
Log.d("LazyScroll", "Scroll to top");
}

@Override
public void onScroll() {
if (menu_block.getVisibility() == View.VISIBLE) {
menu_block.setVisibility(View.GONE);
}
}

@Override
public void onBottom() {
// 滚动到最低端
// AddItemToContainer(++current_page, page_count);
}

@Override
public void onAutoScroll(int l, int t, int oldl, int oldt) {

Log.d("MainActivity",
String.format("%d %d %d %d", l, t, oldl, oldt));

// Log.d("MainActivity", "range:" + range);
// Log.d("MainActivity", "range-t:" + (range - t));
int scrollHeight = t;
scroll_height = waterfall_scroll.getMeasuredHeight();
Log.d("MainActivity", "scroll_height:" + scroll_height);

int longHeight = waterfall_scroll.getChildAt(0).getMeasuredHeight();
Log.d("MainActivity", "height:" + longHeight);

// 当滚到底部时 马上加载
if (scrollHeight + scroll_height >= longHeight - 40) {
if (!isFetchingNow && !noMoreProducts) {
new FetchAsyncTask(false).execute();
bottom_bar.setVisibility(View.VISIBLE);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值