Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
程序员文章站
2023-11-09 19:58:52
实现的效果图:自定义fragment继承bottomsheetdialogfragment重写它的三个方法:oncreatedialog()oncreateview()onstart()他们的执行顺序...
实现的效果图:
自定义fragment继承bottomsheetdialogfragment
重写它的三个方法:
oncreatedialog()
oncreateview()
onstart()
他们的执行顺序是从上到下
import android.app.dialog; import android.content.context; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.framelayout; import android.widget.imageview; import androidx.annotation.nonnull; import androidx.annotation.nullable; import androidx.coordinatorlayout.widget.coordinatorlayout; import androidx.recyclerview.widget.gridlayoutmanager; import androidx.recyclerview.widget.recyclerview; import com.google.android.material.bottomsheet.bottomsheetbehavior; import com.google.android.material.bottomsheet.bottomsheetdialog; import com.google.android.material.bottomsheet.bottomsheetdialogfragment; import java.util.arraylist; import java.util.list; /** * @author: david.lvfujiang * @date: 2019/11/14 * @describe: */ public class basefullbottomsheetfragment extends bottomsheetdialogfragment { private list<shareitem> msharelist = new arraylist<>(); private int[] imgarry= {r.mipmap.five,r.mipmap.four,r.mipmap.one,r.mipmap.three}; private context mcontext; private view view; public static basefullbottomsheetfragment getinstance() { return new basefullbottomsheetfragment(); } @nonnull @override public dialog oncreatedialog(@nullable bundle savedinstancestate) { log.e("tag", "oncreatedialog: "); //返回bottomsheetdialog的实例 return new bottomsheetdialog(this.getcontext()); } @override public void onstart() { log.e("tag", "onstart: "); super.onstart(); //获取dialog对象 bottomsheetdialog dialog = (bottomsheetdialog) getdialog(); //把windowsd的默认背景颜色去掉,不然圆角显示不见 dialog.getwindow().findviewbyid(r.id.design_bottom_sheet).setbackgrounddrawable(new colordrawable(color.transparent)); //获取diglog的根部局 framelayout bottomsheet = dialog.getdelegate().findviewbyid(r.id.design_bottom_sheet); if (bottomsheet != null) { //获取根部局的layoutparams对象 coordinatorlayout.layoutparams layoutparams = (coordinatorlayout.layoutparams) bottomsheet.getlayoutparams(); layoutparams.height = getpeekheight(); //修改弹窗的最大高度,不允许上滑(默认可以上滑) bottomsheet.setlayoutparams(layoutparams); final bottomsheetbehavior<framelayout> behavior = bottomsheetbehavior.from(bottomsheet); //peekheight即弹窗的最大高度 behavior.setpeekheight(getpeekheight()); // 初始为展开状态 behavior.setstate(bottomsheetbehavior.state_expanded); imageview mreback = view.findviewbyid(r.id.re_back_img); //设置监听 mreback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //关闭弹窗 behavior.setstate(bottomsheetbehavior.state_hidden); } }); } } /** * 弹窗高度,默认为屏幕高度的四分之三 * 子类可重写该方法返回peekheight * * @return height */ protected int getpeekheight() { int peekheight = getresources().getdisplaymetrics().heightpixels; //设置弹窗高度为屏幕高度的3/4 return peekheight - peekheight / 3; } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { mcontext = getcontext(); log.e("tag", "oncreateview: "); view = inflater.inflate(r.layout.layoyt_bottomsheet, container, false); initdata(); initviews(view); return view; } private void initviews(view view) { recyclerview recyclerview = view.findviewbyid(r.id.fragment_share_recyclerview); recyclerview.setlayoutmanager(new gridlayoutmanager(mcontext, 3)); recyclercommonadapter adapter = new recyclercommonadapter(r.layout.recyclear_item, msharelist); recyclerview.setadapter(adapter); } private void initdata() { for (int i = 0; i < 30; i++) { shareitem item = new shareitem(); item.seticon(imgarry[i%4]); msharelist.add(item); } } }
有以下几点需要注意:
1.去掉窗口的background,窗口的background默认是白色的,如果不处理我们的根部局设置圆角背景的时候是没有效果的
dialog.getwindow().findviewbyid(r.id.design_bottom_sheet).setbackgrounddrawable (new colordrawable (color.transparent));
2.固定窗口的高度,窗口默认可以向上滑动直到铺满整个屏幕recyclerview才开始滑动
bottomsheetdialog dialog = (bottomsheetdialog) getdialog(); //把windowsd的默认背景颜色去掉,不然圆角显示不见 dialog.getwindow().findviewbyid(r.id.design_bottom_sheet).setbackgrounddrawable(new colordrawable(color.transparent)); //获取diglog的根部局 framelayout bottomsheet = dialog.getdelegate().findviewbyid(r.id.design_bottom_sheet); if (bottomsheet != null) { //获取根部局的layoutparams对象 coordinatorlayout.layoutparams layoutparams = (coordinatorlayout.layoutparams) bottomsheet.getlayoutparams(); layoutparams.height = getpeekheight(); //修改弹窗的最大高度,不允许上滑(默认可以上滑) bottomsheet.setlayoutparams(layoutparams); final bottomsheetbehavior<framelayout> behavior = bottomsheetbehavior.from(bottomsheet); //peekheight即弹窗的最大高度 behavior.setpeekheight(getpeekheight()); // 初始为展开状态 behavior.setstate(bottomsheetbehavior.state_expanded); imageview mreback = view.findviewbyid(r.id.re_back_img); //设置监听 mreback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //关闭弹窗 behavior.setstate(bottomsheetbehavior.state_hidden); } }); }
3.fragment加载的布局文件
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/leave_message_radiobutton_background" android:orientation="vertical"> <relativelayout android:layout_width="match_parent" android:layout_height="@dimen/dp_40"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="100条评论" android:textsize="15dp" android:textstyle="bold"></textview> <imageview android:id="@+id/re_back_img" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="20dp" android:src="@mipmap/back"></imageview> </relativelayout> <androidx.recyclerview.widget.recyclerview android:id="@+id/fragment_share_recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="8dp" android:layout_marginbottom="8dp" /> </linearlayout>
4.fragment布局的圆角背景
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 填充 --> <solid android:color="#ffffff" /> <!-- 圆角 --> <corners android:radius="15dp" /> </shape>
5.recyclerview的item布局
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="140dp" android:layout_margin="8dp" app:cardcornerradius="8dp" app:cardelevation="4dp"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <imageview android:id="@+id/img_recy_item_1_pic" android:layout_width="140dp" android:src="@mipmap/three" android:layout_height="match_parent" android:scaletype="centercrop" /> </relativelayout> </androidx.cardview.widget.cardview>
6.recyclerview适配器是用baserecyclerviewadapterhelper
android 中recyclerview通用适配器的实现
package com.example.bottomsheetdialogapplication; import androidx.annotation.nullable; import com.chad.library.adapter.base.basequickadapter; import com.chad.library.adapter.base.baseviewholder; import java.util.list; import java.util.map; /** * @author: david.lvfujiang * @date: 2019/10/30 * @describe: */ public class recyclercommonadapter extends basequickadapter<shareitem, baseviewholder> { public recyclercommonadapter(int layoutresid, @nullable list<shareitem> data) { super(layoutresid, data); } @override protected void convert(baseviewholder helper, shareitem item) { helper.setimageresource(r.id.img_recy_item_1_pic, item.geticon()); helper.addonclicklistener(r.id.img_recy_item_1_pic); } }
7. 调用
button button = findviewbyid(r.id.on); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new basefullbottomsheetfragment().show(getsupportfragmentmanager(), "dialog"); } });
到此这篇关于android 之bottomsheetdialogfragment仿抖音评论底部弹出对话框效果(实例代码)的文章就介绍到这了,更多相关android 抖音底部弹出对话框内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!