Android 顶部悬浮栏 + ViewPager + RecyclerView

今天写代码遇到一个问题,在ScrollView中加入ViewPager和RecyclerView   list数据显示不完整,而且界面想要实现顶部悬浮的导航栏,所以我们只有重写ScrollView,受到@dyy_csdn的分发事件的启示,由于发生了控件的滑动冲突,所以我们要解决冲突,怎么解决呢?在@dyy_csdn的分发事件这篇博客中有详细提到过,所以我在此我就不做赘述了。
博客地址:http://blog.csdn.net/dyy_csdn/article/details/60574803
要实现悬浮的导航栏只需要记住两点:
	   1、导航栏的悬浮其实就是控件的显示与隐藏
	2、需要测量滑动的距离,重写ScrollView
创建接口:
public interface ScrollViewListener {

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

}

我们需要重写onScrollChanged这个方法,传入接口参数
 
  
@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了
在onCreate方法中获取控件的宽高是为0的。所以我们应该在控件绘制好之后开始测量,使用如下方法;
 
  
ViewTreeObserver vto2 = tab.getViewTreeObserver();
vto2.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Overrde
    public boolean onPreDraw() {
        if (!hasMeasured1){
            tabLayoutHeight = tab.getMeasuredHeight();
            tabLayoutTop = tab.getTop();
            Log.e("onGlobalLayout","--"+tabLayoutTop+"---"+tabLayoutHeight);
            hasMeasured1 = true;
        }
        return true;
    }
});
设置监听事件scrollView.setScrollViewListener(this);
代码中的150是我自己滑动测量的,大家可以根据自己的需要设定不同的值,来显示隐藏导航栏
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
    int[] location = new int[2];
    tab.getLocationOnScreen(location);
    int locationY = location[1];
    //Log.e("locationY", locationY + "  " + "topHeight的值是:" + (tabLayoutHeight+tabLayoutTop));

    if (locationY <= 150 && (tab2.getVisibility() == View.GONE || tab2.getVisibility() == View.INVISIBLE)) {
        tab2.setVisibility(View.VISIBLE);
    }

    if (locationY > 150 && tab2.getVisibility() == View.VISIBLE) {
        tab2.setVisibility(View.GONE);
    }

}

public class ObservableScrollView extends ScrollView {
    private int mTouchSlop;
    private int downX;
    private int downY;
        private ScrollViewListener scrollViewListener = null;

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

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

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

        public void setScrollViewListener(ScrollViewListener scrollViewListener) {
            this.scrollViewListener = scrollViewListener;
        }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
        @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);
            }
        }

    public interface ScrollViewListener {

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

    }
}

你可能感兴趣的:(Android 顶部悬浮栏 + ViewPager + RecyclerView)