您好,欢迎来到星星旅游。
搜索
您的当前位置:首页Android仿高德地图首页底部拉起弹窗BottomSheetBehavior

Android仿高德地图首页底部拉起弹窗BottomSheetBehavior

来源:星星旅游

Android仿高德地图首页

贴上主要代码,转载注明出处,谢谢

private int state = 0;//上拉框状态0代表完全展开,1代表折叠到最底部,2代表折叠到中间
private int height = 200;//折叠到中间时的布局高度

@Override
protected void initData() {
    //测量部分折叠高度
    measureHeight();
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(llBottom);
    //上拉框状态监听
    bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState){
                //下滑动完全隐藏
                case BottomSheetBehavior.STATE_HIDDEN:
                    if (state == 2){
                        bottomSheetBehavior.setPeekHeight(llTop.getHeight());
                        bottomSheetBehavior.setHideable(false);
                    }
                    break;
            }

        }
        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            if (state == 2){
                bottomSheetBehavior.setPeekHeight(llTop.getHeight());
            }
            //gettop越小,越接近手机顶部。所以上拉到小于手机高度的1/3的时候完全展开
            if (bottomSheet.getTop() < coordinatorLayout.getHeight() / 3) {
                state = 0;
                if (attractionAdapter == null) {
                    attractionAdapter = new AttractionAdapter(attractionBeanArrayList);
                }
                recyclerview.setAdapter(attractionAdapter);
                recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
            } else if (bottomSheet.getTop() > coordinatorLayout.getHeight() - height) {
                state = 1;
                if (attractionHorizontalAdapter == null) {
                    attractionHorizontalAdapter = new AttractionHorizontalAdapter(attractionBeanArrayList);
                }
                recyclerview.setAdapter(attractionHorizontalAdapter);
                recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
                bottomSheetBehavior.setPeekHeight(llTop.getHeight());
                bottomSheetBehavior.setHideable(false);
            } else {
                state = 2;
                if (attractionHorizontalAdapter == null) {
                    attractionHorizontalAdapter = new AttractionHorizontalAdapter(attractionBeanArrayList);
                }
                recyclerview.setAdapter(attractionHorizontalAdapter);
                recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
                bottomSheetBehavior.setPeekHeight(height);
                bottomSheetBehavior.setHideable(true);
            }
        }
    });
}
private void measureHeight(){
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    llTop.measure(w, h);
    int llTopHeight = llTop.getMeasuredHeight();

    llTitle.measure(w, h);
    int llTitleHeight = llTitle.getMeasuredHeight();

    recyclerview.measure(w, h);
    int recyclerviewHeight = recyclerview.getMeasuredHeight();
    height = llTopHeight + llTitleHeight + recyclerviewHeight;
    Log.e("zlzheight",height+"");
}

需要代码自取,很早以前写的了,有帮助到你点个赞吧

链接:https://pan.baidu.com/s/1CR-41fx4pOaQfOJqXo1SWg 
提取码:gxgw

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务