使用BottomSheetDialogFragment实现购买出商品,底部弹出商品属性的效果

BottomSheetDialogFragment继承于BottomSheetDialog,一个Dialog形式的framgnet,可实现拖动打开及关闭,通过查看源码,发现在其内部是创建了一个BottomSheetDialog的,而BottomSheetDialog则继承于DialogFragment。归根结底就是一个Dialog,只是在其基础上进行了加强。费话不多说,先上图:

要实现这个效果,需要重写三个方法:
1、重写onCreateDialog()

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        //返回BottomSheetDialog的实例
        return new BottomSheetDialog(getContext());
    }


2、重写onCreateView()

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup   container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_goods_sku_layout, container, false);
        initView(view);
        return view;
    }


3、重写onStart()

    @Override
    public void onStart() {
        super.onStart();
        //获取dialog对象
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        //把windowsd的默认背景颜色去掉,不然圆角显示不见
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);
        //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);
                }
            });*/
        }
    }

XML布局源码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/dialog_top_layout"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:elevation="1dp"
        tools:ignore="UnusedAttribute">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_alignParentBottom="true"
            android:background="@color/colorWhite"
            android:paddingLeft="120dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="vertical"
                android:paddingBottom="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="商品名称"
                    android:textColor="#3F51B5"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/dialog_tv_total_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¥88.88"
                    android:textColor="#FF5722"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText,TextViewEdits" />


                <TextView
                    android:id="@+id/dialog_tv_goods_no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="商品编码:888888"
                    android:textColor="#999999"
                    android:visibility="visible"
                    tools:ignore="HardcodedText" />

            </LinearLayout>


        </FrameLayout>

        <androidx.cardview.widget.CardView
            android:id="@+id/dialog_cardView"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="14dp"
            android:layout_marginBottom="5dp"
            app:cardBackgroundColor="@color/colorWhite"
            app:cardCornerRadius="6dp"
            app:cardElevation="2dp">

            <ImageView
                android:id="@+id/dialog_iv_goods"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                tools:ignore="ContentDescription" />

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/bottomSheetDialog_NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_top_layout"
        android:background="@color/colorWhite">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="14dp"
            android:layout_marginBottom="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:text="其它内容填充区域"
                android:textSize="24sp"
                android:gravity="center"
                android:background="#C6DEE3" />
        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/dialog_bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/bottomSheetDialog_NestedScrollView"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/dialog_add_shopping_sku"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:alpha="0.8"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:text="加入购物车"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            tools:ignore="HardcodedText" />

        <TextView
            android:id="@+id/dialog_orders_sku"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="立即购买"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            tools:ignore="HardcodedText" />
    </LinearLayout>

</RelativeLayout>

源码详见:AndroidUI仿商城APP中的购买商品时,从底部弹出的窗口展示商品属性_android凸出-Android文档类资源-CSDN下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金戈鐡馬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值