当前位置: 移动技术网 > 移动技术>移动开发>Android > android BottomSheetDialog新控件解析实现知乎评论列表效果(实例代码)

android BottomSheetDialog新控件解析实现知乎评论列表效果(实例代码)

2020年06月23日  | 移动技术网移动技术  | 我要评论

bottomsheetdialog使用解析

android support library 23.2里的 design support library新加了一个bottom sheets控件,bottom sheets顾名思义就是底部操作控件,用于在屏幕底部创建一个可滑动关闭的视图,可以替代对话框和菜单。其中包含bottomsheets、bottomsheetdialog和bottomsheetdialogfragment三种可以使用。其中应用较多的控件是bottomsheetdialog,主要运用在界面底部分享列表,评论列表等,最近在知乎评论列表界面看到知乎运用到了这个效果,所有在这里详细介绍一下该控件的使用,以及简单实现知乎评论列表功能。本文实现效果如下:

这里写图片描述

首先我们想要使用bottomsheets相关控件,需要先在build.gradle中添加design依赖,本文中使用的是:

compile 'com.android.support:design:25.3.0'

bottomsheetdialog可以替代大多数网格显示和列表展示的dialog和popupwindow,默认宽度撑满,并且在bottomsheetdialog 区域中向下滑动也让对话框消失。

接下来创建bottomsheetdialog的布局文件dialog_bottomsheet.xml,布局文件如下:

<?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="wrap_content">

 <relativelayout
 android:id="@+id/dialog_bottomsheet_rl_title"
 android:layout_width="match_parent"
 android:layout_height="45dp"
 android:background="@drawable/dialog_bottomsheet_shape">

 <imageview
 android:id="@+id/dialog_bottomsheet_iv_close"
 android:layout_width="45dp"
 android:layout_height="45dp"
 android:layout_centervertical="true"
 android:layout_marginleft="5dp"
 android:padding="5dp"
 android:src="@drawable/img_close" />

 <textview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centervertical="true"
 android:layout_marginleft="10dp"
 android:layout_torightof="@id/dialog_bottomsheet_iv_close"
 android:text="评论"
 android:textcolor="#333"
 android:textsize="16sp" />

 </relativelayout>

 <android.support.v7.widget.recyclerview
 android:id="@+id/dialog_bottomsheet_rv_lists"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_below="@id/dialog_bottomsheet_rl_title"
 android:background="#fff" />

</relativelayout>

布局文件中,主要包含一个recyclerview和一个头布局。

然后,我们在activity界面添加bottomsheetdailog初始化方法,

private void showsheetdialog() {
 view view = view.inflate(bottomsheetdialogactivity.this, r.layout.dialog_bottomsheet, null);
 iv_dialog_close = (imageview) view.findviewbyid(r.id.dialog_bottomsheet_iv_close);
 rv_dialog_lists = (recyclerview) view.findviewbyid(r.id.dialog_bottomsheet_rv_lists);

 iv_dialog_close.setonclicklistener(this);

 bottomsheetadapter = new bottomsheetadapter(bottomsheetdialogactivity.this, list_strs);
 rv_dialog_lists.sethasfixedsize(true);
 rv_dialog_lists.setlayoutmanager(new linearlayoutmanager(bottomsheetdialogactivity.this));
 rv_dialog_lists.setitemanimator(new defaultitemanimator());
 rv_dialog_lists.setadapter(bottomsheetadapter);

 bottomsheetdialog = new bottomsheetdialog(bottomsheetdialogactivity.this, r.style.dialog);
 bottomsheetdialog.setcontentview(view);
 }

在改方法中,我们首先获取bottomsheetdialog的布局文件,获取该布局文件中相关控件,通过创建模拟列表数据,为recyclerview添加适配器

for (int i=0; i<20; i++) {
 list_strs.add("评论" + i);
 }

通过如下代码,创建bottomsheetdialog对象

bottomsheetdialog = new bottomsheetdialog(bottomsheetdialogactivity.this, r.style.dialog);
bottomsheetdialog.setcontentview(view);

至此,我们即可以通过调用

bottomsheetdialog.show();

方法来查看bottomsheetdialog显示效果

使用过程中出现的问题

当我们向下滑动bottomsheetdialog隐藏dialog后,无法用bottomsheetdialog.show()再次打开,为什么呢?我们先看下源码的实现:

@override
public void setcontentview(view view, viewgroup.layoutparams params) {
 super.setcontentview(wrapinbottomsheet(0, view, params));
}

private view wrapinbottomsheet(int layoutresid, view view, viewgroup.layoutparams params) {
 final coordinatorlayout coordinator = view.inflate(getcontext(),r.layout...., null);
 framelayout bottomsheet = (framelayout) coordinator.findviewbyid(r.id.design_bottom_sheet);
 bottomsheetbehavior.from(bottomsheet).setbottomsheetcallback(mbottomsheetcallback);
 ...
 return coordinator;
}

private bottomsheetcallback mbottomsheetcallback = new bottomsheetcallback() {
 @override
 public void onstatechanged(@nonnull view bottomsheet, int newstate) {
 if (newstate == bottomsheetbehavior.state_hidden) {
 dismiss(); //关键代码
 }
 }

 @override
 public void onslide(@nonnull view bottomsheet, float slideoffset) {
 }
};

通过源码文件我们可以看出,系统的bottomsheetdialog是基于bottomsheetbehavior封装的,当我们滑动隐藏了bottomsheetbehavior中的view后,内部是设置了bottomsheetbehavior的状态为state_hidden,接着它替我们关闭了dialog,所以我们再次调用show()的时候dialog没法再此打开状态为hide的dialog了。
查看了源文件,我们就通过复写bottomsheetcallback的回调方法,来实现我们的效果,这里就引入了bottomsheetbehavior,下面先介绍bottomsheetbehavior的使用。

bottomsheetbehavior的作用

根据官方api,bottomsheetbehavior有一个静态方法bottomsheetbehavior.from(view),会返回这个view引用的bottomsheetbehavior,这个方法会检查这个view是否是coordinatorlayout的子view,如果是就会得到这个view的behavior。通过bottomsheetbehavior,我们可以通过setpeekheight(int height)设置dialog的显示高度,通过setbottomsheetcallback(callback)实现bottomsheetdialog的状态监听。其中,在bottomsheetcallback回调方法中,onstatechanged监听状态的改变,onslide是拖拽的回调,onstatechanged可以监听到的回调一共有五种:

  • state_hidden: 隐藏状态。默认是false,可通过app:behavior_hideable属性设置。
  • state_collapsed: 折叠关闭状态。可通过app:behavior_peekheight来设置显示的高度,peekheight默认是0。
  • state_dragging: 被拖拽状态
  • state_settling: 拖拽松开之后到达终点位置(collapsed or expanded)前的状态。
  • state_expanded: 完全展开的状态。

那么如何获取到bottomsheetdialog的bottomsheetbehavior呢?

第一种:在bottomsheetdialog调用setcontentview方法之后,调用

bottomsheetbehavior mdialogbehavior = bottomsheetbehavior.from((view) mcontentview.getparent());

第二种:在bottomsheetdialog调用setcontentview方法之后,调用

final framelayout framelayout = (framelayout) dialog.findviewbyid(android.support.design.r.id.design_bottom_sheet);
framelayout.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
 @override
 public void ongloballayout() {
 framelayout.getviewtreeobserver().removeglobalonlayoutlistener(this);
 bottomsheetbehavior behavior = bottomsheetbehavior.from(framelayout);

 //调用behavior相关方法
 ... 

 framelayout.forcelayout();

}

通过上面的介绍,修改上文中的showsheetdialog()用于解决上文中出现的问题,代码如下:

private void showsheetdialog() {
 view view = view.inflate(bottomsheetdialogactivity.this, r.layout.dialog_bottomsheet, null);
 iv_dialog_close = (imageview) view.findviewbyid(r.id.dialog_bottomsheet_iv_close);
 rv_dialog_lists = (recyclerview) view.findviewbyid(r.id.dialog_bottomsheet_rv_lists);

 iv_dialog_close.setonclicklistener(this);

 bottomsheetadapter = new bottomsheetadapter(bottomsheetdialogactivity.this, list_strs);
 rv_dialog_lists.sethasfixedsize(true);
 rv_dialog_lists.setlayoutmanager(new linearlayoutmanager(bottomsheetdialogactivity.this));
 rv_dialog_lists.setitemanimator(new defaultitemanimator());
 rv_dialog_lists.setadapter(bottomsheetadapter);

 bottomsheetdialog = new bottomsheetdialog(bottomsheetdialogactivity.this, r.style.dialog);
 bottomsheetdialog.setcontentview(view);
 mdialogbehavior = bottomsheetbehavior.from((view) view.getparent());
 mdialogbehavior.setpeekheight(getwindowheight());
 mdialogbehavior.setbottomsheetcallback(new bottomsheetbehavior.bottomsheetcallback() {
 @override
 public void onstatechanged(@nonnull view bottomsheet, int newstate) {
 if (newstate == bottomsheetbehavior.state_hidden) {
  bottomsheetdialog.dismiss();
  mdialogbehavior.setstate(bottomsheetbehavior.state_collapsed);
 }
 }

 @override
 public void onslide(@nonnull view bottomsheet, float slideoffset) {
 }
 });
 }

在监听到用户滑动关闭bottomsheetdialog后,我们把bottomsheetbehavior的状态设置为bottomsheetbehavior.state_collapsed,也就是半个打开状态(bottomsheetbehavior.state_expanded为全打开),至此就解决了调用show()方法无法正常打开的问题。同时我们通过设置setpeekheight和bottomsheetdialog的透明主题来实现知乎评论列表的效果。
在values/styles.xml文件中添加透明主题

<style name="dialog" parent="@android:style/theme.dialog">
 <item name="android:windowframe">@null</item>
 <item name="android:windowisfloating">true</item>
 <item name="android:windowistranslucent">true</item>
 <item name="android:windownotitle">true</item>
 <item name="android:background">@android:color/transparent</item>
 <item name="android:windowbackground">@android:color/transparent</item>
 <item name="android:backgrounddimenabled">true</item>
 <item name="android:backgrounddimamount">0.6</item>
 </style>

最后附上activity界面完整代码如下:

public class bottomsheetdialogactivity extends appcompatactivity implements view.onclicklistener {
 private button bt_start;
 private imageview iv_dialog_close;
 private recyclerview rv_dialog_lists;
 private bottomsheetadapter bottomsheetadapter;
 private bottomsheetdialog bottomsheetdialog;
 private bottomsheetbehavior mdialogbehavior;
 private list<string> list_strs;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_bottom_sheet_dialog);

 list_strs = new arraylist<>();
 initview();
 showsheetdialog();
 }

 private void initview() {
 bt_start = (button) findviewbyid(r.id.main_bt_start);

 for (int i=0; i<20; i++) {
 list_strs.add("评论" + i);
 }

 bt_start.setonclicklistener(this);
 }

 private void showsheetdialog() {
 view view = view.inflate(bottomsheetdialogactivity.this, r.layout.dialog_bottomsheet, null);
 iv_dialog_close = (imageview) view.findviewbyid(r.id.dialog_bottomsheet_iv_close);
 rv_dialog_lists = (recyclerview) view.findviewbyid(r.id.dialog_bottomsheet_rv_lists);

 iv_dialog_close.setonclicklistener(this);

 bottomsheetadapter = new bottomsheetadapter(bottomsheetdialogactivity.this, list_strs);
 rv_dialog_lists.sethasfixedsize(true);
 rv_dialog_lists.setlayoutmanager(new linearlayoutmanager(bottomsheetdialogactivity.this));
 rv_dialog_lists.setitemanimator(new defaultitemanimator());
 rv_dialog_lists.setadapter(bottomsheetadapter);

 bottomsheetdialog = new bottomsheetdialog(bottomsheetdialogactivity.this, r.style.dialog);
 bottomsheetdialog.setcontentview(view);
 mdialogbehavior = bottomsheetbehavior.from((view) view.getparent());
 mdialogbehavior.setpeekheight(getwindowheight());
 mdialogbehavior.setbottomsheetcallback(new bottomsheetbehavior.bottomsheetcallback() {
 @override
 public void onstatechanged(@nonnull view bottomsheet, int newstate) {
 if (newstate == bottomsheetbehavior.state_hidden) {
  bottomsheetdialog.dismiss();
  mdialogbehavior.setstate(bottomsheetbehavior.state_collapsed);
 }
 }

 @override
 public void onslide(@nonnull view bottomsheet, float slideoffset) {
 }
 });
 }

 private int getwindowheight() {
 resources res = bottomsheetdialogactivity.this.getresources();
 displaymetrics displaymetrics = res.getdisplaymetrics();
 return displaymetrics.heightpixels;
 }

 @override
 public void onclick(view v) {
 switch (v.getid()) {
 case r.id.main_bt_start:
 if (bottomsheetdialog != null) {
  bottomsheetdialog.show();
 }
 break;
 case r.id.dialog_bottomsheet_iv_close:
 if (bottomsheetdialog != null) {
  bottomsheetdialog.dismiss();
 }
 break;
 }
 }
}

到此这篇关于android bottomsheetdialog新控件解析实现知乎评论列表效果的文章就介绍到这了,更多相关android 知乎评论列表内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网