当前位置: 移动技术网 > IT编程>移动开发>Android > Android BottomSheet实现可拉伸控件

Android BottomSheet实现可拉伸控件

2020年03月09日  | 移动技术网IT编程  | 我要评论

天天想你伴奏,小虎当家,列车蛇灾

一、简介

bottom sheet是design support library23.2 版本引入的一个类似于对话框的控件。 bottom sheet中的内容默认是隐藏起来的,只显示很小一部分,可以通过在代码中设置其状态或者手势操作将其完全展开,或者完全隐藏,或者部分隐藏。

二、使用

1、添加依赖:

implementation 'com.android.support:design:28.0.0'

2、布局

<?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:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 
  <com.amap.api.maps.mapview
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
  <relativelayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginbottom="@dimen/height52px"
    app:behavior_hideable="false"
    app:behavior_peekheight="@dimen/height84px"
    app:layout_behavior="android.support.design.widget.bottomsheetbehavior"
    tools:ignore="missingprefix"
    android:background="#ffffffff"
    >
 
    <include layout="@layout/bottom_sheet" />
  </relativelayout>
 
</android.support.design.widget.coordinatorlayout>
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="@dimen/height216px"
  >
  <textview
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center"
    android:text="bottom_sheet_peek" />
</relativelayout>

3、代码实现

//底部抽屉栏展示地址
    mbehavior = bottomsheetbehavior.from(mrelativelayout);
 
    mbehavior.setbottomsheetcallback(new bottomsheetbehavior.bottomsheetcallback() {
      @override
      public void onstatechanged(@nonnull view bottomsheet, @bottomsheetbehavior.state int newstate) {
        string state = "null";
        switch (newstate) {
          case 1:
            state = "state_dragging";//过渡状态此时用户正在向上或者向下拖动bottom sheet
            break;
          case 2:
            state = "state_settling"; // 视图从脱离手指自由滑动到最终停下的这一小段时间
            break;
          case 3:
            state = "state_expanded"; //处于完全展开的状态
 
            break;
          case 4:
            state = "state_collapsed"; //默认的折叠状态
            break;
          case 5:
            state = "state_hidden"; //下滑动完全隐藏 bottom sheet
            break;
        }
 
      }
 
      @override
      public void onslide(@nonnull view bottomsheet, float slideoffset) {
        log.i("bottomsheetdemo", "slideoffset:" + slideoffset);
      }
    });

4、几个属性含义:

// behavior_hideable:定义是否能通过下滑手势收起bottom sheet。
   app:behavior_hideable="true"
  //  behavior_peekheight:定义可见部分的高度。
  app:behavior_peekheight="80dp"
  app:layout_behavior="android.support.design.widget.bottomsheetbehavior"

5、bottomsheet的五种状态:

state_dragging:手指在bottomsheet上下拖动从而使得布局跟着上下移动
state_settling:当手指抬起之后,会根据当前的偏移量,决定是要将bottomsheet收起还是展开

这两种属于中间态,类似于viewpager的scroll_state_dragging和scroll_state_settling
--------------------------------------
state_expanded:展开
state_collapsed:收起
state_hidden:隐藏

三、封装的框架推荐

flipboard/bottomsheet

soarcn/bottomsheet

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网