Android Support 23.2 BottomSheetBehavior的使用

Android Support 23.2前几天新鲜出炉,赶紧磨刀霍霍向牛羊吐舌头

一:底部菜单犹抱琵琶半遮面(即peekHeight=56dp)

1,新建工程BottomSheetBehavior

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.bottomsheetbehavior"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
}

2,xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.bottomsheetbehavior.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/design_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="true"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ccc"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:gravity="center"
                android:text="BottomSheets"/>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:src="@mipmap/bg"/>

        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

好了,现在可以运行看下:(小米2原生android6.0.1)

可以看到,TextView向下隐藏了一点(当时还在找BottomSheetBehavior的资料时看到了下面这个)

在父控件中添加了一句android:fitsSystemWindows="true",是为了改变statusBar的颜色,Material Design嘛!
这个button就是少了statusBar的高度了,所以就改了下代码,思路是,当创建bottomsheets时向上移动statusbar的高度,当 bottomsheets发生移动时,就让bottomsheets的高度还原,思路是这样了,贴代码,省的以后用到了忘记(http://www.jian shu.com/p/7b1bb6c0c1f0#

3,所以,代码修改如下:

View bottomSheet = findViewById(R.id.design_bottom_sheet);
bottomSheet.setTranslationY(-getStatusBarHeight(this));
BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(bottomSheet);
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    boolean first = true;

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {

    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        if (first) {
            first = false;
            bottomSheet.setTranslationY(0);
        }
    }
});
运行结果如下:





二:底部菜单完全隐藏(即peekHeight=0dp or 不设置peekHeight) 

在第一部分的第二步设置peekHeight=0或者不设置peekHeoght,再运行一遍,坑爹的恐怖分子,android5.x平台滑动不出来!!手动设置也没有!!
然后再 http://www.jianshu.com/p/21bb14e3be94/comments/1570995#comment-1570995找到了答案
但是我的做法是,在第一部分里只更改xml里的peekHeight(peekHeight=0或者不设置peekHeoght),一样解决了这个问题~~


大神源码解析:https://github.com/android-cjj/SourceAnalysis/blob/master/README.md
解坑:http://www.jianshu.com/p/21bb14e3be94

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值