您的位置:首页 > 编程语言 > Go语言

Google MaterialDesign_BottomSheetDialog简单用法

2016-08-16 21:49 579 查看
谷歌官方文档里叫BottomSheet(底部动作条),不过真心没有,值找到了BottomSheetDialog(底部表格式对话框,以下简称BSD)。具体效果就是从屏幕底部滑上来一个菜单,而且能一直向上滑动,是一个列表形式。这个菜单是通过适配器填充的,我最近一直用recyclerview来代替其他列表,所以今天亦是如此;这个BSD能满足更多需求,因为它是通过适配器填充的。运行了代码就知道了!

看一下效果图(形式一样不过是盗图,我一直没研究咋录制gif)



一 、 list.xml

这是列表中每一个item,其实按照我本意,里面的textview其实是所有的,所以你可以只放一个textview!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/textt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"

android:text="s" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="m" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="l" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="xl" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="XXL" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="XXXL" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="不好意思没更肥的了" />
</LinearLayout>


二 、 外置一个含有recyclerview的xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_main2" /><!--用于底部动作条-->


三、activity中的代码 可以在任何事件中调用如下函数

private void openBottom() {
RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.recyclerview, null);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
BottomSheetAdapter adapter = new BottomSheetAdapter();
recyclerView.setAdapter(adapter);

final BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(recyclerView);
dialog.show();
adapter.setOnItemClickListener(new BottomSheetAdapter.OnBottomItemClickListener() {
@Override
public void onItemClick(int position, String text) {
showToast(text);
dialog.dismiss();
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  谷歌 对话框