|
1 | 1 | # AutoFlowLayout |
2 | | -自定义LinearLayout,支持自动换行,指定行数,实现流式布局 |
| 2 | +### 一、AutoFlowLayout应用场景 |
| 3 | +流式布局,在很多标签类的场景中可以用的;而网格布局在分类中以及自拍九宫格等场景很常见。如下所示: |
| 4 | + |
| 5 | +如此使用频繁而又实现简单的控件,怎能不自己撸一个呢?控件,还是定制的好啊。 |
| 6 | +### 二、AutoFlowLayout实现效果 |
| 7 | +先介绍下自己撸的这个控件的功能及效果。 |
| 8 | +#### 1.功能 |
| 9 | +**流式布局** |
| 10 | +- 自动换行 |
| 11 | +- 行数自定:单行/多行 |
| 12 | +- 支持单选/多选 |
| 13 | +- 支持行居中/靠左显示 |
| 14 | +- 支持添加/删除子View |
| 15 | +- 支持子View点击/长按事件 |
| 16 | + |
| 17 | +**网格布局** |
| 18 | +- 行数/列数自定 |
| 19 | +- 支持单选/多选 |
| 20 | +- 支持添加/删除子View |
| 21 | +- 支持子View点击/长按事件 |
| 22 | +- 支持添加多样式分割线及横竖间隔 |
| 23 | +#### 2.效果 |
| 24 | +下面以gif图的形式展现下实现的效果,样式简单了些,不过依然能展示出这个简单控件的多功能实用性。 |
| 25 | + |
| 26 | +**流式布局** |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +**网格布局** |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +最后一个是带间隔以及分割线的,由于录屏原因,只在跳过去的一瞬间显示了粉红色的一条线。真实如下图所示,可以定义横竖间距的大小,以及分割线的颜色,宽度。 |
| 37 | + |
| 38 | +### 三、AutoFlowLayout使用 |
| 39 | +#### 1.添加依赖 |
| 40 | +①.在项目的 build.gradle 文件中添加 |
| 41 | +``` |
| 42 | +allprojects { |
| 43 | +repositories { |
| 44 | +... |
| 45 | +maven { url 'https://jitpack.io' } |
| 46 | +} |
| 47 | +} |
| 48 | +``` |
| 49 | +②.在 module 的 build.gradle 文件中添加依赖 |
| 50 | +``` |
| 51 | +dependencies { |
| 52 | + compile 'com.github.LRH1993:AutoFlowLayout:1.0.2' |
| 53 | +} |
| 54 | +``` |
| 55 | +#### 2.属性说明 |
| 56 | +下表是自定义的属性说明,可在xml中声明,同时有对应的get/set方法,可在代码中动态添加。 |
| 57 | + |
| 58 | +#### 3.使用示例 |
| 59 | +**布局** |
| 60 | +``` |
| 61 | +<?xml version="1.0" encoding="utf-8"?> |
| 62 | + <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 63 | + android:layout_width="match_parent" |
| 64 | + android:layout_height="match_parent"> |
| 65 | + <com.example.library.AutoFlowLayout |
| 66 | + android:id="@+id/afl_cotent" |
| 67 | + android:layout_width="match_parent" |
| 68 | + android:layout_height="wrap_content"/> |
| 69 | +</RelativeLayout> |
| 70 | +``` |
| 71 | +**代码设置数据** |
| 72 | +``` |
| 73 | +mFlowLayout.setAdapter(new FlowAdapter(Arrays.asList(mData)) { |
| 74 | + @Override |
| 75 | + public View getView(int position) { |
| 76 | + View item = mLayoutInflater.inflate(R.layout.special_item, null); |
| 77 | + TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag); |
| 78 | + tvAttrTag.setText(mData[position]); |
| 79 | + return item; |
| 80 | + } |
| 81 | + }); |
| 82 | +``` |
| 83 | +与ListView,GridView使用方式一样,实现FlowAdapter即可。 |
0 commit comments