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

编程技术  /  houtizong 发布于 3年前   221
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);                    }                }          }     }

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客