最终实现效果
带黑色透明背景
不带黑色透明背景
核心代码
俩种style代表2种样式的弹框
<style name="dialog2" parent="AppTheme">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/transparent</item>
<!--显示区域以外是否使用黑色半透明背景-->
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="dialog" parent="AppTheme">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:backgroundDimAmount">0.5</item>
<!--显示区域以外是否使用黑色半透明背景-->
<item name="android:backgroundDimEnabled">true</item>
</style>
公共的DialogFragment
public abstract class CommonDialogFragment extends DialogFragment {
protected Context mContext;
protected View mRootView;
private LifeCycleListener mLifeCycleListener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
mContext = new WeakReference<>(getActivity()).get();
mRootView = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
Dialog dialog = new Dialog(mContext, getDialogStyle());
dialog.setContentView(mRootView);
dialog.setCancelable(canCancel());
dialog.setCanceledOnTouchOutside(canCancel());
setWindowAttributes(dialog.getWindow());
return dialog;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (mLifeCycleListener != null) {
mLifeCycleListener.onDialogFragmentShow(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mLifeCycleListener != null) {
mLifeCycleListener.onDialogFragmentHide(this);
}
mLifeCycleListener = null;
}
protected abstract int getLayoutId();
protected abstract int getDialogStyle();
protected abstract boolean canCancel();
protected abstract void setWindowAttributes(Window window);
protected <T extends View> T findViewById(int id) {
if (mRootView != null) {
return mRootView.findViewById(id);
}
return null;
}
protected boolean canClick() {
return ClickUtil.canClick();
}
public void setLifeCycleListener(LifeCycleListener lifeCycleListener) {
mLifeCycleListener = lifeCycleListener;
}
public interface LifeCycleListener {
void onDialogFragmentShow(CommonDialogFragment fragment);
void onDialogFragmentHide(CommonDialogFragment fragment);
}
}
public class UserDialogFragment extends CommonDialogFragment {
private ArrayList<UserBean> dataList = new ArrayList<>();
private LiveUserAdapter adapter;
@Override
protected int getLayoutId() {
return R.layout.dialog_common;
}
@Override
protected int getDialogStyle() {
return R.style.dialog2;
}
@Override
protected boolean canCancel() {
return true;
}
@Override
protected void setWindowAttributes(Window window) {
window.setWindowAnimations(R.style.bottomToTopAnim);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM;
// params.y = 50;
window.setAttributes(params);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.user_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
adapter = new LiveUserAdapter(mContext);
adapter.setOnItemClickListener(new LiveUserAdapter.OnItemClickListener() {
@Override
public void onItemClick(UserBean bean, int position) {
dismiss();
String name = bean.getName();
Toast.makeText(mContext, "点击了" + name, Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
iniData();
}
private void iniData() {
UserBean userBean1 = new UserBean("1", "用户1", "10", R.mipmap.touxiang1);
UserBean userBean2 = new UserBean("2", "用户2", "11", R.mipmap.touxiang2);
UserBean userBean3 = new UserBean("3", "用户3", "12", R.mipmap.touxiang3);
UserBean userBean4 = new UserBean("4", "用户4", "13", R.mipmap.touxiang4);
dataList.add(userBean1);
dataList.add(userBean2);
dataList.add(userBean3);
dataList.add(userBean4);
adapter.refreshList(dataList);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
public class MainActivity extends AppCompatActivity implements CommonDialogFragment.LifeCycleListener {
private HashSet<DialogFragment> mDialogFragmentSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDialogFragmentSet = new HashSet<>();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCommonDialog();
}
});
}
private void showCommonDialog() {
UserDialogFragment fragment = new UserDialogFragment() ;
fragment.setLifeCycleListener(this);
fragment.show(getSupportFragmentManager(),"UserDialogFragment");
}
@Override
public void onDialogFragmentShow(CommonDialogFragment dialogFragment) {
if (mDialogFragmentSet != null && dialogFragment != null) {
mDialogFragmentSet.add(dialogFragment);
}
}
@Override
public void onDialogFragmentHide(CommonDialogFragment fragment) {
if (mDialogFragmentSet != null) {
for (DialogFragment dialogFragment : mDialogFragmentSet) {
if (dialogFragment != null) {
dialogFragment.dismissAllowingStateLoss();
}
}
}
}
}
注意:关闭dialogfragment推荐使用dismissAllowingStateLoss()
本文地址:https://blog.csdn.net/jingerlovexiaojie/article/details/107384617
如对本文有疑问, 点击进行留言回复!!
LongClick原理、上下文菜单原理、EditText长按弹窗原理、WebView长按弹窗自定义、修复WebView全选重复bug ———————————————— 版权声明:本文为CSDN博主「
JobScheduler 实现 特定时间,特定条件(系统空闲,电池电量,磁盘空间 ……)下执行任务
温习Android基础知识——《第一行代码(第三版)》读书笔记 Chapter 10 Service
网友评论