Fix hideItem radius bug

Optimize layout code
This commit is contained in:
2022-03-21 19:17:38 +08:00
parent dc3beae036
commit e05806f335
4 changed files with 20 additions and 23 deletions

View File

@@ -26,41 +26,36 @@ public class AutoLinefeedLayout extends ViewGroup {
private void layoutHorizontal() { private void layoutHorizontal() {
final int count = getChildCount(); final int count = getChildCount();
final int lineWidth = getMeasuredWidth() - getPaddingLeft() final int lineWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
- getPaddingRight();
int paddingTop = getPaddingTop(); int paddingTop = getPaddingTop();
int childTop = 0; int childTop;
int childLeft = getPaddingLeft(); int childLeft = getPaddingLeft();
int availableLineWidth = lineWidth; int availableLineWidth = lineWidth;
int maxLineHight = 0; int maxLineHeight = 0;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
final View child = getChildAt(i); final View child = getChildAt(i);
if (child == null) { if (child != null && child.getVisibility() != GONE) {
continue;
} else if (child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth(); final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight(); final int childHeight = child.getMeasuredHeight();
if (availableLineWidth < childWidth) { if (availableLineWidth < childWidth) {
availableLineWidth = lineWidth; availableLineWidth = lineWidth;
paddingTop = paddingTop + maxLineHight; paddingTop = paddingTop + maxLineHeight;
childLeft = getPaddingLeft(); childLeft = getPaddingLeft();
maxLineHight = 0; maxLineHeight = 0;
} }
childTop = paddingTop; childTop = paddingTop;
setChildFrame(child, childLeft, childTop, childWidth, setChildFrame(child, childLeft, childTop, childWidth, childHeight);
childHeight);
childLeft += childWidth; childLeft += childWidth;
availableLineWidth = availableLineWidth - childWidth; availableLineWidth = availableLineWidth - childWidth;
maxLineHight = Math.max(maxLineHight, childHeight); maxLineHeight = Math.max(maxLineHeight, childHeight);
} }
} }
} }
private void setChildFrame(View child, int left, int top, int width, private void setChildFrame(View child, int left, int top, int width, int height) {
int height) {
child.layout(left, top, left + width, top + height); child.layout(left, top, left + width, top + height);
} }
@@ -73,8 +68,7 @@ public class AutoLinefeedLayout extends ViewGroup {
} }
if (heightMode == MeasureSpec.AT_MOST||heightMode == MeasureSpec.UNSPECIFIED) { if (heightMode == MeasureSpec.AT_MOST||heightMode == MeasureSpec.UNSPECIFIED) {
final int width = MeasureSpec.getSize(widthMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec( super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(getDesiredHeight(width), MeasureSpec.EXACTLY));
getDesiredHeight(width), MeasureSpec.EXACTLY));
} else { } else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} }

View File

@@ -60,8 +60,6 @@ public class FoldLayout extends LinearLayout implements View.OnClickListener{
content = new LinearLayout(context); content = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT); ViewGroup.LayoutParams.WRAP_CONTENT);
/*content.setShowDividers(SHOW_DIVIDER_BEGINNING|SHOW_DIVIDER_MIDDLE);
content.setDividerDrawable(ContextCompat.getDrawable(context,R.drawable.item_divider));*/
content.setOrientation(VERTICAL); content.setOrientation(VERTICAL);
addView(content,layoutParams); addView(content,layoutParams);
} }
@@ -95,7 +93,7 @@ public class FoldLayout extends LinearLayout implements View.OnClickListener{
content.setLayoutParams(layoutParams); content.setLayoutParams(layoutParams);
}); });
showAnimator.addListener(new AnimatorListenerAdapter() { showAnimator.addListener(new AnimatorListenerAdapter() {
@Override @SuppressLint("UseCompatLoadingForDrawables")
public void onAnimationStart(Animator animation) { public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation); super.onAnimationStart(animation);
LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout); LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout);
@@ -110,11 +108,14 @@ public class FoldLayout extends LinearLayout implements View.OnClickListener{
content.setLayoutParams(layoutParams); content.setLayoutParams(layoutParams);
}); });
hideAnimator.addListener(new AnimatorListenerAdapter() { hideAnimator.addListener(new AnimatorListenerAdapter() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override @Override
public void onAnimationEnd(Animator animation) { public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation); super.onAnimationEnd(animation);
LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout); if (!isShow) {
linearLayout.setBackground(getResources().getDrawable(R.drawable.top_bottom_radius_background)); LinearLayout linearLayout = defaultView.findViewById(R.id.foldLayoutLinearLayout);
linearLayout.setBackground(getResources().getDrawable(R.drawable.top_bottom_radius_background));
}
} }
}); });
init = true; init = true;
@@ -148,6 +149,7 @@ public class FoldLayout extends LinearLayout implements View.OnClickListener{
content.setLayoutParams(layoutParams); content.setLayoutParams(layoutParams);
} }
@SuppressLint("UseCompatLoadingForDrawables")
public void showItem() { public void showItem() {
isShow = true; isShow = true;
showAnimator.start(); showAnimator.start();
@@ -157,6 +159,7 @@ public class FoldLayout extends LinearLayout implements View.OnClickListener{
animatedVectorDrawable.start(); animatedVectorDrawable.start();
} }
@SuppressLint("UseCompatLoadingForDrawables")
public void hideItem() { public void hideItem() {
isShow = false; isShow = false;
hideAnimator.start(); hideAnimator.start();

View File

@@ -3,7 +3,7 @@
<style name="Theme.OxygenToolbox" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge"> <style name="Theme.OxygenToolbox" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. --> <!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item> <item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorPrimaryVariant">@color/purple_200</item>
<item name="colorOnPrimary">@color/black</item> <item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. --> <!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal_200</item>

View File

@@ -3,7 +3,7 @@
<style name="Theme.OxygenToolbox" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge"> <style name="Theme.OxygenToolbox" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. --> <!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item> <item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorPrimaryVariant">@color/purple_500</item>
<item name="colorOnPrimary">@color/white</item> <item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. --> <!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal_200</item>