DialogFragment使用

https://www.jianshu.com/p/d1852b04a0aa
使用一个DialogFragment一般我们需要重新写一个类来继承DialogFragment,DialogFragment原本其实也是一个fragment,只是其内部绑定了dialog,所以很多操作是差不多的。
创建一个DialogFragment有两个方法,一个是我们比较熟悉的,重新onCreateDialog方法, 返回一个创建的dialog对象就行,具体如下:

public class MyDialogFragment extends DialogFragment {

    private static MyDialogFragment_Listener listener1;
    private ListView mTvDialogContent;
    private TextView mBtnCancel;
    private TextView mBtnConfirm;
    private String content;//粘贴板内容
    private TextView mTvClear;


    public static MyDialogFragment newInstance(String content,MyDialogFragment_Listener listener) {
        MyDialogFragment customDialogFragment = new MyDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("content", content);
        customDialogFragment.setArguments(bundle);
        listener1 = listener;
        return customDialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            content = bundle.getString("content");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            //设置 window 的背景色为透明色.
            //如果通过 window 设置宽高时,想要设置宽为屏宽,就必须调用下面这行代码。
//            window.setBackgroundDrawableResource(R.color.transparent);
            WindowManager.LayoutParams attributes = window.getAttributes();
            //在这里我们可以设置 DialogFragment 弹窗的位置
            attributes.gravity = Gravity.CENTER;
            //我们可以在这里指定 window的宽高
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            attributes.width = (int) (display.getWidth()*0.9);
            attributes.height = (int) (display.getHeight()*0.4);
            //设置 DialogFragment 的进出动画
//            attributes.windowAnimations = R.style.DialogAnimation;
            window.setAttributes(attributes);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        View rootView = inflater.inflate(R.layout.popup_clipboard, container);
        initView(rootView);
        return rootView;
    }

    private void initView(View view) {
        mTvDialogContent = view.findViewById(R.id.tv_dialogContent);
        mBtnCancel = view.findViewById(R.id.btn_cancel);
        mBtnConfirm = view.findViewById(R.id.btn_confirm);
        mTvClear = view.findViewById(R.id.clear_tv);
        ProCityAreasDao proCityAreasDao = new ProCityAreasDao();
        final List tabProCityAreasList = proCityAreasDao.getdateFromPostcode(content);
        PostcodeAdapter postcodeAdapter = new PostcodeAdapter(getContext(),tabProCityAreasList);
        postcodeAdapter.setOnItemClickListener(new PostcodeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                listener1.getDataFrom_DialogFragment(tabProCityAreasList.get(position));
                dismiss();
            }
        });
        mTvDialogContent.setAdapter(postcodeAdapter);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (getActivity() instanceof OnDialogClickListener) {
                    //传递消息给 Activity
                    ((OnDialogClickListener) getActivity()).clearData(content);
                }
            }
        });
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (getActivity() instanceof OnDialogClickListener) {
                    //传递消息给 Activity
                    ((OnDialogClickListener) getActivity()).confirmDialog(content);
                }
            }
        });
    }
    // 回调接口,用于传递数据给Activity -------
    public interface MyDialogFragment_Listener {
        void getDataFrom_DialogFragment(TabProCityAreas tabProCityAreas);
    }
}

调用

MyDialogFragment dialog = MyDialogFragment.newInstance(data, 
new MyDialogFragment.MyDialogFragment_Listener() {
                    @Override
                    public void getDataFrom_DialogFragment(TabProCityAreas tabProCityAreas) {
                        mOrigin.setTextWithoutAnimation(
tabProCityAreas.getTownname() + "," + tabProCityAreas.getAreaname() );
                    }
                }) ;
                dialog.show(getSupportFragmentManager(),"dialog");

自定义xml





    
    
    

        

        

        
            
            
            

        

    

在5.0以上的版本中,我们在style中可以查看默认的dialog风格,其实有设置dialog的一些属性,自己定义一种类型的dialog,然后继承就可以修改整体dialog中的风格了,默认动画,背景颜色,或者宽度之类的都是可以的如下:


     
    //Theme.AppCompat.Dialog主题常用的一般有以下属性: 
     
    @android:color/transparent 
     
    @null 
     
    true 
     
    true 
     
    true 
     
    @android:color/transparent 
     
    true 
     
    0.5

你可能感兴趣的:(DialogFragment使用)