Android控件BottomSheet实现底边弹出选择列表

yizhihongxing

下面是详细讲解 “Android控件BottomSheet实现底边弹出选择列表”的完整攻略。

什么是BottomSheet

BottomSheet是安卓提供的一个UI控件,可以实现在屏幕底部弹出窗口,通常用于展示一些与主要内容相关的操作选项或者附属功能。BottomSheet有两种类型:持续BottomSheet和模态BottomSheet。持续BottomSheet是可以在主UI上同时滚动的,而模态BottomSheet会覆盖主UI的一部分,只有在底部背景上滑动才能关闭。

实现步骤

  1. 在XML布局中定义BottomSheet,设置其样式和高度等属性:
<LinearLayout
  android:id="@+id/bottom_sheet"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/white"
  app:behavior_hideable="true"
  app:behavior_peekHeight=“@dimen/bottom_sheet_peek_height”
  app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

  <!-- 在这里添加Bottom Sheet显示的内容 -->

</LinearLayout>

其中app:behavior_hideable是用来控制BottomSheet是否可以隐藏,app:behavior_peekHeight是设置BottomSheet初始高度,app:layout_behavior是BottomSheet在使用时应该绑定的行为。

  1. 创建BottomSheetBehavior,绑定BottomSheet:
LinearLayout bottomSheet = findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
  1. 设置BottomSheetBehavior的相关属性:
bottomSheetBehavior.setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_sheet_peek_height));
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setSkipCollapsed(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

其中,setPeekHeight()设置卡片最初状态的高度,setHideable()设置是否可以通过手势收缩BottomSheet, setSkipCollapsed()控制当BottomSheet状态为STATE_DRAGGING时,是否跳过折叠状态, 直接变成有设置的peek高度状态。

  1. 监听BottomSheet的状态改变,对BottomSheet进行相应操作:
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            // BottomSheet已隐藏,执行一些操作
        } else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            // BottomSheet在完全扩展时执行操作
        } else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
            // BottomSheet在折叠状态下执行操作
        } else if (newState == BottomSheetBehavior.STATE_DRAGGING) {
            // BottomSheet在拖拽状态下执行操作
        } else if (newState == BottomSheetBehavior.STATE_SETTLING) {
            // BottomSheet在拖拽后自动设置状态时执行操作
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // 对拖动中的BottomSheet进行操作
    }
});

我们可以在Callback中监听到BottomSheet的各种状态,并相应进行相应操作。

示例

示例1:BottomSheet实现底部菜单

在BottomSheet中定义菜单项,并为其设置点击事件:

<LinearLayout
  android:id="@+id/bottom_sheet"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/white"
  app:behavior_hideable="true"
  app:behavior_peekHeight=“@dimen/bottom_sheet_peek_height”
  app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

  <TextView
    android:id="@+id/menu_item1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/menu_item1"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

  <TextView
    android:id="@+id/menu_item2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/menu_item2"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

</LinearLayout>

然后在代码中为其设置点击事件,并在BottomSheetCallback中监听其状态:

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

TextView menuItem1 = findViewById(R.id.menu_item1);
menuItem1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 在此处实现点击事件
    }
});

TextView menuItem2 = findViewById(R.id.menu_item2);
menuItem2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 在此处实现点击事件
    }
});

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            // 当BottomSheet隐藏后执行此处代码
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // 当BottomSheet在拖拽中时执行此处代码
    }
});

示例2:BottomSheet实现底部分享选择

在BottomSheet中添加分享渠道,分别定义:

<LinearLayout
  android:id="@+id/bottom_sheet"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@android:color/white"
  app:behavior_hideable="true"
  app:behavior_peekHeight=“@dimen/bottom_sheet_peek_height”
  app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

  <TextView
    android:id="@+id/share_wechat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/share_wechat"
    android:drawableLeft="@drawable/ic_wechat"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

  <TextView
    android:id="@+id/share_qq"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/share_qq"
    android:drawableLeft="@drawable/ic_qq"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

  <TextView
    android:id="@+id/share_weibo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/share_weibo"
    android:drawableLeft="@drawable/ic_weibo"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

  <TextView
    android:id="@+id/cancel_share"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cancel_share"
    android:textColor="@color/colorPrimary"
    android:padding="16dp"
    android:gravity="center"
    android:background="?android:attr/selectableItemBackground"/>

</LinearLayout>

然后在代码中为其设置点击事件,并在BottomSheetCallback中监听其状态:

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

TextView shareWechat = findViewById(R.id.share_wechat);
shareWechat.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 在此处实现分享到微信代码
    }
});

TextView shareQQ = findViewById(R.id.share_qq);
shareQQ.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 在此处实现分享到QQ代码
    }
});

TextView shareWeibo = findViewById(R.id.share_weibo);
shareWeibo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 在此处实现分享到微博代码
    }
});

TextView cancelShare = findViewById(R.id.cancel_share);
cancelShare.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    }
});

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            // 当BottomSheet隐藏后执行此处代码
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        // 当BottomSheet在拖拽中时执行此处代码
    }
});

当我们点击分享按钮后,BottomSheet被展开,当取消分享时,可以通过设置BottomSheet为隐藏状态来关闭BottomSheet。 同时使用setPeekHeight()通过改变BottomSheet的高度,控制BottomSheet的展示状态。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android控件BottomSheet实现底边弹出选择列表 - Python技术站

(0)
上一篇 2023年5月30日
下一篇 2023年5月30日

相关文章

  • iframe标签用法详解(属性、透明、自适应高度)

    本文将详细讲解iframe标签的用法,包括其属性、如何设置透明度、如何实现自适应高度等。下面我们将逐一介绍。 1. iframe标签的基本用法 iframe(内联框架)是HTML中的一种标签,用于在网页中嵌入其他网页或文档。使用iframe可以在页面中嵌套显示其他页面的内容,实现网页的框架分割、拉取外部数据等功能。 以下是iframe标签的基本语法: &lt…

    html 2023年5月30日
    00
  • C++中TinyXML读取xml文件用法详解

    C++中TinyXML读取xml文件用法详解 什么是TinyXML TinyXML是一款C++语言编写的轻量级XML解析库。它适用于读取和写出XML文件。 TinyXML提供了一组简单易用的API,使得开发者可以方便地读取XML文件中的数据,并且以相同的方式修改XML文件。 安装和引入TinyXML TinyXML提供了可执行程序和源代码两种方式供用户使用。…

    html 2023年5月30日
    00
  • asp.net URL中包含中文参数造成乱码的解决方法

    当ASP.NET的URL地址中包含中文参数时,会遇到URL乱码的问题,这是因为URL中的中文字符默认是被编码的,如果没有进行正确的解码,就会造成乱码的问题。 解决方法如下: 1.使用HttpUtility.UrlEncode和HttpUtility.UrlDecode进行编码和解码 ASP.NET提供了HttpUtility.UrlEncode和HttpUt…

    html 2023年5月31日
    00
  • HTML5新增的标签和属性归纳总结

    当HTML5标准发布之后,它为我们带来了更多的标签和属性,这些新的特性可以使我们更加方便地构建Web页面。下面我们对HTML5新增的标签和属性进行归纳总结。 HTML5新增的标签 语义化标签 HTML5中引入了许多语义化的标签,它们能够让网页的内容更加明确和易于理解。这些标签包括: <article>:定义文章、论坛贴子、博客等独立的内容单元。 …

    html 2023年5月30日
    00
  • Java中四种XML解析技术

    Java中常用的四种XML解析技术包括DOM、SAX、JDOM和DOM4J。每种技术都有自己的特点和适用场景。下面分别进行详细讲解: DOM DOM(Document Object Model)是一种W3C推荐的XML解析技术,能够将整个XML文档加载到内存中,并以树形结构的形式表示XML文档的各个元素节点,因此能够轻松进行对XML文档的读写操作。 DOM解…

    html 2023年5月30日
    00
  • HTML name、id、class 的(格式/应用场景/特性)等区别介绍

    接下来是关于HTML name、id、class 的区别介绍。 HTML name、id、class 的格式 name name 属性需要在被命名的 HTML 元素中定义,其语法格式为: <tag name="value">…</tag> 其中,tag 为标签名,name 为属性名,value 为属性值。 id…

    html 2023年5月30日
    00
  • 比特币怎么挖矿教程 比特币的挖矿教程详细步骤

    以下是“比特币怎么挖矿教程 比特币的挖矿教程详细步骤”的完整攻略: 比特币怎么挖矿教程 比特币挖矿是指通过计算机运算来验证比特币交易并获得比特币奖励的过程。以下是一些比特币挖矿的详细步骤和攻略。 步骤1:选择比特币挖矿硬件 在进行比特币挖矿之前,需要选择比特币挖矿硬件。以下是一些选择比特币挖矿硬件的方法: 选择ASIC矿机,这是一种专门用于比特币挖矿的硬件设…

    html 2023年5月18日
    00
  • 浅析HTML5页面元素及属性

    下面是针对“浅析HTML5页面元素及属性”的完整攻略: 一、HTML5页面元素介绍 HTML(Hypertext Markup Language)是用于创建网页的标准标记语言。HTML5是HTML的最新版本,引入了许多新的元素,包括: 1. \ 定义文档的头部区域,通常包括网站的Logo、导航链接以及其他重要信息。 <header> <a …

    html 2023年5月30日
    00
合作推广
合作推广
分享本页
返回顶部