支持单选,多选,还可以限制选择的数量的android流式布局

来源:互联网 发布:剪切视频在线软件 编辑:程序博客网 时间:2024/06/06 01:33

前言

由于开发需要,需要做一个效果,一个流式布局的标签,可多选,并且要限制选择的数量,在查找了许多大神写的代码后,决定用鸿洋大神写的一个框架.

项目地址

用法

在app的build.grade中加入依赖

dependencies { compile 'com.zhy:flowlayout-lib:1.0.3'}

声明

在布局文件中声明:

<com.zhy.view.flowlayout.TagFlowLayout        android:id="@+id/id_flowlayout"        zhy:max_select="-1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:padding="20dp">    </com.zhy.view.flowlayout.TagFlowLayout>

支持属性:

max_select:-1为不限制选择数量,>=1的数字为控制选择tag的数量 auto_select_effect 是否开启默认的选中效果,即为selector中设置的效果,默认为true;如果设置为false,则无选中效果,需要自己在回调中处理。

设置数据:

private TagAdapter mAdapter;mFlowLayout.setAdapter(mAdapter=new TagAdapter<String>(mVals)   {       @Override       public View getView(FlowLayout parent, int position, String s)       {           TextView tv = (TextView) mInflater.inflate(R.layout.tv,                   mFlowLayout, false);           tv.setText(s);           return tv;       }   });

textview的布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"              android:layout_marginLeft="5dp"              android:layout_marginTop="10dp"              android:background="@drawable/round_rectangle_bg"              android:paddingBottom="5dp"              android:paddingLeft="10dp"              android:paddingRight="10dp"              android:paddingTop="5dp"              android:text="TAG标签"              android:textColor="@color/normal_text_color"    ></TextView>

设置选中的状态改变,在drawable文件中创建一个selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true">        <shape android:shape="rectangle">            <solid android:color="#4DAEFF" />            <corners android:radius="8dp" />        </shape>    </item>    <item  android:state_checked="false">        <shape android:shape="rectangle">            <solid android:color="#fff" />            <corners android:radius="8dp" />            <stroke android:width="1dp" android:color="#CECECE" />        </shape>    </item></selector>

设置选中的字体颜色改变:

在res下创建一个color文件夹,再创建一个selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!--<item android:color="@android:color/white" android:state_pressed="true" />-->    <!--<item android:color="@android:color/white" android:state_selected="true" />-->    <!--<item android:color="#A494AC" />-->    <item android:color="@android:color/white" android:state_checked="true"/>    <item android:color="#A494AC"/></selector>

事件

点击标签时的回调:

mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener(){  @Override  public void onSelected(Set<Integer> selectPosSet)  {      getActivity().setTitle("choose:" + selectPosSet.toString());  }});

选择多个标签时的回调:

mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener(){  @Override  public void onSelected(Set<Integer> selectPosSet)  {      getActivity().setTitle("choose:" + selectPosSet.toString());  }});

预先设置某个Item被选中

//预先设置选中mAdapter.setSelectedList(1,3,5,7,8,9);//获得所有选中的pos集合flowLayout.getSelectedList();

刷新数据

  mAdapter.notifyDataChanged();

获取所有的数据

Set<Integer> selectedList = mFlowLayout.getSelectedList();

虽然功能挺多了,但是还是不能满足我的要求,因为我的项目中在展示完数据后还可以手动的添加新的标签,此项目没有设置添加新数据的方法.虽然有刷新数据的方法,但是这样之前选中的标签也会一块刷新,也就是等于重置.于是我就结合刷新数据的方法和设置默认选中的方法,解决了我的问题.下面放代码

前面的设置都是一样的,重复的步骤就不写了.

当需要新增加数据的时候,首先获取当前被选中的item是什么

Set<Integer> selectedList = mFlowLayout.getSelectedList();

刷新数据,再将之前存下的item给设置成默认选中的状态

   mAdapter.notifyDataChanged();   mAdapter.setSelectedList(selectedList);

此时在添加新数据的同时,还保证了选中的item状态.完美

还有一个项目也是挺不错的,只不多此项目没有设置最多选中多少个item,不然也是完美,由于项目比较赶没时间修改他的代码,不过也可以推荐看下FlowTag

再次感谢鸿洋大神,一直都很喜欢看他的博客,推荐大家:http://my.csdn.net/lmj623565791