关于android中的dialogFragment详解

来源:互联网 发布:阿里云 画图 工具 编辑:程序博客网 时间:2024/06/04 00:35

1.首先看一下DialogFragment的类的继承关系图

public class DialogFragment extends Fragment implements OnCancelListener, OnDismissListener {    public static final int STYLE_NORMAL = 0;    public static final int STYLE_NO_TITLE = 1;    public static final int STYLE_NO_FRAME = 2;    public static final int STYLE_NO_INPUT = 3;    private static final String SAVED_DIALOG_STATE_TAG = "android:savedDialogState";    private static final String SAVED_STYLE = "android:style";    private static final String SAVED_THEME = "android:theme";    private static final String SAVED_CANCELABLE = "android:cancelable";    private static final String SAVED_SHOWS_DIALOG = "android:showsDialog";    private static final String SAVED_BACK_STACK_ID = "android:backStackId

DialogFragment是Fragment的子类 ,因此生命周期的方法和Fragment的生命周期相同

2.使用DialogFragment的好处:

在android3.0之后谷歌推荐使用DialogFragment代替fragment,

       好处(1):由于fragment的生命周期便于控制,而且Fragment可以作为一个组件使用

       好处(2):使用DialogFragment的比较灵活,能够满足在一个项目中多次使用相同的对话框

下面会有具体的代码进行阐述。

3.具体的代码实现

由于DialogFragment和fragment类似,我们完全可以按照创建fragment的步骤来实现DialogFragment的创建

       在这个例子中主要是来实现我们整个项目中所有的加载进度的进度调,这个加载的DialogFragment在整个项目中多次被用到,这就是DialogFragment的好处,

一次创建多次被使用

a.先创建具体的xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"     android:background="@drawable/leba_shape_bg"    android:id="@+id/dialog_loding_ll"    android:padding="10dip">    <ImageView        android:id="@+id/loadingImageView"        android:layout_width="60dip"        android:layout_height="60dip"        android:layout_gravity="center_vertical|center_horizontal"        android:background="@anim/publicloading" />    <TextView        android:id="@+id/id_tv_loadingmsg"        android:layout_marginTop="5dip"        android:layout_width="wrap_content"        android:minWidth="140dip"        android:gravity="center_vertical|center_horizontal"        android:layout_height="wrap_content"         android:layout_gravity="center_vertical|center_horizontal"        android:text="正在加载..."        android:textColor="@color/mycenter"        android:textSize="16.0sp" /></LinearLayout>

b.创建具体的DialogFragment,代码中已经做了详细的注释:

      

import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.support.v4.app.DialogFragment;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.ImageView;import android.widget.TextView;import com.mofangge.arena.R;/** * 显示正在加载对话框的类 * Created by wanyan on 2015/9/1. */public class LoadingDialogFragment extends DialogFragment {    private String msg = null;    public LoadingDialogFragment() {    }    public static final LoadingDialogFragment newInstance(String msg) {        LoadingDialogFragment ldf = new LoadingDialogFragment();        //谷歌推荐使用这种方式保存传进来的数据        Bundle bundle = new Bundle();        bundle.putSerializable("msg", msg);        ldf.setArguments(bundle);        return ldf;    }    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置是否可以取消  点击返回键可以让DialogFragment消失        setCancelable(true);        setStyle(R.style.base_loading_dialogfragment, R.style.base_loading_dialogfragment);        Bundle bundle = getArguments();        if (bundle != null) {            msg = bundle.getString("msg");        }    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);        View view = inflater.inflate(R.layout.dialog_loding, null);        ImageView imageView = (ImageView) view.findViewById(R.id.loadingImageView);        showAnim(imageView);        TextView tv_msg = (TextView) view.findViewById(R.id.id_tv_loadingmsg);//显示文字  正在加载。。        //判断msg是否为空        if (TextUtils.isEmpty(msg)) {            msg = "";        }        tv_msg.setText(msg);        return view;    }    /**     * 开始旋转动画     */    public void showAnim(ImageView imageView) {        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();        if (animationDrawable != null) {            animationDrawable.start();        }    }}

setStyle表示的是对话框的样式,其实这些样式完全可以利用代码来实现,在布局中的简单实现如下:

  <style name="base_loading_dialogfragment">        <item name="android:windowIsFloating">true</item>        <item name="android:backgroundDimEnabled">false</item>        <item name="android:gravity">center</item>        <item name="android:windowBackground">@android:color/transparent</item>   </style>
其实就是设置DialogFragment控件的位置,背景的灰暗效果

c.具体在Activity,fragment的使用,注意:DialogFragment可以在fragment中使用

        /** * 显示正在加载的DialogFragment */public void showDialog(String msg, String className) {if (BackUtil.isActivityRunning(this, className)) {if (loadingDialogFragment == null) {if (msg != null) {loadingDialogFragment = LoadingDialogFragment.newInstance(msg);} else {loadingDialogFragment = LoadingDialogFragment.newInstance("");}}if (!this.isFinishing()) {//显示正在加载....FragmentTransaction ft = getSupportFragmentManager().beginTransaction();loadingDialogFragment.show(ft, "onLoadingDilogFragment");}}}/** * 隐藏正在加载的DialogFragment */public void hiddenDialog() {if (loadingDialogFragment != null) {loadingDialogFragment.dismiss();loadingDialogFragment = null;}}

4.注意事项:

       在使用的过程中曾经试图在DialogFragment创建好之后修改DialogFragment控件的现实的效果,例如我在项目中打算修改textview显示的文字,在使用中

一直报空指针异常,大致就是当你在使用后再次使用DialogFragment的控件时,会找不到控件。为了解决这个问题,我就想到了构造函数直接进行传值,问题

是解决了,但是在as中会报警告,提示我要使用谷歌推荐的方法,大致就是使用能够bundle来实现,原因是当回调的时候会找不到值

    使用bundle实现如下:

   public static final LoadingDialogFragment newInstance(String msg) {        LoadingDialogFragment ldf = new LoadingDialogFragment();        //谷歌推荐使用这种方式保存传进来的数据        Bundle bundle = new Bundle();        bundle.putSerializable("msg", msg);        ldf.setArguments(bundle);        return ldf;    }    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置是否可以取消  点击返回键可以让DialogFragment消失        setCancelable(true);        setStyle(R.style.base_loading_dialogfragment, R.style.base_loading_dialogfragment);        Bundle bundle = getArguments();        if (bundle != null) {            msg = bundle.getString("msg");        }    }

       其实这一点和saveinstances差不多,就是先把值存入到bundle中,然后在取出来。






0 0
原创粉丝点击