在项目中需要用到流式布局的样式,此文学习鸿洋大神的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);//默认选中第一个

最新文章

  1. PHPer不能不看的50个细节!
  2. [Web API] Web API 2 深入系列(1) 路由
  3. ZBrush该如何通过结合KeyShot制作逼真玉佩
  4. Leetcode详解Maximum Sum Subarray
  5. java:[1,1] 需要class, interface或enum
  6. 怎么让LinearLayout充满ScrollView
  7. Authentication with SignalR and OAuth Bearer Token
  8. 【转载】free查看内存
  9. NOIP2015 子串 (DP+优化)
  10. GridView控件的光棒效应
  11. ocs添加仓库受限问题
  12. MBR区、DBR区、FAT区、DIR区和DATA区的区别
  13. 编译Uboot时提示error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
  14. 阿里巴巴Java规约插件试用
  15. YARN的笔记
  16. Windbg:通过vmware调试进行内核调试
  17. OpenEXR的读取机制
  18. SpringMVC学习笔记:单例与并发问题
  19. HBase写入性能改造(续)--MemStore、flush、compact参数调优及压缩卡的使用【转】
  20. [PyCharm] 设置自动启动时自动打开项目

热门文章

  1. [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) in off
  2. mac chromedriver error
  3. vue项目1-pizza点餐系统6-路由精讲之复用router-view
  4. Python爬虫之urllib.parse详解
  5. Django之AJAX请求
  6. Django模型层1
  7. MongoDB与python 交互
  8. HMP许可更新
  9. 矩阵快速幂 求斐波那契第N项
  10. hdu 1505 单调栈升级版