电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)

瀏覽:131日期:2022-06-07 14:14:07

實現的效果圖:

Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)

自定義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: android:layout_width='25dp' android:layout_height='25dp' android:layout_alignParentRight='true' android:layout_centerVertical='true' android:layout_marginRight='20dp' android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/back'></ImageView> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android: 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: android:layout_width='140dp' android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/three' android:layout_height='match_parent' android:scaleType='centerCrop' /> </RelativeLayout></androidx.cardview.widget.CardView>

6.RecyclerView適配器是用BaseRecyclerViewAdapterHelperAndroid 中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() {@Overridepublic void onClick(View view) { new BaseFullBottomSheetFragment().show(getSupportFragmentManager(), 'dialog'); } });

到此這篇關于Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)的文章就介紹到這了,更多相關android 抖音底部彈出對話框內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 抖音
相關文章:
主站蜘蛛池模板: 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 企小优-企业数字化转型服务商_网络推广_网络推广公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 春腾云财 - 为企业提供专业财税咨询、代理记账服务 | 棕刚玉_白刚玉_铝酸钙-锐石新材料 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | app开发|app开发公司|小程序开发|物联网开发||北京网站制作|--前潮网络 | 软文世界-软文推广-软文营销-新闻稿发布-一站式软文自助发稿平台 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 自恢复保险丝_贴片保险丝_力特保险丝_Littelfuse_可恢复保险丝供应商-秦晋电子 | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 计算机毕业设计源码网| 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 广州小程序开发_APP开发公司_分销商城系统定制_小跑科技 | 连续密炼机_双转子连续密炼机_连续式密炼机-南京永睿机械制造有限公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 盘古网络技术有限公司| LCD3D打印机|教育|桌面|光固化|FDM3D打印机|3D打印设备-广州造维科技有限公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! |