1. 程式人生 > >自定義控件 - 流式布局:FlowLayout

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

mat tag cte vertical tail max 數據 使用 getview

在項目中需要用到流式布局的樣式,此文學習鴻洋大神的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);//默認選中第一個

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