获取子视图在ScrollView中达到顶峰

时间:2016-04-07 10:04:03

标签: android android-layout android-scrollview vertical-scrolling

我正在使用Android App,我的需求是RelativeLayout的子项,它是ScrollView的子项,当RelativeLayout的子项在滚动时达到顶部时我怎么得到?

我的代码OnScrollChnnged是

final LinearLayout shareAndFuirteLayOut = (LinearLayout) findViewById(R.id.shareAndFuirteLayOut);
    final ScrollView mainScrollView = (ScrollView) findViewById(R.id.mainScrollView);

    final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {

        @Override
        public void onScrollChanged() {


        }
    };

    mainScrollView.setOnTouchListener(new View.OnTouchListener() {
        private ViewTreeObserver observer;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (observer == null) {
                observer = mainScrollView.getViewTreeObserver();
                observer.addOnScrollChangedListener(onScrollChangedListener);
            } else if (!observer.isAlive()) {
                observer.removeOnScrollChangedListener(onScrollChangedListener);
                observer = mainScrollView.getViewTreeObserver();
                observer.addOnScrollChangedListener(onScrollChangedListener);
            }
            return false;
        }
    });

1 个答案:

答案 0 :(得分:0)

详细介绍@Nanoc注释,代码将按

            if (nestedScrollView.getScrollY() > View.getY()) {
                Log.d(TAG, "View visible");
                stickyHeaderLayout.setVisibility(View.VISIBLE);
            } else {
                stickyHeaderLayout.setVisibility(View.GONE);
            }