android双列复选框组件

近期项目有用到多选需求,UI设计为双列样式,先看效果图。

双列选择且有说明,说明条数不一。使用GridView或者RecyclerView我嫌麻烦所以就写了一个集成LinearLayout的类组件。

三层LinearLayout嵌套后刚好满足需求。先上代码。

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.CompoundButton;
import android.widget.TextView;

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

import *******.R;//R文件
import *******.BusinessBean;//数据类
import *******.ContextWindoUtil;//dip转px类


public class BusinessChoiceLayout extends LinearLayout implements CompoundButton.OnCheckedChangeListener {
    private List<String> TAGs = new ArrayList<>(); //选中TAG
    private int SucNum = 0; //选中TAG数量
    private List<CheckBox> checkBoxes = new ArrayList<>(); //复选框
    List<BusinessBean> businessBeans = new ArrayList<>(); //数据源
    public BusinessChoiceLayout(Context context) {
        super(context);
        initLayout();
    }

    public BusinessChoiceLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initLayout();
    }

    public BusinessChoiceLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout();
    }
    private void initLayout() {
        //设置Layout的排列方式
        setOrientation(VERTICAL);
    }
    public void addData(BusinessBean data){
        this.businessBeans.add(data);
        notifyDataSetChanged(businessBeans);
    }
    public void addData(BusinessBean data,int p){
        this.businessBeans.add(p,data);
        notifyDataSetChanged(businessBeans);
    }
    //刷新数据
    public void notifyDataSetChanged(List<BusinessBean> datas){
        this.businessBeans = datas;
        removeAllViews();
        addView(datas);
    }
    @SuppressLint("ResourceType")
    private void addView(List<BusinessBean> datas){
        if (datas==null){//空数据处理
            datas = new ArrayList<>();
        }
        //定义边距
        int margin = ContextWindoUtil.dip2px(getContext(), 20);
        for (int i = 0;i<datas.size();i+=2) {//每排显示两个
            BusinessBean data = datas.get(i);//获取当前行第一个内容
            LinearLayout linearLayout = new LinearLayout(getContext());//创建当前行LinearLayout控件
            linearLayout.setOrientation(HORIZONTAL);//设置LinearLayout排序方式
            linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));//设置LayoutParams,如不设置可能会出现不显示的情况

            //设置当前行第一个参数
            LinearLayout l1 = new LinearLayout(getContext());
            l1.setOrientation(VERTICAL);
            l1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
            l1.setPadding(margin, 0, margin, margin);//设置边距

            //定义复选框
            CheckBox checkBox = new CheckBox(getContext());
            checkBox.setPadding(margin / 2, 0, 0, 0);
            checkBox.setTextSize(14);
            checkBox.setButtonDrawable(R.drawable.check_box);//设置复选框的图片
            checkBox.setText(data.getTitle());//设置复选框文字
            checkBox.setTag(data.getId());//设置TAG , 用TAG代替ID
            checkBox.setOnCheckedChangeListener(this);//复选框点击事件监听
            l1.addView(checkBox);
            checkBoxes.add(checkBox);//保存至集合
            for (String str:data.getContexts()==null?new ArrayList<String>():data.getContexts()) {//设置当前复选框的说明
                TextView textView = new TextView(getContext());
                textView.setText("· " +str);
                textView.setPadding(ContextWindoUtil.dip2px(getContext(), 30), 0, 0, 0);
                textView.setTextSize(12);
                textView.setTextColor(Color.parseColor("#ff7f92a9"));
                l1.addView(textView);
            }

            linearLayout.addView(l1);

            //设置中间分割线(根据需求设置)
            View view = new View(getContext());
            LayoutParams params = new LayoutParams(2,ViewGroup.LayoutParams.MATCH_PARENT);
            if (i==0){
                params.topMargin = margin/2;
            }
            if (i+2>=datas.size()){
                params.bottomMargin = margin/2;
            }
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.paidan_v_bg);
            view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
            linearLayout.addView(view);


            //当前行第二个复选框
            LinearLayout l2 = new LinearLayout(getContext());
            l2.setOrientation(VERTICAL);
            l2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
            l2.setPadding(margin, 0, margin, margin);
            if (i+1<datas.size()) {//判断是否存在数据
                BusinessBean data1 = datas.get(i+1);
                CheckBox checkBox1 = new CheckBox(getContext());
                checkBox1.setPadding(margin / 2, 0, 0, 0);
                checkBox1.setText(data1.getTitle());
                checkBox1.setButtonDrawable(R.drawable.check_box);
                checkBox1.setTag(data1.getId());
                checkBox1.setTextSize(16);
                checkBox1.setOnCheckedChangeListener(this);
                l2.addView(checkBox1);
                checkBoxes.add(checkBox1);
                for (String str1:data1.getContexts()==null?new ArrayList<String>():data1.getContexts()) {
                    TextView textView2 = new TextView(getContext());
                    textView2.setText("· "+str1);
                    textView2.setPadding(ContextWindoUtil.dip2px(getContext(), 30), 0, 0, 0);
                    textView2.setTextSize(13);
                    textView2.setTextColor(Color.parseColor("#ff7f92a9"));
                    l2.addView(textView2);
                }

            }
            linearLayout.addView(l2);
            addView(linearLayout);
        }

    }

    /**
     * 通过TAG设置默认选中
     * @param ids TAG集合
     */
    public void setCheck(String [] ids){
        try {
        for (String id:ids){
                for (CheckBox checkBox:checkBoxes){
                    if (id.equals((String) checkBox.getTag())){
                        checkBox.setChecked(true);
                    }
                }
        }
        }catch (NumberFormatException e){

        }
    }

    /**
     * 选中内容的TAG集合
     * @return
     */
    public String[] getDatas() {
        return TAGs.toArray(new String[TAGs.size()]);
    }

    /**
     * 选中数量
     * @return
     */
    public int getSucNum() {
        return SucNum;
    }

    /**
     * 此处可添加接口来返回点击事件及参数通知页面做相应操作
     * @param compoundButton
     * @param b
     */
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            TAGs.add((String) compoundButton.getTag());//将TAG存入集合
            SucNum++;
        }else {
            TAGs.remove(compoundButton.getTag());//将TAG移除集合
            SucNum--;
        }
        //Log.d("点击了:",(String) compoundButton.getTag());
    }
}

使用方法↓(星号为包名记得修改)

<******.BusinessChoiceLayout
                android:id="@+id/business_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></******.BusinessChoiceLayout>

 

List<BusinessBean> datas = new ArrayList<>();
        BusinessBean bean;
        bean = new BusinessBean();
        bean.setId("10");
        bean.setTitle("按揭房放大贷");
        bean.setContexts(Arrays.asList(new String[]{"本人名下房产"}));
        datas.add(bean);
            businessLayout.notifyDataSetChanged(datas);

也可以作为没有说明的样式↓

 

用法只是不传Contexts

List<BusinessBean> datas = new ArrayList<>();
                BusinessBean bean;
                bean = new BusinessBean();
                bean.setId("1");
                bean.setTitle("打卡工资");
                datas.add(bean);
businessChoiceLayout.notifyDataSetChanged(datas);

就是简单的代码组件,BusinessBean 代码↓

import java.util.List;

public class BusinessBean {
    private String id;
    private String title;
    private List<String> contexts;

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getContexts() {
        return contexts;
    }

    public void setContexts(List<String> contexts) {
        this.contexts = contexts;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值