BottomSheet组件用法

一、布局:

将需要转换为BottomSheet类组件的组件属性中加入以下属性:

behavior_peekHeight:表示Bottomsheet类组件的默认显示高度

behavior_hideable:表示组件可否完全隐藏

        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        app:behavior_peekHeight="100dp"
        app:behavior_hideable="true"

bottomsheet类组件必须为android.support.design.widget.CoordinatorLayout布局的子组件,该布局需要在gradle.app中添加design依赖才能使用,版本23.0.0以上就行,代码如下:

dependencies {
    implementation 'com.android.support:design:28.0.0-rc01'
}

整体布局代码如下:

<?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"
    tools:context=".MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottomsheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        app:behavior_peekHeight="100dp"
        app:behavior_hideable="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="50dp"
                android:background="@color/colorPrimary"
                android:text="人不会死在绝境,却往往栽在十字路口"
                android:textColor="@android:color/white"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="50dp"
                android:background="@color/colorPrimary"
                android:text="人不会死在绝境,却往往栽在十字路口"
                android:textColor="@android:color/white"/>
        </LinearLayout>
        
    </android.support.v4.widget.NestedScrollView>

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

二、Activity

在Java中可以通过获取到BottomSheetBehavior对象来对bottomsheet类组件进行状态控制:

bottomSheet = findViewById(R.id.bottomsheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

以下四种状态:

STATE_COLLAPSED:默认折叠状态, bottom sheet只在底部显示一部分布局。显示高度可以通过 app:behavior_peekHeight 设置(默认是0)

STATE_DRAGGING:拖动状态,此时用户正在向上或者向下拖动bottom sheet

STATE_SETTLING:自由滑动状态,视图从脱离手指自由滑动到最终停下的这一小段时间

STATE_EXPANDED:完全展开的状态,当bottom sheet的高度低于CoordinatorLayout容器时,整个bottom sheet都可见;或者CoordinatorLayout容器已经被bottom sheet 填满

STATE_HIDDEN:完全隐藏状态(可通过app:behavior_hideable =true启用此状态),启用后用户将能通过向下滑动完全隐藏 bottom sheet

 

通过该对象setstate以及getstate进行状态逻辑判断:

if(bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_COLLAPSED){
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值