没有好用的底部抽屉?还不速度自己写一个

本文介绍了一种通过FrameLayout和自定义View实现Android底部抽屉效果的方法,包括弹出和收回动画,并提供了具体的XML布局代码及自定义View的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,先贴出效果图
这里写图片描述

看到这里,肯定有人会挠着头问:“那个…你有没有….那种….?”
答案肯定是有的啦,系好安全带,黑车司机准备开车。

具体思路

思路很简单:
其实就是巧用布局,把父布局设置成FrameLayout,这样一来,后面的布局就会盖住前面的。
把底部抽屉放在最低端,刚进入界面把它滑到底端,留出一个把手。点击把手,弹出抽屉,基本上抽屉就ok了

搭建界面

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorLightDark"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_question"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="46dp" />

    //这个imageView 其实是就是底部抽屉弹出后界面的一个透明灰色背景
    <ImageView
        android:id="@+id/iv_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    //这个就是我的底部抽屉(里面包裹的内容可以自己定义)
    <com.bdrk.onlineexams.widgets.ScrollBottomLayout
        android:id="@+id/ll_drawer"
        android:layout_width="match_parent"
        android:layout_height="346dp"
        android:layout_gravity="bottom"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/rl_handle"
            android:layout_width="match_parent"
            android:layout_height="46dp"
            android:layout_gravity="bottom"
            android:background="@color/colorWhite"
            android:clickable="true"
            android:gravity="center_vertical">

            //这个是底部的把手,点击弹出抽屉
            <LinearLayout
                android:id="@+id/ll_collect"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/iv_collect"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:src="@mipmap/collected" />

                <TextView
                    android:id="@+id/tv_collect"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="4dp"
                    android:gravity="center"
                    android:text="收藏"
                    android:textColor="@color/colorTextDark"
                    android:textSize="12sp" />
            </LinearLayout>


        <TextView
            android:id="@+id/tv_tip"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:background="@color/colorDark"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:text="第一章:道路交通安全法律"
            android:textColor="@color/colorTextDark"
            android:textSize="14sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/re_test_number"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorWhite" />

    </com.bdrk.onlineexams.widgets.ScrollBottomLayout>

</FrameLayout>

我这里弹出效果是用view的scroll方法,但是该方法没有那种弹出的效果,于是我就百度出了这个:

public class ScrollBottomLayout extends LinearLayout {
    private Scroller mScroller;

    public ScrollBottomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    //调用此方法滚动到目标位置
    public void smoothScrollTo(int fx, int fy, int duration) {
        int dx = fx - mScroller.getFinalX();
        int dy = fy - mScroller.getFinalY();
        smoothScrollBy(dx, dy, duration);
    }

    //调用此方法设置滚动的相对偏移
    public void smoothScrollBy(int dx, int dy, int duration) {
        //设置mScroller的滚动偏移量
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration);
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
    }

    @Override
    public void computeScroll() {

        //先判断mScroller滚动是否完成
        if (mScroller.computeScrollOffset()) {

            //这里调用View的scrollTo()完成实际的滚动
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

            //必须调用该方法,否则不一定能看到滚动效果
            postInvalidate();
        }
        super.computeScroll();
    }
}

这样一来,弹入弹出效果也实现了

使用

这里贴出我的使用代码:

onCreate()的时候设置它滑到底部,留出把手。

bottomScoll = SizeUtils.dp2px(PersonalPracticeActivity.this, 300);
        llDrawer.smoothScrollTo(0, -bottomScoll, 0);
图片设置为半透明黑色
ivBg.setBackgroundColor(Color.argb(100, 0, 0, 0));

开启抽屉

private void openDrawer() {
        llDrawer.smoothScrollTo(0, 0, 800);
        ivBg.setVisibility(View.VISIBLE);
        isOpen = true;
    }

关闭抽屉

private void closeDrawer() {
        llDrawer.smoothScrollTo(0, -bottomScoll, 800);
        ivBg.setVisibility(View.GONE);
        isOpen = false;
    }

本来写完这个是打算加一个滑动开启跟滑动关闭的功能的,奈何太菜,对view事件处理不是特别熟悉,唉只有以后再填坑了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值