├── .gitignore ├── FlowTagLayout ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jet │ │ └── flowtaglayout │ │ ├── FlowTagLayout.java │ │ └── Utils.java │ └── res │ ├── layout │ └── item_tag.xml │ └── values │ ├── attrs.xml │ └── strings.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jet │ │ └── flowtaglayoutdemo │ │ └── MainActivity.java │ └── res │ ├── drawable-v21 │ └── ripple_gray.xml │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ ├── ic_launcher_background.xml │ └── ripple_gray.xml │ ├── layout │ ├── activity_main.xml │ └── item_tag.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── attrs.xml │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /FlowTagLayout/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /FlowTagLayout/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | apply plugin: 'com.jfrog.bintray' 4 | apply plugin: 'com.github.dcendents.android-maven' 5 | 6 | def siteUrl = 'https://github.com/jetLee92/FlowTagLayoutDemo' // 项目的主页 7 | def gitUrl = 'git@github.com:jetLee92/FlowTagLayoutDemo.git' // Git仓库的url 8 | 9 | version = "1.0.1" 10 | group = "com.jetlee" 11 | Properties properties = new Properties() 12 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 13 | 14 | android { 15 | compileSdkVersion 27 16 | 17 | defaultConfig { 18 | minSdkVersion 15 19 | targetSdkVersion 27 20 | versionCode 1 21 | versionName "1.0" 22 | 23 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 24 | 25 | } 26 | 27 | buildTypes { 28 | release { 29 | minifyEnabled false 30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 31 | } 32 | } 33 | 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | 39 | implementation 'com.android.support:appcompat-v7:27.1.1' 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 42 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 43 | } 44 | 45 | 46 | bintray { 47 | user = properties.getProperty("bintray.user") 48 | key = properties.getProperty("bintray.apikey") 49 | pkg { 50 | repo = 'FlowTagLayout'//自己bintray创建仓库名字 51 | name = 'FlowTagLayout'//Library的名字 52 | websiteUrl = siteUrl 53 | vcsUrl = gitUrl 54 | licenses = ['MIT']//不能随便写,只能是仓库创建时选择的license type 55 | userOrg = 'jetlee' //自己bintray创建的organization名称 56 | publish = true // 是否是公开项目。 57 | 58 | version { 59 | name = '1.0.1' 60 | desc = 'High integrated development framework for Android applications.' 61 | released = new Date() 62 | vcsTag = 'v1.0.1' 63 | attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin'] 64 | } 65 | } 66 | configurations = ['archives'] 67 | } 68 | 69 | install { 70 | repositories.mavenInstaller { 71 | // This generates POM.xml with proper parameters 72 | pom { 73 | project { 74 | packaging 'aar' 75 | // Add your description here 76 | name 'FlowTagLayout Android' 77 | description 'High integrated development framework for Android applications.' 78 | url siteUrl 79 | // Set your license 80 | licenses { 81 | license { 82 | name 'MIT' 83 | url 'https://github.com/jetLee92/FlowTagLayoutDemo/blob/master/LICENSE' 84 | } 85 | } 86 | developers { 87 | developer { 88 | id 'jetLee92' //填写bintray或者github的用户名 89 | name 'Jet啟思' //姓名,可以是中文 90 | email 'liqisi92@gmail.com' 91 | } 92 | } 93 | scm { 94 | connection gitUrl 95 | developerConnection gitUrl 96 | url siteUrl 97 | } 98 | } 99 | } 100 | } 101 | } 102 | 103 | task sourcesJar(type: Jar) { 104 | from android.sourceSets.main.java.srcDirs 105 | classifier = 'sources' 106 | } 107 | task javadoc(type: Javadoc) { 108 | failOnError false //必须添加以免出错 109 | source = android.sourceSets.main.java.srcDirs 110 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 111 | } 112 | task javadocJar(type: Jar, dependsOn: javadoc) { 113 | classifier = 'javadoc' 114 | from javadoc.destinationDir 115 | } 116 | artifacts { 117 | archives javadocJar 118 | archives sourcesJar 119 | } 120 | -------------------------------------------------------------------------------- /FlowTagLayout/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/java/com/jet/flowtaglayout/FlowTagLayout.java: -------------------------------------------------------------------------------- 1 | package com.jet.flowtaglayout; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.res.TypedArray; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.Rect; 9 | import android.util.AttributeSet; 10 | import android.util.Log; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.LinearLayout; 15 | import android.widget.TextView; 16 | 17 | import java.util.ArrayList; 18 | import java.util.Arrays; 19 | import java.util.Collections; 20 | import java.util.List; 21 | 22 | /** 23 | * @author:Jet啟思 24 | * @date:2018/8/7 11:44 25 | */ 26 | public class FlowTagLayout extends ViewGroup { 27 | 28 | private Context context; 29 | private Rect[] childrenBounds; 30 | 31 | private List tagList; 32 | private OnTagClickListener onTagClickListener; 33 | 34 | private int defaultColor = Color.parseColor("#666666"); 35 | private float defaultTextSize = Utils.spToPx(16); 36 | 37 | /****************** 自定义的Attribute *************************/ 38 | private int leftMargin; 39 | private int rightMargin; 40 | private int topMargin; 41 | private int bottomMargin; 42 | private int leftPadding; 43 | private int rightPadding; 44 | private int topPadding; 45 | private int bottomPadding; 46 | private int background; 47 | private int textColor; 48 | private float textSize; 49 | 50 | public FlowTagLayout(Context context) { 51 | super(context); 52 | this.context = context; 53 | } 54 | 55 | public FlowTagLayout(Context context, AttributeSet attrs) { 56 | super(context, attrs); 57 | this.context = context; 58 | init(attrs); 59 | } 60 | 61 | public FlowTagLayout(Context context, AttributeSet attrs, int defStyleAttr) { 62 | super(context, attrs, defStyleAttr); 63 | this.context = context; 64 | init(attrs); 65 | } 66 | 67 | private void init(AttributeSet attrs) { 68 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlowTagLayout); 69 | leftMargin = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_leftMargin, 0); 70 | rightMargin = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_rightMargin, 0); 71 | topMargin = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_topMargin, 0); 72 | bottomMargin = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_bottomMargin, 0); 73 | leftPadding = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_leftPadding, 0); 74 | rightPadding = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_rightPadding, 0); 75 | topPadding = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_topPadding, 0); 76 | bottomPadding = (int) typedArray.getDimension(R.styleable.FlowTagLayout_item_bottomPadding, 0); 77 | background = typedArray.getResourceId(R.styleable.FlowTagLayout_item_background, -1); 78 | textColor = typedArray.getColor(R.styleable.FlowTagLayout_item_textColor, defaultColor); 79 | textSize = typedArray.getDimension(R.styleable.FlowTagLayout_item_textSize, defaultTextSize); 80 | typedArray.recycle(); 81 | } 82 | 83 | @Override 84 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 85 | // 已用宽度 86 | int widthUsed = getPaddingLeft(); 87 | // 已用高度 88 | int heightUsed = getPaddingTop(); 89 | // 当前行高度 90 | int lineHeight = 0; 91 | // 当前行宽度 92 | int lineWidth = 0; 93 | // 本身的宽度 94 | int parentWidth = 0; 95 | // 本身的高度 96 | int parentHeight = 0; 97 | 98 | int specWidth = MeasureSpec.getSize(widthMeasureSpec); 99 | int specWidthMode = MeasureSpec.getMode(widthMeasureSpec); 100 | int specHeight = MeasureSpec.getSize(heightMeasureSpec); 101 | int specHeightMode = MeasureSpec.getMode(heightMeasureSpec); 102 | 103 | int childCount = getChildCount(); 104 | if (childrenBounds == null) { 105 | childrenBounds = new Rect[childCount]; 106 | } else if (childrenBounds.length < childCount) { 107 | childrenBounds = Arrays.copyOf(childrenBounds, childCount); 108 | } 109 | 110 | // 遍历测量子view 111 | for (int i = 0; i < childCount; i++) { 112 | View child = getChildAt(i); 113 | // 测量子view 114 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); 115 | MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); 116 | // 判断是否换行(1.不是非限制 2.已用宽度+当前添加的child宽度+左右的padding 是否大于 父view自身测量的宽度) 117 | int paddingRight = getPaddingRight(); 118 | Log.e("20000", paddingRight + ":padding"); 119 | Log.e("20000", lp.leftMargin + ":margin"); 120 | Log.e("20000", child.getMeasuredWidth() + ":width"); 121 | widthUsed = widthUsed + leftMargin; 122 | if (specWidthMode != MeasureSpec.UNSPECIFIED && 123 | widthUsed + child.getMeasuredWidth() + rightMargin + getPaddingRight() > specWidth) { 124 | parentWidth = specWidth; 125 | widthUsed = getPaddingLeft() + leftMargin; 126 | // 已用高度 = 之前已用高度 + 当前行的高度 127 | heightUsed = heightUsed + lineHeight; 128 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); 129 | } 130 | 131 | Rect childBounds = childrenBounds[i]; 132 | if (childBounds == null) { 133 | childBounds = childrenBounds[i] = new Rect(); 134 | } 135 | 136 | lineHeight = child.getMeasuredHeight() + topMargin + bottomMargin; 137 | 138 | // 保存child的位置 139 | childBounds.set(widthUsed, heightUsed + topMargin, 140 | widthUsed + child.getMeasuredWidth(), heightUsed + lineHeight - bottomMargin); 141 | // 当前行已用的宽度 142 | widthUsed = widthUsed + child.getMeasuredWidth() + rightMargin; 143 | } 144 | // 因为换行parentWidth会被赋值,所以需要对比是否换行过赋值后的parentWidth 145 | parentWidth = Math.max(parentWidth, widthUsed + getPaddingRight()); 146 | // 父view的高度 = 已用高度 + 当前行高度 + padding 147 | parentHeight = heightUsed + lineHeight + getPaddingBottom(); 148 | 149 | setMeasuredDimension(resolveSizeAndState(parentWidth, widthMeasureSpec, 0), 150 | resolveSizeAndState(parentHeight, heightMeasureSpec, 0)); 151 | } 152 | 153 | @Override 154 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 155 | for (int i = 0; i < getChildCount(); i++) { 156 | View view = getChildAt(i); 157 | Rect childRect = childrenBounds[i]; 158 | view.layout(childRect.left, childRect.top, childRect.right, childRect.bottom); 159 | } 160 | } 161 | 162 | @Override 163 | protected void onDraw(Canvas canvas) { 164 | super.onDraw(canvas); 165 | } 166 | 167 | /** 168 | * 必须重写这个方法,不然measure的时候child.getLayoutParams()的margin是为0的 169 | * 170 | * @param attrs 171 | * @return 172 | */ 173 | @Override 174 | public LayoutParams generateLayoutParams(AttributeSet attrs) { 175 | return new MarginLayoutParams(getContext(), attrs); 176 | } 177 | 178 | @Override 179 | protected LayoutParams generateDefaultLayoutParams() { 180 | return new MarginLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 181 | } 182 | 183 | /** 184 | * 添加tags列表 185 | * 186 | * @param list 187 | */ 188 | public void addTags(List list) { 189 | if (tagList == null) { 190 | tagList = Collections.synchronizedList(new ArrayList()); 191 | } else { 192 | tagList.clear(); 193 | } 194 | tagList.addAll(list); 195 | loadTagView(); 196 | } 197 | 198 | /** 199 | * 初始化tags 200 | */ 201 | private void loadTagView() { 202 | removeAllViews(); 203 | for (int i = 0; i < tagList.size(); i++) { 204 | setTagContent(i); 205 | } 206 | } 207 | 208 | /** 209 | * 添加到尾部 210 | * 211 | * @param label 212 | */ 213 | public void addTag(String label) { 214 | tagList.add(label); 215 | setTagContent(tagList.size() - 1); 216 | } 217 | 218 | /** 219 | * 添加tag到index位置 220 | * 221 | * @param index 222 | * @param label 223 | */ 224 | public void addTagOfIndex(int index, String label) { 225 | tagList.add(index, label); 226 | setTagOfIndex(index); 227 | } 228 | 229 | /** 230 | * 初始化item 231 | * 232 | * @param i 233 | */ 234 | @SuppressLint("NewApi") 235 | private void setTagContent(int i) { 236 | LayoutInflater.from(context).inflate(R.layout.item_tag, this); 237 | // 这里用是getChildAt(index)获取子view,不能直接拿上面inflate的view 238 | TextView textView = getChildAt(i).findViewById(R.id.item_text); 239 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 240 | lp.setMargins(leftPadding, topPadding, rightPadding, bottomPadding); 241 | textView.setLayoutParams(lp); 242 | textView.setText(tagList.get(i)); 243 | textView.setTextColor(textColor); 244 | textView.setTextSize(Utils.pxToSp(textSize)); 245 | final int finalI = i; 246 | if (background == -1) { 247 | getChildAt(i).setBackground(null); 248 | } else { 249 | getChildAt(i).setBackground(getResources().getDrawable(background)); 250 | } 251 | getChildAt(i).setOnClickListener(new OnClickListener() { 252 | @Override 253 | public void onClick(View v) { 254 | if (onTagClickListener != null) { 255 | onTagClickListener.tagClick(finalI); 256 | } 257 | } 258 | }); 259 | } 260 | 261 | /** 262 | * 添加到index位置,使用的是addView(view ,index),最后需要把点击事件重置 263 | * 264 | * @param i 265 | */ 266 | @SuppressLint("NewApi") 267 | private void setTagOfIndex(int i) { 268 | View view = LayoutInflater.from(context).inflate(R.layout.item_tag, null, false); 269 | addView(view, i); 270 | // 这里用是getChildAt(index)获取子view,不能直接拿上面inflate的view 271 | TextView textView = getChildAt(i).findViewById(R.id.item_text); 272 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 273 | lp.setMargins(leftPadding, topPadding, rightPadding, bottomPadding); 274 | textView.setLayoutParams(lp); 275 | textView.setText(tagList.get(i)); 276 | textView.setTextColor(textColor); 277 | textView.setTextSize(Utils.pxToSp(textSize)); 278 | 279 | if (background == -1) { 280 | getChildAt(i).setBackground(null); 281 | } else { 282 | getChildAt(i).setBackground(getResources().getDrawable(background)); 283 | } 284 | for (int j = 0; j < getChildCount(); j++) { 285 | // 去掉点击事件 286 | getChildAt(j).setOnClickListener(null); 287 | final int finalJ = j; 288 | // 重新绑定点击监听 289 | getChildAt(j).setOnClickListener(new OnClickListener() { 290 | @Override 291 | public void onClick(View v) { 292 | if (onTagClickListener != null) { 293 | onTagClickListener.tagClick(finalJ); 294 | } 295 | } 296 | }); 297 | } 298 | } 299 | 300 | /** 301 | * 移除最后一个tag 302 | */ 303 | public void removeTag() { 304 | getChildAt(tagList.size() - 1).setOnClickListener(null); 305 | removeViewAt(tagList.size() - 1); 306 | tagList.remove(tagList.size() - 1); 307 | } 308 | 309 | /** 310 | * 移除index位置的tag 311 | * 312 | * @param index 313 | */ 314 | public void removeTagOfIndex(int index) { 315 | getChildAt(index).setOnClickListener(null); 316 | removeViewAt(index); 317 | tagList.remove(index); 318 | for (int j = index; j < getChildCount(); j++) { 319 | // 去掉点击事件 320 | getChildAt(j).setOnClickListener(null); 321 | final int finalJ = j; 322 | // 重新绑定点击监听 323 | getChildAt(j).setOnClickListener(new OnClickListener() { 324 | @Override 325 | public void onClick(View v) { 326 | if (onTagClickListener != null) { 327 | onTagClickListener.tagClick(finalJ); 328 | } 329 | } 330 | }); 331 | } 332 | } 333 | 334 | public void setTagClickListener(OnTagClickListener onTagClickListener) { 335 | this.onTagClickListener = onTagClickListener; 336 | } 337 | 338 | public interface OnTagClickListener { 339 | void tagClick(int position); 340 | } 341 | 342 | } 343 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/java/com/jet/flowtaglayout/Utils.java: -------------------------------------------------------------------------------- 1 | package com.jet.flowtaglayout; 2 | 3 | import android.content.res.Resources; 4 | import android.util.TypedValue; 5 | 6 | /** 7 | * @Author:Jet啟思 8 | * @Date:2018/7/24 15:07 9 | */ 10 | public class Utils { 11 | 12 | public static float dpToPx(float dp) { 13 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics()); 14 | } 15 | 16 | public static float spToPx(float sp) { 17 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, Resources.getSystem().getDisplayMetrics()); 18 | } 19 | 20 | public static float pxToSp(float px) { 21 | final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; 22 | return (int) (px / fontScale + 0.5f); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/res/layout/item_tag.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /FlowTagLayout/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | FlowTagLayout 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jet啟思 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FlowTagLayoutDemo 2 | 3 | #### 为什么写这个库? 4 | 5 | 项目中有时候会有这样的需求,比如*兴趣爱好的选择、搜索历史标签*等,自带的Android控件实现起来贼麻烦,所以自己撸一个呗,当然前提是这个库开发者必须用得简洁高效,而且功能到位。 6 | 7 | #### 特点是什么? 8 | 9 | 1、最基本的功能,标签布局并且自动换行。 10 | 11 | 2、自定义了各种styleable,在xml布局文件就可以设置tag的属性。 12 | 13 | 3、可以单选、多选,这个全有开发者自由选择,只要自己加background就完事。 14 | 15 | 4、最后一点,简洁高效。 16 | 17 | #### 如何使用? 18 | 19 | 对,在使用之前看看效果,来,上效果图 20 | 21 | ![效果图](https://upload-images.jianshu.io/upload_images/5596129-ca5481c8dadc17bb.gif?imageMogr2/auto-orient/strip) 22 | 23 | 24 | 首先,引入这个库 25 | 26 | 这是GitHub地址[自定义标签布局FloaTagLayout](https://github.com/jetLee92/FlowTagLayoutDemo),源码项目里面有,这篇文章暂不说该控件的原理,之后会出源码的分析。 27 | 28 | **gradle:** 29 | 30 | compile 'com.jetlee:FlowTagLayout:1.0.1' 31 | 32 | **Maven:** 33 | 34 | 35 | com.jetlee 36 | FlowTagLayout 37 | 1.0.1 38 | pom 39 | 40 | 41 | 把库依赖到项目中后,在布局中使用,并配置各种属性 42 | 43 | 59 | 60 | 下面列出FlowTagLayout的重要方法(注意,这些方法可以共用,也可以只用一种,看各自的需求) 61 | 62 | 先准备几条数据,都是tag的label 63 | 64 | List dataList = new ArrayList<>(); 65 | dataList.add("数据结构"); 66 | dataList.add("算法"); 67 | dataList.add("Java"); 68 | dataList.add("多线程编程"); 69 | 70 | // 添加tag 71 | flowTagLayout.addTags(dataList); // 添加tag的列表,该方法会把之前的tags全部清空 72 | flowTagLayout.addTag("Kotlin"); // 在尾部添加一个tag 73 | flowTagLayout.addTagOfIndex(2,"自定义view"); // 在指定的位置插入tag,示例为在index为2,也就是第三个位置插入tag 74 | 75 | // 移除tag 76 | flowTagLayout.removeTag(); // 移除尾部tag 77 | flowTagLayout.removeTagOfIndex(3); // 移除指定位置的tag,示例为移除index为3,也就是第四个tag移除 78 | 79 | 最后就是绑定点击事件 80 | 81 | flowTagLayout.setTagClickListener(new FlowTagLayout.OnTagClickListener() { 82 | @Override 83 | public void tagClick(int position) { 84 | // getChildAt(position)方法在这很实用 85 | flowTagLayout.getChildAt(position).setSelected(!flowTagLayout.getChildAt(position).isSelected()); 86 | } 87 | }); 88 | 89 | 90 | **这个库的使用就是这么简单,喜欢的给个star呗,哈哈哈。** 91 | 92 | 93 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.jet.flowtaglayoutdemo" 7 | minSdkVersion 15 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | // implementation project(':FlowTagLayout') 30 | implementation 'com.jetlee:FlowTagLayout:1.0.1' 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/jet/flowtaglayoutdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jet.flowtaglayoutdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.EditText; 7 | import android.widget.Toast; 8 | 9 | import com.jet.flowtaglayout.FlowTagLayout; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | private FlowTagLayout flowTagLayout; 17 | private List dataList; 18 | private EditText editText; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | 25 | dataList = new ArrayList<>(); 26 | 27 | flowTagLayout = findViewById(R.id.flowTagLayout); 28 | editText = findViewById(R.id.edit); 29 | 30 | setDatas(); 31 | flowTagLayout.addTags(dataList); 32 | 33 | flowTagLayout.setTagClickListener(new FlowTagLayout.OnTagClickListener() { 34 | @Override 35 | public void tagClick(int position) { 36 | flowTagLayout.getChildAt(position).setSelected(!flowTagLayout.getChildAt(position).isSelected()); 37 | Toast.makeText(MainActivity.this, dataList.get(position), Toast.LENGTH_SHORT).show(); 38 | } 39 | }); 40 | 41 | findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View v) { 44 | flowTagLayout.addTag(editText.getText().toString()); 45 | dataList.add(editText.getText().toString()); 46 | Toast.makeText(MainActivity.this, "添加了“" + editText.getText().toString() + "”", Toast.LENGTH_SHORT).show(); 47 | } 48 | }); 49 | findViewById(R.id.remove).setOnClickListener(new View.OnClickListener() { 50 | @Override 51 | public void onClick(View v) { 52 | flowTagLayout.removeTag(); 53 | Toast.makeText(MainActivity.this, "移除了“" + dataList.get(dataList.size() - 1) + "”", Toast.LENGTH_SHORT).show(); 54 | dataList.remove(dataList.size() - 1); 55 | } 56 | }); 57 | 58 | } 59 | 60 | private void setDatas() { 61 | dataList.add("数据结构"); 62 | dataList.add("算法"); 63 | dataList.add("多线程编程"); 64 | dataList.add("JVM"); 65 | dataList.add("自定义view"); 66 | dataList.add("TCP/IP"); 67 | dataList.add("gradle强化"); 68 | dataList.add("设计模式"); 69 | dataList.add("git"); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ripple_gray.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ripple_gray.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 25 | 26 | 31 | 32 |