From b5eeef50acdffad1b785cf701394f5a3b76f8415 Mon Sep 17 00:00:00 2001 From: Jackzhou Date: Sun, 21 Jul 2019 00:18:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=80=E4=BD=8E?= =?UTF-8?q?=E7=89=88=E6=9C=AC14=E7=BC=96=E8=AF=91=E7=89=88=E6=9C=AC29?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9Support=E4=B8=BAAndroidX=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9gradle=E4=B8=BA5.1.1=E5=92=8C3.4.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .idea/gradle.xml | 3 + .idea/modules.xml | 6 +- build.gradle | 14 +- flowlayout-lib/build.gradle | 30 +- flowlayout-lib/flowlayout-lib.iml | 99 ++-- .../com/zhy/view/flowlayout/FlowLayout.java | 431 +++++++++--------- flowlayout/build.gradle | 19 +- flowlayout/flowlayout.iml | 129 ++++-- .../com/zhy/flowlayout/CategoryActivity.java | 12 +- .../com/zhy/flowlayout/EventTestFragment.java | 5 +- .../com/zhy/flowlayout/GravityFragment.java | 5 +- .../zhy/flowlayout/LimitSelectedFragment.java | 5 +- .../zhy/flowlayout/ListViewTestFragment.java | 5 +- .../flowlayout/ScrollViewTestFragment.java | 5 +- .../com/zhy/flowlayout/SimpleFragment.java | 5 +- .../zhy/flowlayout/SingleChooseFragment.java | 5 +- .../src/main/res/layout/activity_category.xml | 4 +- gradle.properties | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 20 files changed, 428 insertions(+), 363 deletions(-) diff --git a/.gitignore b/.gitignore index 31df7f6..9170674 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .gradle +.idea /local.properties /.idea/workspace.xml /.idea/libraries diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 25e8f3d..ced0a1d 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,6 +3,9 @@ - - + + - - - - + + + - - - - + + + + @@ -56,6 +56,13 @@ + + + + + + + @@ -63,13 +70,6 @@ - - - - - - - @@ -77,39 +77,50 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + - - + + + - - + - - - - - - - + + + + + + + \ No newline at end of file diff --git a/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/FlowLayout.java b/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/FlowLayout.java index 57850b5..21721fb 100755 --- a/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/FlowLayout.java +++ b/flowlayout-lib/src/main/java/com/zhy/view/flowlayout/FlowLayout.java @@ -1,215 +1,216 @@ -package com.zhy.view.flowlayout; - -import android.content.Context; -import android.content.res.TypedArray; -import android.util.AttributeSet; -import android.util.LayoutDirection; -import android.view.View; -import android.view.ViewGroup; -import android.support.v4.text.TextUtilsCompat; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; - -public class FlowLayout extends ViewGroup { - private static final String TAG = "FlowLayout"; - private static final int LEFT = -1; - private static final int CENTER = 0; - private static final int RIGHT = 1; - - protected List> mAllViews = new ArrayList>(); - protected List mLineHeight = new ArrayList(); - protected List mLineWidth = new ArrayList(); - private int mGravity; - private List lineViews = new ArrayList<>(); - - public FlowLayout(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout); - mGravity = ta.getInt(R.styleable.TagFlowLayout_tag_gravity, LEFT); - int layoutDirection = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()); - if (layoutDirection == LayoutDirection.RTL) { - if (mGravity == LEFT) { - mGravity = RIGHT; - } else { - mGravity = LEFT; - } - } - ta.recycle(); - } - - public FlowLayout(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } - - public FlowLayout(Context context) { - this(context, null); - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); - int modeWidth = MeasureSpec.getMode(widthMeasureSpec); - int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); - int modeHeight = MeasureSpec.getMode(heightMeasureSpec); - - // wrap_content - int width = 0; - int height = 0; - - int lineWidth = 0; - int lineHeight = 0; - - int cCount = getChildCount(); - - for (int i = 0; i < cCount; i++) { - View child = getChildAt(i); - if (child.getVisibility() == View.GONE) { - if (i == cCount - 1) { - width = Math.max(lineWidth, width); - height += lineHeight; - } - continue; - } - measureChild(child, widthMeasureSpec, heightMeasureSpec); - MarginLayoutParams lp = (MarginLayoutParams) child - .getLayoutParams(); - - int childWidth = child.getMeasuredWidth() + lp.leftMargin - + lp.rightMargin; - int childHeight = child.getMeasuredHeight() + lp.topMargin - + lp.bottomMargin; - - if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) { - width = Math.max(width, lineWidth); - lineWidth = childWidth; - height += lineHeight; - lineHeight = childHeight; - } else { - lineWidth += childWidth; - lineHeight = Math.max(lineHeight, childHeight); - } - if (i == cCount - 1) { - width = Math.max(lineWidth, width); - height += lineHeight; - } - } - setMeasuredDimension( - // - modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(), - modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()// - ); - - } - - - @Override - protected void onLayout(boolean changed, int l, int t, int r, int b) { - mAllViews.clear(); - mLineHeight.clear(); - mLineWidth.clear(); - lineViews.clear(); - - int width = getWidth(); - - int lineWidth = 0; - int lineHeight = 0; - - int cCount = getChildCount(); - - for (int i = 0; i < cCount; i++) { - View child = getChildAt(i); - if (child.getVisibility() == View.GONE) continue; - MarginLayoutParams lp = (MarginLayoutParams) child - .getLayoutParams(); - - int childWidth = child.getMeasuredWidth(); - int childHeight = child.getMeasuredHeight(); - - if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight()) { - mLineHeight.add(lineHeight); - mAllViews.add(lineViews); - mLineWidth.add(lineWidth); - - lineWidth = 0; - lineHeight = childHeight + lp.topMargin + lp.bottomMargin; - lineViews = new ArrayList(); - } - lineWidth += childWidth + lp.leftMargin + lp.rightMargin; - lineHeight = Math.max(lineHeight, childHeight + lp.topMargin - + lp.bottomMargin); - lineViews.add(child); - - } - mLineHeight.add(lineHeight); - mLineWidth.add(lineWidth); - mAllViews.add(lineViews); - - - int left = getPaddingLeft(); - int top = getPaddingTop(); - - int lineNum = mAllViews.size(); - - for (int i = 0; i < lineNum; i++) { - lineViews = mAllViews.get(i); - lineHeight = mLineHeight.get(i); - - // set gravity - int currentLineWidth = this.mLineWidth.get(i); - switch (this.mGravity) { - case LEFT: - left = getPaddingLeft(); - break; - case CENTER: - left = (width - currentLineWidth) / 2 + getPaddingLeft(); - break; - case RIGHT: - // 适配了rtl,需要补偿一个padding值 - left = width - (currentLineWidth + getPaddingLeft()) - getPaddingRight(); - // 适配了rtl,需要把lineViews里面的数组倒序排 - Collections.reverse(lineViews); - break; - } - - for (int j = 0; j < lineViews.size(); j++) { - View child = lineViews.get(j); - if (child.getVisibility() == View.GONE) { - continue; - } - - MarginLayoutParams lp = (MarginLayoutParams) child - .getLayoutParams(); - - int lc = left + lp.leftMargin; - int tc = top + lp.topMargin; - int rc = lc + child.getMeasuredWidth(); - int bc = tc + child.getMeasuredHeight(); - - child.layout(lc, tc, rc, bc); - - left += child.getMeasuredWidth() + lp.leftMargin - + lp.rightMargin; - } - top += lineHeight; - } - - } - - @Override - public LayoutParams generateLayoutParams(AttributeSet attrs) { - return new MarginLayoutParams(getContext(), attrs); - } - - @Override - protected LayoutParams generateDefaultLayoutParams() { - return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - } - - @Override - protected LayoutParams generateLayoutParams(LayoutParams p) { - return new MarginLayoutParams(p); - } -} +package com.zhy.view.flowlayout; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.util.LayoutDirection; +import android.view.View; +import android.view.ViewGroup; + +import androidx.core.text.TextUtilsCompat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +public class FlowLayout extends ViewGroup { + private static final String TAG = "FlowLayout"; + private static final int LEFT = -1; + private static final int CENTER = 0; + private static final int RIGHT = 1; + + protected List> mAllViews = new ArrayList>(); + protected List mLineHeight = new ArrayList(); + protected List mLineWidth = new ArrayList(); + private int mGravity; + private List lineViews = new ArrayList<>(); + + public FlowLayout(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout); + mGravity = ta.getInt(R.styleable.TagFlowLayout_tag_gravity, LEFT); + int layoutDirection = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()); + if (layoutDirection == LayoutDirection.RTL) { + if (mGravity == LEFT) { + mGravity = RIGHT; + } else { + mGravity = LEFT; + } + } + ta.recycle(); + } + + public FlowLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public FlowLayout(Context context) { + this(context, null); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); + int modeWidth = MeasureSpec.getMode(widthMeasureSpec); + int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); + int modeHeight = MeasureSpec.getMode(heightMeasureSpec); + + // wrap_content + int width = 0; + int height = 0; + + int lineWidth = 0; + int lineHeight = 0; + + int cCount = getChildCount(); + + for (int i = 0; i < cCount; i++) { + View child = getChildAt(i); + if (child.getVisibility() == View.GONE) { + if (i == cCount - 1) { + width = Math.max(lineWidth, width); + height += lineHeight; + } + continue; + } + measureChild(child, widthMeasureSpec, heightMeasureSpec); + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int childWidth = child.getMeasuredWidth() + lp.leftMargin + + lp.rightMargin; + int childHeight = child.getMeasuredHeight() + lp.topMargin + + lp.bottomMargin; + + if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) { + width = Math.max(width, lineWidth); + lineWidth = childWidth; + height += lineHeight; + lineHeight = childHeight; + } else { + lineWidth += childWidth; + lineHeight = Math.max(lineHeight, childHeight); + } + if (i == cCount - 1) { + width = Math.max(lineWidth, width); + height += lineHeight; + } + } + setMeasuredDimension( + // + modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(), + modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()// + ); + + } + + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + mAllViews.clear(); + mLineHeight.clear(); + mLineWidth.clear(); + lineViews.clear(); + + int width = getWidth(); + + int lineWidth = 0; + int lineHeight = 0; + + int cCount = getChildCount(); + + for (int i = 0; i < cCount; i++) { + View child = getChildAt(i); + if (child.getVisibility() == View.GONE) continue; + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int childWidth = child.getMeasuredWidth(); + int childHeight = child.getMeasuredHeight(); + + if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight()) { + mLineHeight.add(lineHeight); + mAllViews.add(lineViews); + mLineWidth.add(lineWidth); + + lineWidth = 0; + lineHeight = childHeight + lp.topMargin + lp.bottomMargin; + lineViews = new ArrayList(); + } + lineWidth += childWidth + lp.leftMargin + lp.rightMargin; + lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + + lp.bottomMargin); + lineViews.add(child); + + } + mLineHeight.add(lineHeight); + mLineWidth.add(lineWidth); + mAllViews.add(lineViews); + + + int left = getPaddingLeft(); + int top = getPaddingTop(); + + int lineNum = mAllViews.size(); + + for (int i = 0; i < lineNum; i++) { + lineViews = mAllViews.get(i); + lineHeight = mLineHeight.get(i); + + // set gravity + int currentLineWidth = this.mLineWidth.get(i); + switch (this.mGravity) { + case LEFT: + left = getPaddingLeft(); + break; + case CENTER: + left = (width - currentLineWidth) / 2 + getPaddingLeft(); + break; + case RIGHT: + // 适配了rtl,需要补偿一个padding值 + left = width - (currentLineWidth + getPaddingLeft()) - getPaddingRight(); + // 适配了rtl,需要把lineViews里面的数组倒序排 + Collections.reverse(lineViews); + break; + } + + for (int j = 0; j < lineViews.size(); j++) { + View child = lineViews.get(j); + if (child.getVisibility() == View.GONE) { + continue; + } + + MarginLayoutParams lp = (MarginLayoutParams) child + .getLayoutParams(); + + int lc = left + lp.leftMargin; + int tc = top + lp.topMargin; + int rc = lc + child.getMeasuredWidth(); + int bc = tc + child.getMeasuredHeight(); + + child.layout(lc, tc, rc, bc); + + left += child.getMeasuredWidth() + lp.leftMargin + + lp.rightMargin; + } + top += lineHeight; + } + + } + + @Override + public LayoutParams generateLayoutParams(AttributeSet attrs) { + return new MarginLayoutParams(getContext(), attrs); + } + + @Override + protected LayoutParams generateDefaultLayoutParams() { + return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + } + + @Override + protected LayoutParams generateLayoutParams(LayoutParams p) { + return new MarginLayoutParams(p); + } +} diff --git a/flowlayout/build.gradle b/flowlayout/build.gradle index 97325f6..47c2d25 100644 --- a/flowlayout/build.gradle +++ b/flowlayout/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion 29 + buildToolsVersion "29.0.1" defaultConfig { applicationId "com.zhy.flowlayout" - minSdkVersion 10 - targetSdkVersion 25 + minSdkVersion 14 + targetSdkVersion 29 versionCode 1 versionName "1.0" } @@ -20,9 +20,10 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:25.3.0' - compile project(':flowlayout-lib') - compile 'com.android.support:design:25.3.0' - compile 'com.zhy:base-adapter:2.0.1' + api fileTree(dir: 'libs', include: ['*.jar']) + api 'androidx.appcompat:appcompat:1.0.2' + api project(':flowlayout-lib') +// api 'androidx.appcompat:appcompat:1.0.2' + api 'com.zhy:base-adapter:2.0.1' + implementation 'com.google.android.material:material:1.0.0' } diff --git a/flowlayout/flowlayout.iml b/flowlayout/flowlayout.iml index 33086c4..fdca174 100644 --- a/flowlayout/flowlayout.iml +++ b/flowlayout/flowlayout.iml @@ -17,30 +17,30 @@