Android customize a list of commonly used simple pop Dialog

First, a custom dialog inherited from dialog

public class CustomDialog extends Dialog {

    private Context c;

    public CustomDialog(@NonNull Context context) {
        super(context);

        c = context;
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        c = context;


    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);

        c = context;

    }

    /**
     * 基本列表弹窗
     * @param datas   内容
     * @param dialogListener  位置回调
     */

    public void init(String[]datas, final DialogListener dialogListener){

        View v = LayoutInflater.from(c).inflate(R.layout.dialog_style1,null);
        this.setContentView(v);
        this.setCancelable(true);

        RecyclerView rv = v.findViewById(R.id.rv);
        DialogAdapter adapter = new DialogAdapter(c, datas, new DialogRvListener() {
            @Override
            public void onItemClick(int pos) {

                if (dialogListener!=null){

                    dialogListener.onItemClick(pos);
                    dismiss();

                }

            }
        });

        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(c));

        //窗口位置
        setWindow(getWindow());

    }

    /***
     * 带标题弹窗
     * @param datas   内容
     * @param title    标题
     * @param dialogListener  位置回调
     */

    public void init(String[]datas,String title,final DialogListener dialogListener){

        View v = LayoutInflater.from(c).inflate(R.layout.dialog_style2,null);
        this.setContentView(v);
        this.setCancelable(true);

        //列表
        RecyclerView rv = v.findViewById(R.id.rv);
        DialogAdapter adapter = new DialogAdapter(c, datas, new DialogRvListener() {
            @Override
            public void onItemClick(int pos) {

                if (dialogListener!=null){

                    dialogListener.onItemClick(pos);
                    dismiss();

                }

            }
        });

        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(c));

        //标题
        TextView tv = v.findViewById(R.id.tv_title);
        tv.setText(title);


        //窗口位置
        setWindow(getWindow());

    }

    /***
     * 自定义标题
     * @param datas
     * @param viewId
     * @param dialogListener
     */

    public void init(String[]datas,int viewId,final DialogListener dialogListener){

        LinearLayout v = new LinearLayout(c);
        v.setOrientation(LinearLayout.VERTICAL);

        //自定义标题
        View titleView = LayoutInflater.from(c).inflate(viewId,null);
        LinearLayout.LayoutParams header_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        v.addView(titleView,header_params);

        //列表
        RecyclerView rv = new RecyclerView(c);
        DialogAdapter adapter = new DialogAdapter(c, datas, new DialogRvListener() {
            @Override
            public void onItemClick(int pos) {

                if (dialogListener!=null){

                    dialogListener.onItemClick(pos);
                    dismiss();

                }

            }
        });


        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(c));
        rv.setId(R.id.rv);
        LinearLayout.LayoutParams rv_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        v.addView(rv,rv_params);


        this.setContentView(v);
        this.setCancelable(true);

        //窗口位置
        setWindow(getWindow());

    }


    /**
     * 设置窗口位置
     */

    private void setWindow(Window wd){


        if (wd==null){
            return;
        }
        WindowManager.LayoutParams params = wd.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wd.setAttributes(params);
        wd.setGravity(Gravity.BOTTOM);

    }

    /**
     * 设置动画
     */

    public void setAnim(int styleId){

        Window w = getWindow();
        if (w==null){
            return;
        }
        w.setWindowAnimations(styleId);

    }

    /**
     * 设置位置
     */

    public void setPosition(int position){
        Window w = getWindow();
        if (w==null){
            return;
        }
        w.setGravity(position);

    }


}

dialog layout file (here only show no title bar)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/ll"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

 <android.support.v7.widget.RecyclerView
     android:id="@+id/rv"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

</LinearLayout>

dialog in the adapter used recyclerview

public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.DialogViewHolder> {

    private String[]datas;
    private DialogRvListener dialogRvListener;
    private Context c;

    DialogAdapter(Context c, String[] datas, DialogRvListener dialogRvListener){

        this.c = c;
        this.datas = datas;
        this.dialogRvListener = dialogRvListener;

    }


    @NonNull
    @Override
    public DialogViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(c).inflate(R.layout.item_dialog,viewGroup,false);

        return new DialogViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull DialogViewHolder holder, final int i) {

        holder.tv.setText(datas[i]);
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialogRvListener.onItemClick(i);

            }
        });


    }

    @Override
    public int getItemCount() {
        return datas.length;
    }

    class DialogViewHolder extends RecyclerView.ViewHolder{

        TextView tv;
        DialogViewHolder(@NonNull View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
        }
    }
}

recyclerview item in the layout file is actually a textview

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="@android:color/black"
    android:text="123"
    android:gravity="center"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:id="@+id/tv"/>

Two locations interfaces, in fact, the method is the same, but in order to distinguish between two write:

Dialog Listener

void onItemClick(int pos);

DialogRvListener

void onItemClick(int pos);

In this way, a basic custom dialog is complete, just call in to the activity

  //带标题,同时自定义风格(这样可以保证宽度占满屏幕)
       CustomDialog titledialog = new CustomDialog(this,R.style.customdialog);
        titledialog.init(datas, "请选择", new DialogListener() {
            @Override
            public void onItemClick(int pos) {

                //your logic

            }
        });
//自定义动画
        titledialog.setAnim(R.style.mydialog);

I have a good package and spread to github, it can be added directly dependent on use. If you feel good trouble spots like this one, you have any questions or good suggestions can leave a comment below.

Source address
https://github.com/moo611/DesongDialog-Android

Released four original articles · won praise 2 · views 76

Guess you like

Origin blog.csdn.net/qq_39678305/article/details/102930844