PopupWindow、dialog,DialogFragment。
比如说需求:
只拦截自身所占空间部分的事件,其余空间的点击事件不处理
可以根据改变View的布局排列方式,View是否设置底部背景及居中方式
虽然在功能上 PopupWindow 更符合需要,dialog也能做到,但是使用 DialogFragment 代码更简洁、更方便封装功能模块。
基于Fragment的DialogFragment,便于自定义UI,在生命周期上也优于其他两个,性能上也是。
从代码的编写角度看,Dialog使用起来要更为简单
Android 官方推荐使用 DialogFragment 来代替 Dialog ,可以让它具有更高的可复用性(降低耦合)和更好的便利性(很好的处理屏幕翻转的情况)。
DialogFragment果然有一个非常好的特性(在手机配置变化,导致Activity需要重新创建时,例如旋屏,基于DialogFragment的对话框将会由FragmentManager自动重建,然而基于Dialog实现的对话框则没有这样的能力)。
创建:创建 DialogFragment 有两种方式:
覆写其 onCreateDialog 方法
*应用场景*:一般用于创建替代传统的 Dialog 对话框的场景,UI 简单,功能单一。
覆写其 onCreateView 方法
*应用场景*:一般用于创建复杂内容弹窗或全屏展示效果的场景,UI 复杂,功能复杂,一般有网络请求等异步操作。
示例:
public class HotKeyDialogFragment extends DialogFragment implements Constants, View.OnFocusChangeListener, View.OnClickListener {
public static final String TAG =HotKeyDialogFragment.class.getSimpleName();
private View mView =null;
private ImageView mIvSearch =null;
private ImageView mIvCollection =null;
private ImageView mIvHistory =null;
private ImageView mIvAbout =null;
private SimpleDraweeView mImageView =null;
private int mCurrentPosition = -1;
public static HotKeyDialogFragment newInstance() {
HotKeyDialogFragment dialog =new HotKeyDialogFragment();
return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.hot_key_fragment_dialog, null);
setLocation();
initData();
mIvSearch =mView.findViewById(R.id.iv_search);
mIvSearch.setFocusable(true);
mIvSearch.requestFocus();
if (mIvSearch.hasFocus()) {
mIvSearch.setImageResource(R.mipmap.bottom_popup_search_sel);
}
mIvCollection =mView.findViewById(R.id.iv_collection);
mIvHistory =mView.findViewById(R.id.iv_history);
mIvAbout =mView.findViewById(R.id.iv_about);
mImageView =mView.findViewById(R.id.iv_app_er_code);
mImageView.setAspectRatio(1);
mImageView.setImageResource(R.mipmap.er_code_image);
setListener();
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void setListener() {
mIvSearch.setOnFocusChangeListener(this);
mIvCollection.setOnFocusChangeListener(this);
mIvHistory.setOnFocusChangeListener(this);
mIvAbout.setOnFocusChangeListener(this);
mIvSearch.setOnClickListener(this);
mIvCollection.setOnClickListener(this);
mIvHistory.setOnClickListener(this);
mIvAbout.setOnClickListener(this);
}
private void setLocation() {
Window window =getDialog().getWindow();
window.setGravity(Gravity.CENTER_HORIZONTAL |Gravity.BOTTOM);
window.getAttributes().windowAnimations =R.style.dialogAnim;
}
@Override
public void show(FragmentManager manager, String tag) {
try {
manager.beginTransaction().remove(this).commit();
super.show(manager, tag);
}catch (IllegalStateException ignore) {
}
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
switch (view.getId()) {
case R.id.iv_search:
if (hasFocus) {
mIvSearch.setImageResource(R.mipmap.bottom_popup_search_sel);
}else {
mIvSearch.setImageResource(R.mipmap.bottom_popup_search);
}
break;
case R.id.iv_collection:
if (hasFocus) {
mIvCollection.setImageResource(R.mipmap.bottom_popup_collection_sel);
}else {
mIvCollection.setImageResource(R.mipmap.bottom_popup_collection);
}
break;
case R.id.iv_history:
if (hasFocus) {
mIvHistory.setImageResource(R.mipmap.bottom_popup_history_sel);
}else {
mIvHistory.setImageResource(R.mipmap.bottom_popup_history);
}
break;
case R.id.iv_about:
if (hasFocus) {
mIvAbout.setImageResource(R.mipmap.bottom_popup_about_sel);
}else {
mIvAbout.setImageResource(R.mipmap.bottom_popup_about);
}
break;
default:
break;
}
}
@Override
public void onResume() {
super.onResume();
switch (mCurrentPosition){
case 0:
mIvSearch.setFocusable(true);
mIvSearch.requestFocus();
if (mIvSearch.hasFocus()) {
mIvSearch.setImageResource(R.mipmap.bottom_popup_search_sel);
}
break;
case 1:
mIvCollection.setFocusable(true);
mIvCollection.requestFocus();
if (mIvCollection.hasFocus()) {
mIvCollection.setImageResource(R.mipmap.bottom_popup_collection_sel);
}
break;
case 2:
mIvHistory.setFocusable(true);
mIvHistory.requestFocus();
if (mIvHistory.hasFocus()) {
mIvHistory.setImageResource(R.mipmap.bottom_popup_history_sel);
}
break;
case 3:
mIvAbout.setFocusable(true);
mIvAbout.requestFocus();
if (mIvAbout.hasFocus()) {
mIvAbout.setImageResource(R.mipmap.bottom_popup_about_sel);
}
break;
default:
break;
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.iv_search:
mCurrentPosition=0;
if (getActivity()instanceof SearchActivity) {
this.dismiss();
return;
}else {
if (getActivity()instanceof MainActivity||getActivity()instanceof BookSecondActivity){
Intent intent =new Intent(getActivity(), SearchActivity.class);
startActivity(intent);
}else {
getActivity().finish();
Intent intent =new Intent(getActivity(), SearchActivity.class);
startActivity(intent);
}
}
break;
case R.id.iv_collection:
mCurrentPosition=1;
if (getActivity()instanceof CollectionActivity) {
this.dismiss();
return;
}else {
if (getActivity()instanceof MainActivity||getActivity()instanceof BookSecondActivity){
Intent intent =new Intent(getActivity(), CollectionActivity.class);
startActivity(intent);
}else {
getActivity().finish();
Intent intent =new Intent(getActivity(), CollectionActivity.class);
startActivity(intent);
}
}
break;
case R.id.iv_history:
mCurrentPosition=2;
if (getActivity()instanceof HistoryActivity) {
this.dismiss();
return;
}else {
if (getActivity()instanceof MainActivity||getActivity()instanceof BookSecondActivity){
Intent intent =new Intent(getActivity(), HistoryActivity.class);
startActivity(intent);
}else {
getActivity().finish();
Intent intent =new Intent(getActivity(), HistoryActivity.class);
startActivity(intent);
}
}
break;
case R.id.iv_about:
mCurrentPosition=3;
if (getActivity()instanceof AboutActivity) {
this.dismiss();
return;
}else {
if (getActivity()instanceof MainActivity||getActivity()instanceof BookSecondActivity){
Intent intent =new Intent(getActivity(), AboutActivity.class);
startActivity(intent);
}else {
getActivity().finish();
Intent intent =new Intent(getActivity(), AboutActivity.class);
startActivity(intent);
}
}
break;
default:
break;
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
以上示例是我在项目里面的一个小功能应用在TV端的,需要注意的是DialogFragment在show的时候,最好是自已重写show方法,因为DialogFrament是继承自Fragment的,他本身的show方法其实是调用了Fragment的commit方法,这样在重复打开的时候容易有问题,
@Override
public void show(FragmentManager manager, String tag) {
try {
manager.beginTransaction().remove(this).commit();
super.show(manager, tag);
}catch (IllegalStateException ignore) {
}
}
这样重写show方法每次show的时候把前一次commit移除掉,有效的避免了重复commit引起的bug.另外还需要注意dialogFrament设置大小和位置的办法,如果有具体的背景图就不需要在代码里面手动设置大小,在xml文件里面设置wrap_content就可以,但是位置需要通过window手动设置,弹框动画也是直接设置window的stytle去设置。
以上是我个人使用的一点小总结,谢谢浏览。
网友评论