Android 仿RxDialog自定义DialogFragment

Android 仿RxDialog自定义DialogFragment_第1张图片
image

一、简介
二、效果预览
三、实现步骤
(1)LinListDialogFragment:列表弹窗
(2)LinCustomDialogFragment:自定义弹窗
(3)BaseDialogFragment:base弹窗
四、功能解析
(1)BaseDialogFragment
(2)LinListDialogFragment
(3)LinCustomDialogFragment
五、Demo地址
六、内容推荐


一、简介

不知道大家有没有用过RxDialog,就是封装Dialog的一个工具。没用过不知道是咋也没关系,可以下载本项目玩一下。项目中有集成了RxDialog。而本篇是仿RxDialog弹窗样式制作的一个DialogFragment工具。为什么还要如此多此一举,主要是个人使用RxDialog没有自己定义的方便。所以仿RxDialog又封装了一个。喜欢的可以收藏下没有RxDialog那么多类就是。

二、效果预览

不吹牛逼了,先看看效果自行决定。没那么绚丽多彩,实用就行。没有找到自己想要的样式也可以自定义布局,只需继承下面的一个类即可。

image

image

底部弹窗主要还是仿微信,不像没关系。大不了不用就是。。。开心就好

​三、实现步骤

上面实现过程分为两个类:LinListDialogFragment,LinCustomDialogFragment

(1)LinListDialogFragment:列表弹窗

1、调用方式

private LinListDialogFragment init;
init = LinListDialogFragment.init(datas).setOnClickListener(new LinListDialogFragment.OnItemClickListener() {
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Toast.makeText(activity,datas.get(position),Toast.LENGTH_SHORT).show();
        init.dismiss();
    }
});

2、LinListDialogFragment实现过程

import android.os.Bundle;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import blcs.lwb.lwbtool.R;
import blcs.lwb.lwbtool.adapter.DialogFragmentAdapter;
public class LinListDialogFragment extends BaseDialogFragment {
    private View view;
    private DialogFragmentAdapter mAdapter;
    private ArrayList datas;
    private static LinListDialogFragment dialogFragment;
    private final static String Key = "LinListDialogFragment";
    private RecyclerView rv;

    public static LinListDialogFragment init(ArrayList datas) {
        if (dialogFragment == null) {
            dialogFragment = new LinListDialogFragment();
            Bundle bundle = new Bundle();
            bundle.putStringArrayList(Key, datas);
            dialogFragment.setArguments(bundle);
        }
        return dialogFragment;
    }
    @Override
    public View getView(LayoutInflater inflater, ViewGroup container) {
        view =inflater.inflate(R.layout.tool_recyclerview, container,false);
        rv = view.findViewById(R.id.tool_recyclerView);
        Bundle arguments = getArguments();
        datas = arguments.getStringArrayList(Key);
        initRv();
        return view;
    }
    private void initRv() {
        mAdapter = new DialogFragmentAdapter();
        rv.setLayoutManager(new LinearLayoutManager(getActivity(),OrientationHelper.VERTICAL,false));
        rv.setAdapter(mAdapter);
        mAdapter.setNewData(datas);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (linstener != null) {
                    linstener.onItemClick(adapter, view, position);
                }
            }
        });
    }
    public OnItemClickListener linstener;
    public LinListDialogFragment setOnClickListener(OnItemClickListener onItemClickListener) {
        linstener = onItemClickListener;
        return this;
    }
    public interface OnItemClickListener {
        void onItemClick(BaseQuickAdapter adapter, View view, int position);
    }
}

布局就是一个简单RecyclerView。

RecyclerView的适配器大家应该都玩腻了,就不贴了省的浪费空间。如果觉得比较麻烦可以下载本项目直接CP好了

BaseDialogFragment类下面再贴。基础封装类都会被用到。

详细实现过程下面再细说,因为有的可能看了代码就不需要作者再啰嗦了(什么意思都表现在代码里面,任何解释都没代码来的直接、飘逸)。

Android 仿RxDialog自定义DialogFragment_第2张图片
image

(2)LinCustomDialogFragment:自定义弹窗

1、调用方式

LinCustomDialogFragment comDialog = LinCustomDialogFragment.init();
//下面的代码写在点击事件当中

//显示透明图片弹窗
comDialog.setImage(R.mipmap.img_view).setType(LinCustomDialogFragment.TYPE_IMAGE).show(getFragmentManager());
//显示确定弹窗
comDialog.setTitle("提示").
        setContent("内容不能为空").
        setType(LinCustomDialogFragment.TYPE_SURE)
        .setOnClickListener(new LinCustomDialogFragment.OnSureListener() {
            @Override
            public void clickSure() {
                 Toast.makeText(activity,"确定",Toast.LENGTH_SHORT).show();
            }
        }).show(getFragmentManager());
//显示确定取消弹窗
comDialog.setTitle(getString(R.string.app_name)).
        setContent("是否退出登录?").
        setType(LinCustomDialogFragment.TYPE_CANCLE)
        .setOnClickListener(new LinCustomDialogFragment.OnSureCancleListener() {
            @Override
            public void clickSure(String str) {
                RxToast.info(activity,"确定");
            }
            @Override
            public void clickCancle() {
                RxToast.info(activity,"取消");
            }
        }).show(getFragmentManager());
//显示输入弹窗
comDialog.setTitle(getString(R.string.app_name)).
        setType(LinCustomDialogFragment.TYPE_EDITEXT)
        .setOnClickListener(new LinCustomDialogFragment.OnSureCancleListener() {
            @Override
            public void clickSure(String str) {
                RxToast.info(activity,str);
            }
            @Override
            public void clickCancle() {
                RxToast.info(activity,"取消");
            }
        }).show(getFragmentManager());
//显示加载弹窗
comDialog.setType(LinCustomDialogFragment.TYPE_LOADING).show(getFragmentManager());
//显示大图弹窗
comDialog.setImage(R.mipmap.ic1).setType(LinCustomDialogFragment.TYPE_IMAGE_BIG).show(getFragmentManager());

2、LinCustomDialogFragment实现过程

import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import blcs.lwb.lwbtool.R;
import blcs.lwb.lwbtool.View.RxScaleImageView;
import blcs.lwb.lwbtool.utils.dialog.decoder.ImageSource;

public class LinCustomDialogFragment extends BaseDialogFragment implements View.OnClickListener {
    private static final String TAG = "CustomDialogFragment";
    private static LinCustomDialogFragment customDialogFragment;

    private int mipmap;
    private String title;
    private String content;

    private int DialogType;

    public static final int TYPE_IMAGE = 0;
    public static final int TYPE_SURE = 1;
    public static final int TYPE_CANCLE = 2;
    public static final int TYPE_EDITEXT = 3;
    public static final int TYPE_LOADING = 4;
    public static final int TYPE_IMAGE_BIG = 5;
    private EditText et_dialog_fragment_content;
    private TextView tv_dialog_fragment_content;

    public static LinCustomDialogFragment init() {
        if (customDialogFragment == null) {
            customDialogFragment = new LinCustomDialogFragment();
        }
        return customDialogFragment;
    }

    @Override
    public View getView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_dialog_custom, container, false);
        initUI(view);
        return view;
    }

    private void initUI(View view) {
        LinearLayout ll_dialog_fragment = view.findViewById(R.id.ll_dialog_fragment);
        //TYPE_IMAGE
        ImageView iv_dialog_fragment = view.findViewById(R.id.iv_dialog_fragment);
        //TYPE_SURE
        LinearLayout ll_dialog_fragment_sure = view.findViewById(R.id.ll_dialog_fragment_sure);
        TextView tv_dialog_fragment_title = view.findViewById(R.id.tv_dialog_fragment_title);
        tv_dialog_fragment_content = view.findViewById(R.id.tv_dialog_fragment_content);
        TextView tv_dialog_fragment_sure = view.findViewById(R.id.tv_dialog_fragment_sure);
        tv_dialog_fragment_sure.setOnClickListener(this);
        //TYPE_CANCLE
        TextView tv_dialog_fragment_cancle = view.findViewById(R.id.tv_dialog_fragment_cancle);
        View view_dialog_fragment_cancle = view.findViewById(R.id.view_dialog_fragment_cancle);
        tv_dialog_fragment_cancle.setOnClickListener(this);
        //TYPE_EDITEXT
        et_dialog_fragment_content = view.findViewById(R.id.et_dialog_fragment_content);
        //TYPE_LOADING
        LinearLayout ll_dialog_fragment_loading = view.findViewById(R.id.ll_dialog_fragment_loading);
        //TYPE_IMAGE_BIG
        RelativeLayout rl_dialog_fragment_big = view.findViewById(R.id.rl_dialog_fragment_big);
        RxScaleImageView rx_dialog_fragment_big = view.findViewById(R.id.rx_dialog_fragment_big);
        ImageView iv_dialog_fragment_close = view.findViewById(R.id.iv_dialog_fragment_close);
        switch (DialogType) {
            case TYPE_IMAGE:
                ll_dialog_fragment.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
                iv_dialog_fragment.setVisibility(View.VISIBLE);
                iv_dialog_fragment.setImageResource(mipmap);
                setGravity(Gravity.CENTER_HORIZONTAL);
                break;
            case TYPE_SURE:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_CANCLE:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.VISIBLE);
                tv_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                view_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_EDITEXT:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.GONE);
                tv_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                view_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                et_dialog_fragment_content.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_LOADING:
                ll_dialog_fragment_loading.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_IMAGE_BIG:
                rl_dialog_fragment_big.setVisibility(View.VISIBLE);
                rx_dialog_fragment_big.setImage(ImageSource.resource(mipmap));
                iv_dialog_fragment_close.setOnClickListener(this);
                setGravity(Gravity.NO_GRAVITY);
                break;
        }
    }

    private void common(LinearLayout ll_dialog_fragment_sure, TextView tv_dialog_fragment_title) {
        ll_dialog_fragment_sure.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(title)) {
            tv_dialog_fragment_title.setVisibility(View.VISIBLE);
            tv_dialog_fragment_title.setText(title);
        }
        tv_dialog_fragment_content.setText(content);
    }

    /**
     * 设置图片
     */
    public LinCustomDialogFragment setImage(int mipmap) {
        this.mipmap = mipmap;
        return this;
    }

    /**
     * 设置标题
     */
    public LinCustomDialogFragment setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * 设置内容
     */
    public LinCustomDialogFragment setContent(String content) {
        this.content = content;
        return this;
    }

    /**
     * 显示格式
     */
    public LinCustomDialogFragment setType(int type) {
        this.DialogType = type;
        return this;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_dialog_fragment_sure) {
            if (onSureListener != null) {
                onSureListener.clickSure();
            }
            if (onSureCancleListener != null) {
                onSureCancleListener.clickSure(et_dialog_fragment_content.getText().toString());
            }
        } else if (i == R.id.tv_dialog_fragment_cancle) {
            if (onSureCancleListener != null) {
                onSureCancleListener.clickCancle();
            }
        } else if (i == R.id.iv_dialog_fragment_close){
        }
        dismiss();
    }

    public OnSureListener onSureListener;
    public OnSureCancleListener onSureCancleListener;

    public interface OnSureListener {
        void clickSure();
    }

    public interface OnSureCancleListener {
        void clickSure(String EdiText);
        void clickCancle();
    }

    public LinCustomDialogFragment setOnClickListener(OnSureListener listener) {
        onSureListener = listener;
        return this;
    }

    public LinCustomDialogFragment setOnClickListener(OnSureCancleListener listener) {
        onSureCancleListener = listener;
        return this;
    }
}

大家CP完可能会发现一个比较麻烦的问题,这边还引用了RxScaleImageView图片缩放控件。不过这个不是重点,大家可以先把该控件去掉,忽略这个展示图片功能。这篇要告诉大家的是如何自定义自己需要的DialogFragment。就请不要在意这些细节。

下面贴出布局:



    
    
    
    
        
            
            
            
            
            
                
                
                
            

        
        
        
            
            
        
    

    
    
        
        
    



部分布局资源:

//styles.xml文件中

 
    
    
    

//drawable文件 dialog_fragment_center_radious.xml


    
    


//drawable文件 dialog_editext.xml


    
    
    


//drawable文件 progressbar.xml



    
        
    


ic_clear_white_48dp.png

image

好人做到底,想着大家可能比较懒,所以大部分代码已提供给大家了。

(3)BaseDialogFragment:base弹窗

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import blcs.lwb.lwbtool.R;

/**
 * TODO base弹出框
 */
public abstract class BaseDialogFragment extends DialogFragment {
    private static final String TAG = "BaseDialogFragment";
    private int DefaultGravity;
    private GradientDrawable drawable;
    private View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.bottom_dialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置宽度
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams attributes = getDialog().getWindow().getAttributes();
        if (DefaultGravity == Gravity.CENTER) {
            getDialog().getWindow().setLayout(attributes.width, attributes.height);
        } else if (DefaultGravity == Gravity.BOTTOM || DefaultGravity == Gravity.TOP) {
            getDialog().getWindow().setLayout(dm.widthPixels, attributes.height);
        } else if (DefaultGravity == Gravity.LEFT || DefaultGravity == Gravity.RIGHT) {
            getDialog().getWindow().setLayout(attributes.width, dm.heightPixels);
        } else if (DefaultGravity == Gravity.NO_GRAVITY) {
            getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initDialog();
        view = getView(inflater, container);
        if (drawable != null) {
            view.setBackground(drawable);
        }
        return view;
    }

    /**
     * 方向
     */
    public BaseDialogFragment setGravity(int gravity) {
        DefaultGravity = gravity;
        drawable = new GradientDrawable();
        drawable.setColor(Color.WHITE);
        switch (gravity) {
            case Gravity.CENTER:
                drawable.setCornerRadius(20);
                break;
            case Gravity.BOTTOM:
                drawable.setCornerRadii(new float[]{20, 20, 20, 20, 0, 0, 0, 0});
                break;
            case Gravity.TOP:
                drawable.setCornerRadii(new float[]{0, 0, 0, 0, 20, 20, 20, 20});
                break;
            case Gravity.LEFT:
                drawable.setCornerRadii(new float[]{0, 0, 20, 20, 20, 20, 0, 0});
                break;
            case Gravity.RIGHT:
                drawable.setCornerRadii(new float[]{20, 20, 0, 0, 0, 0, 20, 20});
                break;
            case Gravity.CENTER_HORIZONTAL:
                drawable.setColor(Color.TRANSPARENT);
                break;
        }
        return this;
    }

    /**
     * 自定义View
     */
    public abstract View getView(LayoutInflater inflater, ViewGroup container);

    private void initDialog() {
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = DefaultGravity;
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        setCancelable(true);
    }

    public void show(FragmentManager fragmentManager) {
        setGravity(Gravity.CENTER);
        show(fragmentManager, TAG);
    }

    public void show(FragmentManager fragmentManager, int Gravity) {
        setGravity(Gravity);
        show(fragmentManager, TAG);
    }
}

引用资源:

//styles.xml

    

   

//anim.xml
//bottom_dialog_enter



//bottom_dialog_exit


看到这里已经没了,就3个类。前两个自定义弹窗都是继承BaseDialogFragment基础上实现自己所需的布局,虽然封装的比较简单。但是基本上可以满足大部分的功能弹窗。你们也可以仿造前两个弹窗,定义自己的弹窗。

篇幅有点长了,下面就简单分析一下内容。如果看累了,可以学一下悬梁刺股,个人感觉这招不错,可惜还没学会。

Android 仿RxDialog自定义DialogFragment_第3张图片
image

四、功能解析

(1)BaseDialogFragment

1.初始化DialogFragment样式

在这里面可以设置进出动画,弹窗样式,背景颜色....

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.bottom_dialog);
}

2.定义弹窗的宽高

@Override
    public void onStart() {
        super.onStart();
        //设置宽度
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams attributes = getDialog().getWindow().getAttributes();
        if (DefaultGravity == Gravity.CENTER) {
            getDialog().getWindow().setLayout(attributes.width, attributes.height);
        } else if (DefaultGravity == Gravity.BOTTOM || DefaultGravity == Gravity.TOP) {
            getDialog().getWindow().setLayout(dm.widthPixels, attributes.height);
        } else if (DefaultGravity == Gravity.LEFT || DefaultGravity == Gravity.RIGHT) {
            getDialog().getWindow().setLayout(attributes.width, dm.heightPixels);
        } else if (DefaultGravity == Gravity.NO_GRAVITY) {
            getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels);
        }
    }

通过获取屏幕大小,让弹窗填充满屏幕。比如底部弹出的时候让宽充满屏幕,在左右弹出的时候让高填充满。

3.创建布局与初始化基础属性

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initDialog();
        view = getView(inflater, container);
        if (drawable != null) {
            view.setBackground(drawable);
        }
        return view;
    }
    /**
     * 自定义View
     */
    public abstract View getView(LayoutInflater inflater, ViewGroup container);

    private void initDialog() {
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = DefaultGravity;
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        setCancelable(true);
    }

继承BaseDialogFragment时实现getView方法自定义自己所需的布局。

initDialog()方法设置了弹窗的方向,与弹窗自适应大小。

有人可能有疑问:竟然这边可以设置弹窗大小那为什么onStart()里面在定义一次。

主要是这边设置WindowManager.LayoutParams.MATCH_PARENT也不会填满整个屏幕,因为它还是个Dialog。

4.定义弹窗弹出方向

/**
     * 定义方向
     */
    public BaseDialogFragment setGravity(int gravity) {
        DefaultGravity = gravity;
        drawable = new GradientDrawable();
        drawable.setColor(Color.WHITE);
        switch (gravity) {
            case Gravity.CENTER:
                drawable.setCornerRadius(20);
                break;
            case Gravity.BOTTOM:
                drawable.setCornerRadii(new float[]{20, 20, 20, 20, 0, 0, 0, 0});
                break;
            case Gravity.TOP:
                drawable.setCornerRadii(new float[]{0, 0, 0, 0, 20, 20, 20, 20});
                break;
            case Gravity.LEFT:
                drawable.setCornerRadii(new float[]{0, 0, 20, 20, 20, 20, 0, 0});
                break;
            case Gravity.RIGHT:
                drawable.setCornerRadii(new float[]{20, 20, 0, 0, 0, 0, 20, 20});
                break;
            case Gravity.CENTER_HORIZONTAL:
                drawable.setColor(Color.TRANSPARENT);
                break;
        }
        return this;
    }

这边根据不同的弹出方向设置了,不同的方向圆角。

(2)LinListDialogFragment

1.继承BaseDialogFragment自定义布局样式

    @Override
    public View getView(LayoutInflater inflater, ViewGroup container) {
        view =inflater.inflate(R.layout.tool_recyclerview, container,false);
        rv = view.findViewById(R.id.tool_recyclerView);
        Bundle arguments = getArguments();
        datas = arguments.getStringArrayList(Key);
        initRv();
        return view;
    }

这边布局只用RecyclerView,通过传递过来的数据显示出一个列表的弹窗。没什么特色,也许大家闭着眼睛也能敲出来。

所以就下面就不写太多废话了 ,此处省略一万字........(哈哈!!看在作者写了这么累的份上 ,大家别忘了鼓励一下)

Android 仿RxDialog自定义DialogFragment_第4张图片
image

(3)LinCustomDialogFragment

1、首先实现布局

    @Override
    public View getView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_dialog_custom, container, false);
        initUI(view);
        return view;
    }

     private void initUI(View view) {
        LinearLayout ll_dialog_fragment = view.findViewById(R.id.ll_dialog_fragment);
        //TYPE_IMAGE
        ImageView iv_dialog_fragment = view.findViewById(R.id.iv_dialog_fragment);
        //TYPE_SURE
        LinearLayout ll_dialog_fragment_sure = view.findViewById(R.id.ll_dialog_fragment_sure);
        TextView tv_dialog_fragment_title = view.findViewById(R.id.tv_dialog_fragment_title);
        tv_dialog_fragment_content = view.findViewById(R.id.tv_dialog_fragment_content);
        TextView tv_dialog_fragment_sure = view.findViewById(R.id.tv_dialog_fragment_sure);
        tv_dialog_fragment_sure.setOnClickListener(this);
        //TYPE_CANCLE
        TextView tv_dialog_fragment_cancle = view.findViewById(R.id.tv_dialog_fragment_cancle);
        View view_dialog_fragment_cancle = view.findViewById(R.id.view_dialog_fragment_cancle);
        tv_dialog_fragment_cancle.setOnClickListener(this);
        //TYPE_EDITEXT
        et_dialog_fragment_content = view.findViewById(R.id.et_dialog_fragment_content);
        //TYPE_LOADING
        LinearLayout ll_dialog_fragment_loading = view.findViewById(R.id.ll_dialog_fragment_loading);
        //TYPE_IMAGE_BIG
        RelativeLayout rl_dialog_fragment_big = view.findViewById(R.id.rl_dialog_fragment_big);
        RxScaleImageView rx_dialog_fragment_big = view.findViewById(R.id.rx_dialog_fragment_big);
        ImageView iv_dialog_fragment_close = view.findViewById(R.id.iv_dialog_fragment_close);
        switch (DialogType) {
            case TYPE_IMAGE:
                ll_dialog_fragment.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
                iv_dialog_fragment.setVisibility(View.VISIBLE);
                iv_dialog_fragment.setImageResource(mipmap);
                setGravity(Gravity.CENTER_HORIZONTAL);
                break;
            case TYPE_SURE:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_CANCLE:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.VISIBLE);
                tv_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                view_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_EDITEXT:
                common(ll_dialog_fragment_sure, tv_dialog_fragment_title);
                tv_dialog_fragment_content.setVisibility(View.GONE);
                tv_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                view_dialog_fragment_cancle.setVisibility(View.VISIBLE);
                et_dialog_fragment_content.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_LOADING:
                ll_dialog_fragment_loading.setVisibility(View.VISIBLE);
                setGravity(Gravity.CENTER);
                break;
            case TYPE_IMAGE_BIG:
                rl_dialog_fragment_big.setVisibility(View.VISIBLE);
                rx_dialog_fragment_big.setImage(ImageSource.resource(mipmap));
                iv_dialog_fragment_close.setOnClickListener(this);
                setGravity(Gravity.NO_GRAVITY);
                break;
        }
    }

这边代码看起来比较多的原因是,里面包含了所有的自定义样式。

    public static final int TYPE_IMAGE = 0;//透明图片样式
    public static final int TYPE_SURE = 1;//确定弹窗样式
    public static final int TYPE_CANCLE = 2;//确定取消弹窗样式
    public static final int TYPE_EDITEXT = 3;//输入弹窗样式
    public static final int TYPE_LOADING = 4;//加载弹窗样式
    public static final int TYPE_IMAGE_BIG = 5;//大图片样式

根据上面6种样式,在调用的时候可以通过setType()方法来指定显示样式,隐藏其它布局控件。

    /**
     * 显示格式
     */
    public LinCustomDialogFragment setType(int type) {
        this.DialogType = type;
        return this;
    }

 //调用时
 LinCustomDialogFragment.init()
                .setImage(R.mipmap.img_view)
                .setType(LinCustomDialogFragment.TYPE_IMAGE)
                .show(getFragmentManager());

如果您用到了上面样式,就可以直接拷贝上面那个类直接去使用。而无需引用开源库。也可以通过稍微更改代码实现自己所需。

五、Demo地址

源码地址:

https://github.com/DayorNight/BLCS

apk下载体验地址:https://www.pgyer.com/BLCS

Android 仿RxDialog自定义DialogFragment_第5张图片
image

六、内容推荐

CSDN:https://blog.csdn.net/cs_lwb/article/details/88672286

《Android 获取App应用、缓存、数据等大小适配8.0(仿微信存储空间)》

《Android 仿微信多语言切换》

《Android 仿微信全局字体大小调整》

《Android Rxjava+Retrofit网络请求框架封装(一)》

如果你觉得写的不错或者对您有所帮助的话

不妨顶一个【微笑】,别忘了点赞、收藏、加关注哈

看在花了这么多时间整理写成文章分享给大家的份上,记得手下留情哈

您的每个举动都是对我莫大的支持

Android 仿RxDialog自定义DialogFragment_第6张图片
image

你可能感兴趣的:(Android 仿RxDialog自定义DialogFragment)