安卓项目实战之具有显示,单选,多选功能的流式布局

版权声明:转载必须注明本文转自郭子轩的博客 https://blog.csdn.net/gpf1320253667/article/details/82805202

效果演示

在这里插入图片描述
GitHub地址:https://github.com/crazyandcoder/MultiLineChoose
更多功能的流式布局用法,参见鸿洋大神的FlowLayout,GitHub地址:https://github.com/hongyangAndroid/FlowLayout

使用方法

1、gradle引用:

compile 'liji.library.dev:multilinechooselib:2.0.0'

2、布局文件中使用:

<com.ihidea.multilinechooselib.MultiLineChooseLayout
     android:id="@+id/flowLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="@style/FlowLayout"
     android:layout_marginTop="50dp"
     >
</com.ihidea.multilinechooselib.MultiLineChooseLayout>

3、自定义属性:

<style name="FlowLayout">
        
        //选项item是否可以点击
        <item name="item_click">true</item>

        //选项item文字默认值
        <item name="item_textColor">#666666</item>

        //选项item背景色
        <item name="item_backgroundColor">#ffffff</item>

        //选中后选项item的文字颜色
        <item name="item_selectedTextColor">#ffffff</item>

        //选中后选项item的背景颜色
        <item name="item_selectedBackgroundColor">#7289ff</item>

        //选项item的圆角大小
        <item name="item_radius">10dp</item>

        //选项item的边缘颜色
        <item name="item_strokeColor">#939393</item>

        //选项item选中后边缘颜色
        <item name="item_selectedStrokeColor">#939393</item>

        //选项item边缘的大小
        <item name="item_strokeWidth">2px</item>

        //水平方向,选项item的间距
        <item name="item_horizontalSpacing">10dp</item>

        //选项item内部padding
        <item name="item_horizontalPadding">5dp</item>

        ////竖直方向,选项item的间距
        <item name="item_verticalPadding">5dp</item>


        //选项item的大小,如果设置wrap_content则包裹它的内容,可设置固定值如80,单位dp
        <item name="item_width">wrap_content</item>
        <item name="item_height">wrap_content</item>


        //限制item文字显示长度
        <item name="item_maxEms">5</item>
        <item name="item_singleLine">true</item>
        
       //限制item文字大小
        <item name="item_textSize">15dp</item>

        //是否可以多选
        <item name="item_multiChooseable">true</item>
    </style>

如果要设置标签不可点击,请在style中设置item_click=false,单选多选时都必须设置为true
如果要设置单选,请在style中设置item_multiChooseable=false
如果要设置多选,请在style中设置item_multiChooseable=true
如果要设置流式布局,则将item的宽和高都设置为wrap_content

4、代码逻辑较为简单:

private List<String> mColorData = new ArrayList<>();
private MultiLineChooseLayout singleChoose;
singleChoose = (MultiLineChooseLayout) findViewById(R.id.singleChoose);

	mColorData.add("红色");
        mColorData.add("橙色");
        mColorData.add("黄色");
        mColorData.add("绿色");
        mColorData.add("蓝色");
        mColorData.add("灰色");
        mColorData.add("紫色");
        
//设置数据源
singleChoose.setList(mColorData);


//单选时这样写
singleChoose.setOnItemClickListener(new MultiLineChooseLayout.onItemClickListener() {
            @Override
            public void onItemClick(int position, String text) {
                singleChooseTv.setText("结果:position: " + position + "   " + text);
            }
});

//多选时这样写
List<String> singleChooseResult = new ArrayList<>();
singleChoose.setOnItemClickListener(new MultiLineChooseLayout.onItemClickListener() {
            @Override
            public void onItemClick(int position, String text) {
               singleChooseResult = singleChoose.getAllItemSelectedTextWithListArray();
                if (singleChooseResult != null && singleChooseResult.size() > 0) {
                    String textSelect = "";
                    for (int i = 0; i < singleChooseResult.size(); i++) {
                        textSelect += singleChooseResult.get(i) + " , ";
                    }
                    Toast.makeText(mActivity, textSelect, Toast.LENGTH_SHORT).show();
                }
            }
});

// 取消所有选中项
singleChoose.cancelAllSelectedItems();

常用方法介绍

1、设置数据源

public void setList(List<String> tagList);

2、设置默认的选中项

public int setIndexItemSelected(int position)

3、获取选中的item内容

protected String getSelectedItemText()

4、多选情况下,返回所有选中的item内容

public String[] getAllItemSelectedTextWithStringArray()

5、多选情况下,返回所有选中项的下标

public ArrayList<Integer> getAllItemSelectedIndex()

6、取消所有选中项

public void cancelAllSelectedItems()

7、设置item点击事件

public void setOnItemClickListener(onItemClickListener l);
//点击事件,返回点击的position和选中项
 public interface onItemClickListener {
        void onItemClick(int position, String text);
 }

所有支持的属性如下图:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/gpf1320253667/article/details/82805202