1. 程式人生 > >ScrollView的滑動事件===判斷是否滑到底部

ScrollView的滑動事件===判斷是否滑到底部

首先自定義一個ScrollView 
package one.bw.com.jingdong.sousuo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ScrollView;

import one.bw.com.jingdong.sousuo.inerts.ScrollViewListener;

/**
 * Created by Administrator on 2017/12/13/013.
 */
public class 
MyScrollView extends ScrollView {
//定義一個介面的物件
    private ScrollViewListener scrollViewListener = null;

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
//對外提供介面回撥的方法
    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this
.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); //scrollview的起始點+總高度==crollViewcomputeVerticalScrollRange if(getScrollY() + getHeight() == computeVerticalScrollRange()) { Log.d
("jiaba","------滾動到最下方------"); if(scrollViewListener!=null){ scrollViewListener.onScrollChanged(this, l, t, oldl, oldt); } } } }
在做一個介面的回撥,傳值
package one.bw.com.jingdong.sousuo.inerts;

import one.bw.com.jingdong.sousuo.view.MyScrollView;

/**
 * Created by Administrator on 2017/12/13/013.
 */
public interface ScrollViewListener {
    void onScrollChanged(MyScrollView context, int x, int y, int oldx, int oldy);
}
最後activity中的程式碼,,
//上拉載入資料
souxiangScrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
public void onScrollChanged(MyScrollView context, int x, int y, int oldx, int oldy) {
//                Toast.makeText(MySouSuoShow.this,"正在載入。。。",Toast.LENGTH_SHORT).show();
try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                List<MySouXiangBean.DataBean> data = mySouXiangBean.getData();
                list.addAll(data);
setAdapter();
}