老规矩 先上效果图
附下Demo TikTok
如果想要实现抖音一样的评论功能,就要再列表dialog上面再弹出一个dialog.
那么问题来了:当评论的dialog弹出来的时候两个dialog就会被顶起,但是我们想要的是评论列表不要被顶起
给评论列表的dialog设置此方法即可
Window win = dialog.getWindow();
WindowManager.LayoutParams params = win.getAttributes();
win.setSoftInputMode(params.SOFT_INPUT_ADJUST_NOTHING);
添加依赖 版本根据你build版本来
implementation 'com.android.support:design:27.1.1'
自定义类:ListBottomSheetDialogFragment继承BottomSheetDialogFragment
/**
* @Author: 淘跑
* @Date: 2018/7/2 15:18
* @Use:
*/
public class ListBottomSheetDialogFragment extends BottomSheetDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//给dialog设置主题为透明背景 不然会有默认的白色背景
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomBottomSheetDialogTheme);
}
/**
* 如果想要点击外部消失的话 重写此方法
*
* @param savedInstanceState
* @return
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
//设置点击外部可消失
dialog.setCanceledOnTouchOutside(true);
//设置使软键盘弹出的时候dialog不会被顶起
Window win = dialog.getWindow();
WindowManager.LayoutParams params = win.getAttributes();
win.setSoftInputMode(params.SOFT_INPUT_ADJUST_NOTHING);
//这里设置dialog的进出动画
win.setWindowAnimations(R.style.DialogBottomAnim);
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 在这里将view的高度设置为精确高度,即可屏蔽向上滑动不占全屏的手势。
//如果不设置高度的话 会默认向上滑动时dialog覆盖全屏
View view = inflater.inflate(R.layout.fragment_item_list_dialog, container, false);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getScreenHeight(getActivity()) / 2));
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new ItemAdapter(100));
view.findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//弹出评论输入框
InputDialog inputDialog = new InputDialog(getActivity());
Window window = inputDialog.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//设置软键盘通常是可见的
window.setSoftInputMode(params.SOFT_INPUT_STATE_VISIBLE);
inputDialog.show();
}
});
}
private class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> {
private final int mItemCount;
ItemAdapter(int itemCount) {
mItemCount = itemCount;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.text.setText(String.valueOf(position));
}
@Override
public int getItemCount() {
return mItemCount;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
final TextView text;
MyViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.fragment_item_list_dialog_item, parent, false));
text = (TextView) itemView.findViewById(R.id.text);
}
}
}
/**
* 得到屏幕的高
*
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int height = wm.getDefaultDisplay().getHeight();
return height;
}
}
自定义布局文件 fragment_item_list_dialog
<?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="wrap_content"
android:background="@drawable/shape_round_white">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#454545"
android:gravity="center"
android:text="假装这是输入框"
android:textColor="#FFFFFF"
android:textSize="25dp" />
</LinearLayout>
item布局文件 fragment_item_list_dialog_item
<?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">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textColor="@android:color/black" />
</LinearLayout>
自定义样式style: CustomBottomSheetDialogTheme这里设置的是背景也是透明的
<style name="CustomBottomSheetDialogTheme" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item><!--边框-->
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
<item name="android:backgroundDimEnabled">false</item><!--模糊-->
<item name="android:windowSoftInputMode">adjustPan|stateHidden</item>
</style>
动画
<!-- 底部弹出动画 -->
<style name="DialogBottomAnim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_bottom_in</item>
<item name="android:windowExitAnimation">@anim/dialog_bottom_out</item>
</style>
dialog_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromYDelta="100%p" />
</set>
dialog_bottom_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:toYDelta="100%p" />
</set>
评论输入Dialog
/**
* @Author:淘跑
* @Date: 2018/11/14 17:20
* @Use:
*/
public class InputDialog extends Dialog {
public InputDialog(@NonNull Context context) {
super(context);
// super(context, R.style.CustomDialog);
init(context);
}
public Context mContext;
public View mRootView;
public void init(Context context) {
mContext = context;
mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_input, null);
setContentView(mRootView);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.setGravity(Gravity.BOTTOM);
}
}
评论输入Dialog 布局
<?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">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="80dp" />
</LinearLayout>