老套路上图:
底部菜单滑出效果如上图:
首先依赖三方库
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
再看主页面的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cl_chouti"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="测试数据" />
</FrameLayout>
<!--behavior_hideable布局是否可以哦隐藏-->
<RelativeLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:behavior_hideable="false"
app:behavior_peekHeight="160dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<include layout="@layout/layout_bottom_sheet" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
include文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:text="附近热点"
android:textSize="10sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/red" />
</LinearLayout>
在看下使用方法
mainactivity
package cn.xiayiye5.xiayiye5library.activity;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import cn.xiayiye5.xiayiye5library.R;
/**
* @author : xiayiye5
* @date : 2021/3/15 09:59
* 类描述 :
*/
public class BottomScrollerOutActivity extends BaseActivity {
@Override
protected int getLayoutView() {
return R.layout.activity_scroller_out;
}
@Override
protected void initId() {
//底部抽屉栏展示地址
RelativeLayout bottomSheet = findViewById(R.id.bottom_sheet);
BottomSheetBehavior<RelativeLayout> behavior = BottomSheetBehavior.from(bottomSheet);
//setBottomSheetCallback已过时
behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, @BottomSheetBehavior.State int newState) {
String state = "null";
switch (newState) {
case 1:
state = "STATE_DRAGGING";//过渡状态此时用户正在向上或者向下拖动bottom sheet
break;
case 2:
state = "STATE_SETTLING"; // 视图从脱离手指自由滑动到最终停下的这一小段时间
break;
case 3:
state = "STATE_EXPANDED"; //处于完全展开的状态
break;
case 4:
state = "STATE_COLLAPSED"; //默认的折叠状态
break;
case 5:
state = "STATE_HIDDEN"; //下滑动完全隐藏 bottom sheet
break;
default:
break;
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
Log.d("BottomSheetDemo", "slideOffset:" + slideOffset);
}
});
}
@Override
protected void loadData() {
}
}
特别简单吧:源码直达
应用到产品中的效果是这样的