android一步步实现dialog选择框,支持单选和多选

前提:需要先导入baseReclyAdapterlibrary的model点击打开链接

1、创建dialog

package com.yann.yanndemo.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.helper.RecyclerViewHelper;
import com.yann.yanndemo.R;
import com.yann.yanndemo.adapter.DialogSelectorAdapter;
import com.yann.yanndemo.entity.SimpleListItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyy on 2018/1/20.
 */

public class DialogSelector extends AlertDialog implements DialogSelectorAdapter.OnItemClickListener {
    private RecyclerView rv_selector_branch;
    private OnSelectorListener cdListener;
    private List mSimpleListItemEntity = new ArrayList<>();
    private List selectorSimpleListItemEntity = new ArrayList<>();
    private DialogSelectorAdapter mSelectorBranchAdapter;
    private Context context;
    private Button btn_cancel;
    private Button btn_ok;
    private String isProcessVariable;

    public DialogSelector(Context context, List mSimpleListItemEntity, String isProcessVariable, OnSelectorListener cdListener) {
        super(context);
        this.context = context;
        this.cdListener = cdListener;
        this.mSimpleListItemEntity = mSimpleListItemEntity;
        this.isProcessVariable = isProcessVariable;    //1为单选,否则为多选
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_selector);
        this.setCanceledOnTouchOutside(true); // 点击外部会消失
        InitViews();
    }

    private void InitViews() {
        rv_selector_branch = (RecyclerView) findViewById(R.id.rv_selector);
        mSelectorBranchAdapter = new DialogSelectorAdapter(R.layout.selector_branch_item, isProcessVariable.equals("1") ? 1 : 0);
        mSelectorBranchAdapter.setOnBranchItemClickListener(this);
        RecyclerViewHelper.initRecyclerViewV(context, rv_selector_branch, true, mSelectorBranchAdapter);
        mSelectorBranchAdapter.setNewData(mSimpleListItemEntity);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_ok = (Button) findViewById(R.id.btn_ok);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cdListener.cancel();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mSimpleListItemEntity.size(); i++) {
                    if (mSimpleListItemEntity.get(i).isSelector()) {
                        selectorSimpleListItemEntity.add(mSimpleListItemEntity.get(i));
                    }
                }
                cdListener.getSelectorData(selectorSimpleListItemEntity);

            }
        });
    }

    @Override
    public void onItemClick(int position) {
        if (isProcessVariable.equals("1")) {
            for (int i = 0; i < mSimpleListItemEntity.size(); i++) {
                if (i == position) {
                    if (!mSimpleListItemEntity.get(i).isSelector()) {
                        mSimpleListItemEntity.get(i).setSelector(true);
                    }
                } else {
                    mSimpleListItemEntity.get(i).setSelector(false);
                }
            }
        } else {
            boolean temp = !mSimpleListItemEntity.get(position).isSelector();
            mSimpleListItemEntity.get(position).setSelector(temp);
        }
        mSelectorBranchAdapter.setNewData(mSimpleListItemEntity);
    }

    public interface OnSelectorListener {
        void getSelectorData(List trees);

        void cancel();
    }
}
添加dialog布局文件dialog_selector




    

        

        

        

            
二、新建弹出框列表适配器

package com.yann.yanndemo.adapter;

import android.support.annotation.LayoutRes;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yann.yanndemo.R;
import com.yann.yanndemo.entity.SimpleListItemEntity;

/**
 * Created by wyy on 2018/1/20.
 */

public class DialogSelectorAdapter extends BaseQuickAdapter {

    private static final String TAG = DialogSelectorAdapter.class.getSimpleName();
    private OnItemClickListener mOnItemClickListener;
    private int isSingle = 1;    //为1的话为单选,默认为单选,否则的话为多选
    public DialogSelectorAdapter(@LayoutRes int layoutResId, int type) {
        super(layoutResId);
        this.isSingle = type;
    }

    public void setOnBranchItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, SimpleListItemEntity item, final int position) {
        RelativeLayout rootView = helper.getView(R.id.rl_branch_item_root);
        TextView name = helper.getView(R.id.txt_tree_name);
        CheckBox cb_contact = helper.getView(R.id.cb_contact);
        RadioButton rb_contact = helper.getView(R.id.rb_contact);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(position);
            }
        });
        name.setText(item.getName());
        if (isSingle == 1) {
            rb_contact.setChecked(item.isSelector());
        } else {
            cb_contact.setVisibility(View.VISIBLE);
            rb_contact.setVisibility(View.GONE);
            cb_contact.setChecked(item.isSelector());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
添加适配器布局文件selector_branch_item



    

    

    


创建实体类

package com.yann.yanndemo.entity;

/**
 * Created by wyy on 2018/1/20.
 */

public class SimpleListItemEntity {
    private String id;
    private String name;
    private boolean isSelector = false;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelector() {
        return isSelector;
    }

    public void setSelector(boolean selector) {
        isSelector = selector;
    }
}
三、创建一个DialogHelper管理dialog

package com.yann.yanndemo.dialog;

import android.content.Context;

import com.yann.yanndemo.R;
import com.yann.yanndemo.entity.SimpleListItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyy on 2018/1/21.
 */

public class DialogHelper {
    private static final String TAG = DialogHelper.class.getSimpleName();
    private Context context;
    private int style = R.style.CustomDialog;//默认style
    private String title = "";//标题
    private List mSimpleListItemEntity = new ArrayList<>();    //选择列表的数据
    private OnHelperSelectorListener onSelectorListener;
    private DialogSelector mDialogSelector;
    private String isProcessVariable;    //判断单选还是多选

    private DialogHelper(Context context) {
        this.context = context;
    }

    /**
     * 初始化
     *
     * @param context
     * @return
     */
    public static DialogHelper create(Context context) {
        return new DialogHelper(context);
    }

    /**
     * 可选,设置dialog的style,一般情况下用默认的
     *
     * @param style
     * @return
     */
    public DialogHelper style(int style) {
        this.style = style;
        return this;
    }

    /**
     * 设置标题,建议使用的时候都配置
     *
     * @param title
     * @return
     */
    public DialogHelper title(String title) {
        this.title = title;
        return this;
    }

    /**
     * 设置部门列表的数据
     *
     * @param contactTreeList
     * @return
     */
    public DialogHelper setSelectorData(List contactTreeList) {
        this.mSimpleListItemEntity = contactTreeList;
        return this;
    }

    /**
     * 设置选择部门监听
     *
     * @param onSelectorListener
     * @return
     */
    public DialogHelper setSelectorListener(OnHelperSelectorListener onSelectorListener) {
        this.onSelectorListener = onSelectorListener;
        return this;
    }

    public DialogHelper setIsProcessVariable(String isProcessVariable) {
        this.isProcessVariable = isProcessVariable;
        return this;
    }

    public void showSelectorDialog() {
        mDialogSelector = new DialogSelector(context, mSimpleListItemEntity, isProcessVariable, new DialogSelector.OnSelectorListener() {
            @Override
            public void getSelectorData(List trees) {
                mDialogSelector.dismiss();
                onSelectorListener.getSelectorPosition(trees);
            }

            @Override
            public void cancel() {
                mDialogSelector.dismiss();
            }
        });
        mDialogSelector.setCancelable(false);
        mDialogSelector.show();
    }

    public interface OnHelperSelectorListener {
        void getSelectorPosition(List trees);
    }

}
最后在activity中调用dialog,实现相关的点击事件

package com.yann.yanndemo.ui.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.yann.yanndemo.R;
import com.yann.yanndemo.dialog.DialogHelper;
import com.yann.yanndemo.entity.SimpleListItemEntity;

import java.util.ArrayList;
import java.util.List;

public class DialogActivity extends Activity implements View.OnClickListener, DialogHelper.OnHelperSelectorListener {
    private static final String TAG = DialogActivity.class.getSimpleName();
    private Button btn_dialog_selector;

    //选择弹出框
    private List mSimpleListItemEntity = new ArrayList<>();
    private String selectorBranchName = "";
    private String branchNames = "";
    private static final String isProcessVariable = "1";    //这里1为单选,否则为多选

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        initView();
        initData();
    }

    private void initData() {
        for (int i = 0; i < 6; i++) {
            SimpleListItemEntity simpleListItemEntity = new SimpleListItemEntity();
            simpleListItemEntity.setId(i + "");
            simpleListItemEntity.setName("星期" + i);
            mSimpleListItemEntity.add(simpleListItemEntity);
        }

    }

    private void initView() {
        btn_dialog_selector = (Button) findViewById(R.id.btn_dialog_selector);
        btn_dialog_selector.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_selector:
                DialogHelper.create(this).setIsProcessVariable(isProcessVariable).setSelectorData(mSimpleListItemEntity).setSelectorListener(this)
                        .showSelectorDialog();
                break;
            default:
                break;
        }
    }

    /**
     * 获取选择弹出框返回的数据
     *
     * @param trees
     */
    @Override
    public void getSelectorPosition(List trees) {
        StringBuilder branchIdBuilder = new StringBuilder();
        StringBuilder branchNameBuilder = new StringBuilder();
        if (!selectorBranchName.equals(" ")) {
            selectorBranchName = " ";
        }
        branchNames = "";
        if (trees != null && !trees.isEmpty()) {
            for (int i = 0; i < trees.size(); i++) {
                String branchId = trees.get(i).getId();
                if (i == 0) {
                    branchIdBuilder.append(branchId);
                    branchNameBuilder.append(trees.get(i).getName());
                } else {
                    branchIdBuilder.append("," + branchId);
                    branchNameBuilder.append("," + trees.get(i).getName());
                }
                for (int j = 0; j < mSimpleListItemEntity.size(); j++) {
                    if (branchId.equals(mSimpleListItemEntity.get(j).getId())) {
                        mSimpleListItemEntity.get(j).setSelector(true);
                    }
                }
            }
            branchNames = branchNameBuilder.toString();
            selectorBranchName = " " + branchNames;
        }
        Toast.makeText(this, selectorBranchName, Toast.LENGTH_LONG).show();
        Log.d(TAG, "selectorBranchName===" + selectorBranchName);
    }
}

以上可以实现弹出框的默认选择值,保存选中状态,实体类和适配器的布局都可以根据项目的实际情况自定义,效果图如下:

android一步步实现dialog选择框,支持单选和多选_第1张图片

demo地址



你可能感兴趣的:(Android,android,dialog,选择框,单选和多选)