简体   繁体   English

如何确定垂直ScrollView的方向?

[英]How can I determine the direction of a vertical ScrollView?

How can I determine the direction of a vertical ScrollView? 如何确定垂直ScrollView的方向? I need to hide and display another linear layout accoding to scrollview scroll 我需要隐藏并显示另一条线性布局以适应scrollview滚动

You can use the onScrollChanged(int l, int t, int oldl, int oldt) method for that. 您可以使用onScrollChanged(int l, int t, int oldl, int oldt)方法。 As there isn't a setter for a listener, but just that method, you will have to create your own ScrollView class and override that method and do your thing in your implementation. 由于没有用于侦听器的设置器,而只有该方法,因此您将必须创建自己的ScrollView类并重写该方法,然后在实现中执行操作。

public class ObservableScrollView extends ScrollView {
private static final int DEFAULT_THRESHOLD_DP = 4;

private ScrollDirectionListener scrollDirectionListener;
private int scrollThreshold;

public ObservableScrollView(Context context) {
    this(context, null);
}

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

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

private void init(Context context) {
    scrollThreshold = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_THRESHOLD_DP, context.getResources().getDisplayMetrics());
}

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);

    if (Math.abs(y - oldY) > scrollThreshold && scrollDirectionListener != null) {
        if (y > oldY) {
            scrollDirectionListener.onScrollUp(Math.abs(y - oldY));
        } else {
            scrollDirectionListener.onScrollDown(Math.abs(y - oldY));
        }
    }
}

public void setScrollThresholdInPx(int px) {
    scrollThreshold = px;
}

public void setScrollThresholdInDp(Context context, float dp) {
    scrollThreshold = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

public void setOnScrollDirectionListener(ScrollDirectionListener listener) {
    scrollDirectionListener = listener;
}

public interface ScrollDirectionListener {
    void onScrollDown(int pixels);

    void onScrollUp(int pixels);
}}

got answer :) use this custom scrollview 得到答案:)使用此自定义scrollview

ScrollView.setOnScrollDirectionListener(new ObservableScrollView.ScrollDirectionListener() {
        @Override
        public void onScrollDown(int pixels) {
            contenttool.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onScrollUp(int pixels) {
            contenttool.setVisibility(View.VISIBLE);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM