单选多选按钮

2018-04-27  本文已影响14人  糖葫芦_倩倩
notify9.gif

项目中出现单选多选这种情况挺多的,所以整合成一个控件了,使用方便,可以设置单选,也可以设置多选,最主要的是采用了流式布局,会自动根据屏幕大小和内容进行自动折行。

<app.myapplication.ButtonFlowLayout
        android:id="@+id/buttnLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
mButtonFLowLayout = findViewById(R.id.buttnLayout);

        data.add("天气不错");
        data.add("咔嚓咔嚓咔嚓");
        data.add("多云");
        data.add("雷电交加");
        data.add("雨夹雪");

        mButtonFLowLayout.setDataList(data);
mButtonFLowLayout.setDataList(data,true);

默认不传是单选效果,默认选中第一项,也可以修改:
0 默认是第一项,例如设置默认选中第二项:

mButtonFLowLayout.setSingleSelectItem(1);
mButtonFLowLayout.setOnSingleChanedListener(new ButtonFlowLayout.OnSingleChanedListener() {
           @Override
            public void onSingleChaned(int index) {
               Toast.makeText(MainActivity.this,"当前选中:"+index,Toast.LENGTH_SHORT).show();

            }
        });

多选

mButtonFLowLayout.setOnMultifyChanedListenerr(new ButtonFlowLayout.OnMultifyChanedListener() {
            @Override
            public void onMultifyChaned(List<Integer> indexs) {
               for(int i=0;i<indexs.size();i++){
                   Log.i("xx","当前选中的集合是:"+indexs.get(i));
                }
            }
        });

方便快捷,代码传送门:https://github.com/QQabby/MultifyCheckButton 喜欢就给个start吧。

上一篇 下一篇

猜你喜欢

热点阅读