Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話(huà)框效果(實(shí)例代碼)
實(shí)現(xiàn)的效果圖:

自定義Fragment繼承BottomSheetDialogFragment
重寫(xiě)它的三個(gè)方法:
onCreateDialog()
onCreateView()
onStart()
他們的執(zhí)行順序是從上到下
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的實(shí)例
return new BottomSheetDialog(this.getContext());
}
@Override
public void onStart() {
Log.e("TAG", "onStart: ");
super.onStart();
//獲取dialog對(duì)象
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
//把windowsd的默認(rèn)背景顏色去掉,不然圓角顯示不見(jiàn)
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對(duì)象
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
layoutParams.height = getPeekHeight();
//修改彈窗的最大高度,不允許上滑(默認(rèn)可以上滑)
bottomSheet.setLayoutParams(layoutParams);
final BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
//peekHeight即彈窗的最大高度
behavior.setPeekHeight(getPeekHeight());
// 初始為展開(kāi)狀態(tài)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
ImageView mReBack = view.findViewById(R.id.re_back_img);
//設(shè)置監(jiān)聽(tīng)
mReBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//關(guān)閉彈窗
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
}
}
/**
* 彈窗高度,默認(rèn)為屏幕高度的四分之三
* 子類(lèi)可重寫(xiě)該方法返回peekHeight
*
* @return height
*/
protected int getPeekHeight() {
int peekHeight = getResources().getDisplayMetrics().heightPixels;
//設(shè)置彈窗高度為屏幕高度的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);
}
}
}
有以下幾點(diǎn)需要注意:
1.去掉窗口的background,窗口的background默認(rèn)是白色的,如果不處理我們的根部局設(shè)置圓角背景的時(shí)候是沒(méi)有效果的
dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable (new ColorDrawable (Color.TRANSPARENT));
2.固定窗口的高度,窗口默認(rèn)可以向上滑動(dòng)直到鋪滿(mǎn)整個(gè)屏幕RecyclerView才開(kāi)始滑動(dòng)
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
//把windowsd的默認(rèn)背景顏色去掉,不然圓角顯示不見(jiàn)
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對(duì)象
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
layoutParams.height = getPeekHeight();
//修改彈窗的最大高度,不允許上滑(默認(rèn)可以上滑)
bottomSheet.setLayoutParams(layoutParams);
final BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
//peekHeight即彈窗的最大高度
behavior.setPeekHeight(getPeekHeight());
// 初始為展開(kāi)狀態(tài)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
ImageView mReBack = view.findViewById(R.id.re_back_img);
//設(shè)置監(jiān)聽(tīng)
mReBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//關(guān)閉彈窗
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條評(píng)論" android:textSize="15dp" android:textStyle="bold"></TextView> <ImageView android:id="@+id/re_back_img" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp" android:src="@mipmap/back"></ImageView> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/fragment_share_recyclerView" 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:id="@+id/img_recy_item_1_pic" android:layout_width="140dp" android:src="@mipmap/three" android:layout_height="match_parent" android:scaleType="centerCrop" /> </RelativeLayout> </androidx.cardview.widget.CardView>
6.RecyclerView適配器是用BaseRecyclerViewAdapterHelper
Android 中RecyclerView通用適配器的實(shí)現(xiàn)
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. 調(diào)用
Button button = findViewById(R.id.on);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new BaseFullBottomSheetFragment().show(getSupportFragmentManager(), "dialog");
}
});
到此這篇關(guān)于Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話(huà)框效果(實(shí)例代碼)的文章就介紹到這了,更多相關(guān)android 抖音底部彈出對(duì)話(huà)框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RxJava+Retrofit+OkHttp實(shí)現(xiàn)文件上傳
本篇文章主要介紹了RxJava+Retrofit+OkHttp實(shí)現(xiàn)文件上傳,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android入門(mén)之ListView應(yīng)用解析(二)
這篇文章主要介紹了Android入門(mén)之ListView應(yīng)用,繼上一篇之后將對(duì)Android的ListView用法做更深入的剖析,需要的朋友可以參考下2014-08-08
Android Bluetooth藍(lán)牙技術(shù)初體驗(yàn)
這篇文章主要介紹了Android Bluetooth藍(lán)牙技術(shù)初體驗(yàn)的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android的OkHttp包中的HTTP攔截器Interceptor用法示例
攔截器是OkHttp處理HTTP請(qǐng)求方面所具有的一個(gè)強(qiáng)大特性,這里我們就來(lái)看一下Android的OkHttp包中的HTTP攔截器Interceptor用法示例,需要的朋友可以參考下2016-07-07
Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
本篇文章主要介紹了Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
淺談Android開(kāi)發(fā)中ListView控件性能的一些優(yōu)化方法
這篇文章主要介紹了Android開(kāi)發(fā)中ListView控件性能的一些優(yōu)化方法,需要的朋友可以參考下2016-01-01
詳解Android通過(guò)修改配置文件設(shè)置wifi密碼
這篇文章主要介紹了詳解Android通過(guò)修改配置文件設(shè)置wifi密碼的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android自定義View的實(shí)現(xiàn)方法實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹了Android自定義View的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09

