<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_200"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e8e8e8"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_float_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="@color/colorPrimary"
android:onClick="designClick"
android:src="@mipmap/ic_launcher"
android:visibility="invisible"
app:borderWidth="0dp"
app:elevation="3dp"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="right|top"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/colorAccent" />
<ImageView
android:id="@+id/iv_float_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="@color/colorPrimary"
android:onClick="designClick"
android:src="@mipmap/ic_launcher"
app:borderWidth="0dp"
app:elevation="3dp"
app:layout_anchor="@id/iv_float_1"
app:layout_anchorGravity="right|top"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/colorAccent" />
<ImageView
android:id="@+id/iv_float_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="@color/colorPrimary"
android:onClick="designClick"
android:src="@mipmap/ic_launcher"
android:visibility="invisible"
app:borderWidth="0dp"
app:elevation="3dp"
app:layout_anchor="@id/iv_float_2"
app:layout_anchorGravity="right|top"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/colorAccent" />
<ImageView
android:id="@+id/iv_float_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="@color/colorPrimary"
android:onClick="designClick"
android:src="@mipmap/ic_launcher"
app:borderWidth="0dp"
app:elevation="3dp"
app:layout_anchor="@id/iv_float_3"
app:layout_anchorGravity="right|top"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/colorAccent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
package com.example.myapplication.Activitys;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.example.myapplication.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import java.util.ArrayList;
import java.util.List;
/**
* BottomSheetBehavior可以轻松实现底部动作条功能,底部动作条的引入需要在布局添加app:layout_behavior属性,
* 并将这个布局作为CoordianatorLayout的子View。这个可以用于一些从下面弹出选项的操作。
* <p>
* 方法 用途
* setPeekHeight 默认显示后View露头的高度
* getPeekHeight @see setPeekHeight()
* setHideable 设置是否可以隐藏,如果为true,表示状态可以为STATE_HIDDEN
* isHideable @see setHideable()
* setState 设置状态;设置不同的状态会影响BottomSheetView的显示效果
* getState 获取状态
* setBottomSheetCallback 设置状态改变回调
*/
public class BottomSheetActivity extends AppCompatActivity {
CoordinatorLayout coordinatorLayout;
RecyclerView recyclerview;
RecyclerView.Adapter<MyViewHolder> adapter;
List<String> texts;
BottomSheetBehavior<View> behavior;
private static final String TAG = "MainActivity";
private View mBottomSheet;
TextView tv_1;
View floatingaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_sheet);
initView();
initData();
initEvent();
}
private void initEvent() {
//设置监听bottomSheet的状态
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
Log.i(TAG, "新状态:" + newState);
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
Log.i(TAG, "拖动操作:" + slideOffset);
}
});
}
private void initData() {
texts = new ArrayList<>();
texts.add("测试1");
texts.add("测试2");
texts.add("测试3");
texts.add("测试4");
texts.add("测试5");
texts.add("测试5");
texts.add("测试5");
//创建适配器
adapter = new MyAdapter();
//初始化recyclerview
recyclerview.setAdapter(adapter);
recyclerview.setHasFixedSize(true);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setLayoutManager(new LinearLayoutManager(this));
//配置bottomSheet
behavior = BottomSheetBehavior.from(mBottomSheet);
}
private void initView() {
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
mBottomSheet = findViewById(R.id.bottom_sheet);
recyclerview = findViewById(R.id.recyclerview);
tv_1 = findViewById(R.id.tv_1);
floatingaction = findViewById(R.id.iv_float_1);
}
//所有点击事件
public void designClick(View view) {
switch (view.getId()) {
case R.id.iv_float_1:
// int state = behavior.getState();
// if (state == BottomSheetBehavior.STATE_EXPANDED) {
// behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
// }else{
// behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
// }
case R.id.iv_float_2:
case R.id.iv_float_3:
case R.id.iv_float_4:
String s = "施工方IEU我一美蜗牛妇儿都快水电费功夫甜热热";
String b = (String) tv_1.getText();
s = "" + s + b;
tv_1.setText(s);
ToastUtils.showLong("" + view.getId());
break;
}
}
//用于展示弹窗的list
public class MyViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> array;
public MyViewHolder(View itemView) {
super(itemView);
array = new SparseArray<>();
}
private <T extends View> T findViewById(int viewId) {
View view = array.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
array.put(viewId, view);
}
return (T) view;
}
private View findView(int viewId) {
return findViewById(viewId);
}
public TextView getTextView(int viewid) {
return (TextView) findView(viewid);
}
}
class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(BottomSheetActivity.this).inflate(R.layout.item, parent, false));
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.getTextView(R.id.text).setText(texts.get(position));
holder.getTextView(R.id.text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(BottomSheetActivity.this, "点击了" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return texts.size();
}
}
;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp">
<TextView
android:id="@+id/text"
android:gravity="center"
android:text="测试"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
</LinearLayout>
plugins {
id 'com.android.library'
}
android {
compileSdkVersion 29
buildToolsVersion "30.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
api 'androidx.appcompat:appcompat:1.2.0'
api 'com.google.android.material:material:1.2.1'
api 'androidx.constraintlayout:constraintlayout:2.0.4'
api 'com.blankj:utilcodex:1.30.5'
api 'com.squareup.okhttp3:okhttp:4.9.0'
api 'org.litepal.guolindev:core:3.2.2'
}