自定义控件 - 流式布局:FlowLayout

在项目中需要用到流式布局的样式,此文学习鸿洋大神的FlowLayout控件,学习使用一下。出自 http://blog.csdn.net/lmj623565791/article/details/38352503

流式布局的特点:

  • 支持setAdapter设置数据源
  • 支持单选、多选
  • 点击回调事件

使用方法

1 gradle依赖

 compile 'com.hyman:flowlayout-lib:1.1.2'

2 布局文件中使用

    <com.zhy.view.flowlayout.TagFlowLayout
        android:id="@+id/tagfl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:max_select="1" />

app:max_select = 1 表示单选

3 代码中设置数据源

    List<String> datas = new ArrayList<>();
        TagAdapter<String> adapter= new TagAdapter<String>(datas) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_tag, parent,false);
                view.setText(o);
                return view;
            }
        };
        tagFlowLayout.setAdapter(adapter);

4 设置textview资源文件布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_tag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="@dimen/margin"
    android:textColor="@drawable/b_text_color"
    android:text="标签" />

5 设置点击事件,默认选中第一个

tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                LogUtil.e("点击了tag:"+position);
                return false;
            }
        });
        tagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                LogUtil.e("选中了tag:"+selectPosSet);

            }
        });
adapter.setSelectedList(0);//默认选中第一个

猜你喜欢

转载自www.cnblogs.com/suiyilaile/p/9073166.html