ScrollView的滚动事件--监听顶部、底部

ScrollView 滚动监听,一般不能直接使用,需要重写ScrollView的onScrollChanged的方法,在onScrollChanged函数中判断

方式一:

自定义滚动接口:

public interface ScrollViewListener {

    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);

}

自定义ScrollView子类 ObservableScrollView


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

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

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

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }
   //重写滚动方法
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

}

在Activity中使用

 scrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (scrollView.getScrollY() == 0) {
                    Log.i("顶部", "顶部");
                }
                if (scrollView.getScrollY() + scrollView.getHeight() - scrollView.getPaddingTop() -
                        scrollView.getPaddingBottom() == scrollView.getChildAt(0).getHeight()) {
                  System.out.println("底部");

               }
        });

方式二:

自定义滚动


public class MyScrollView extends ScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;

    public boolean isTop() {
        return isTop;
    }

    public void setTop(boolean top) {
        isTop = top;
    }

    private boolean isTop = false;//是不是滑动到了最低端 ;使用这个方法,解决了上拉加载的问题
    private OnScrollToBottomListener onScrollToBottom;

    public MyScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
                                  boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (scrollY != 0 && null != onScrollToBottom && isTop()) {
            onScrollToBottom.onScrollBottomListener(clampedY);
        }
    }

    public void setOnScrollToBottomLintener(OnScrollToBottomListener listener) {
        onScrollToBottom = listener;
    }

    public interface OnScrollToBottomListener {
        public void onScrollBottomListener(boolean isBottom);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                setTop(false);
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                Log.i("-----::----downY-----::", downY + "");
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                Log.i("-----::----moveY-----::", moveY + "");
             /****判断是向下滑动,才设置为true****/
                if (downY - moveY > 0) {
                    setTop(true);
                } else {
                    setTop(false);
                }
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}

在Activity中

  scrollView.setOnScrollToBottomLintener(new MyScrollView.OnScrollToBottomListener() {
            @Override
            public void onScrollBottomListener(boolean isBottom) {
                if (isBottom&&scrollView.isTop()) {
                  
                }
            }
        });

recyclerview禁止滑动

 homeTwoRecyclerView.setNestedScrollingEnabled(false);

2、

LinearLayoutManager linearLayoutManager = new LinearLayoutManager( getActivity(),
      LinearLayoutManager.VERTICAL, false) {  

     @Override  
 
public boolean canScrollVertically() {
  
 
return false;
  

}
  
   };
recyclerview.setLayoutManager(linearLayoutManager);  
recyclerview.setAdapter(tempCommonAdapter);  




 LinearLayoutManager  linearLayoutManager = new LinearLayoutManager(context) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

3、

custommanager = new LinearLayoutManager(getActivity()) {
          @Override
            public boolean canScrollVertically() {
              return false;
           }
        };

参考文章:

Android ScrollView监听滑动到顶部和底部的两种方式

你可能感兴趣的:(ScrollView的滚动事件--监听顶部、底部)