Refactor, ability to use additional spacing with auto = true

This commit is contained in:
androiddevkotlin 2021-03-06 00:56:37 +02:00
parent cf8346187f
commit d4c6e92a34
4 changed files with 60 additions and 62 deletions

View file

@ -6,19 +6,29 @@
<net.osmand.plus.widgets.FlowLayout
android:id="@+id/select_custom_color"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/list_content_padding"
android:layout_marginLeft="@dimen/list_content_padding"
android:layout_marginEnd="@dimen/list_content_padding"
android:layout_marginRight="@dimen/list_content_padding" />
<include
layout="@layout/simple_divider_item"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="@dimen/list_content_padding"
android:layout_marginLeft="@dimen/list_content_padding"
android:layout_marginTop="@dimen/context_menu_padding_margin_tiny"
android:layout_marginBottom="@dimen/content_padding_half" />
<net.osmand.plus.widgets.FlowLayout
android:id="@+id/select_default_color"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/list_content_padding"
android:layout_marginLeft="@dimen/list_content_padding"
android:layout_marginEnd="@dimen/list_content_padding"
android:layout_marginRight="@dimen/list_content_padding" />
</LinearLayout>

View file

@ -453,11 +453,9 @@
android:id="@+id/select_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding_small"
android:layout_marginTop="@dimen/context_menu_padding_margin_tiny"
android:layout_marginBottom="@dimen/content_padding_half"
android:orientation="horizontal"
android:layout_marginStart="@dimen/content_padding_small" />
android:orientation="horizontal" />
<View
android:layout_width="match_parent"

View file

@ -645,7 +645,8 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment implemen
horizontalSelectionAdapter.notifyDataSetChanged();
iconCategoriesRecyclerView.smoothScrollToPosition(horizontalSelectionAdapter.getItemPositionByTitle(selectedIconCategory));
for (String name : iconNameList) {
selectIcon.addView(createIconItemView(name, selectIcon), new FlowLayout.LayoutParams(0, 0));
int minimalPaddingBetweenIcon = app.getResources().getDimensionPixelSize(R.dimen.favorites_select_icon_button_right_padding);
selectIcon.addView(createIconItemView(name, selectIcon), new FlowLayout.LayoutParams(minimalPaddingBetweenIcon, 0));
selectIcon.setHorizontalAutoSpacing(true);
}
}

View file

@ -12,22 +12,6 @@ public class FlowLayout extends ViewGroup {
private int line_height;
private boolean horizontalAutoSpacing;
public static class LayoutParams extends ViewGroup.LayoutParams {
final int horizontalSpacing;
final int verticalSpacing;
/**
* @param horizontalSpacing Pixels between items, horizontally
* @param verticalSpacing Pixels between items, vertically
*/
public LayoutParams(int horizontalSpacing, int verticalSpacing) {
super(0, 0);
this.horizontalSpacing = horizontalSpacing;
this.verticalSpacing = verticalSpacing;
}
}
public FlowLayout(Context context) {
super(context);
}
@ -36,7 +20,7 @@ public class FlowLayout extends ViewGroup {
super(context, attrs);
}
// If true available horizontal space is added to items horizontalSpacing.
// If true, available horizontal space is added to items horizontalSpacing to fit the screen width.
public void setHorizontalAutoSpacing(boolean horizontalAutoSpacing) {
this.horizontalAutoSpacing = horizontalAutoSpacing;
}
@ -100,56 +84,61 @@ public class FlowLayout extends ViewGroup {
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
final int width = r - l;
int freeSizeSpacing;
boolean isLayoutRtl = AndroidUtils.isLayoutRtl(getContext());
int horizontalPosition = isLayoutRtl ? width - getPaddingRight() : getPaddingLeft();
int verticalPosition = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
int itemsCount = width / childWidth;
if (itemsCount > 1) {
freeSizeSpacing = width % childWidth / (itemsCount - 1);
} else {
freeSizeSpacing = width % childWidth / itemsCount;
}
if (horizontalAutoSpacing) {
int freeSizeSpacing = getFreeSizeSpacing(width, lp, childWidth);
if (isLayoutRtl) {
if (horizontalPosition - childWidth < getPaddingLeft()) {
horizontalPosition = width - getPaddingRight();
verticalPosition += line_height;
}
child.layout(horizontalPosition - childWidth, verticalPosition, horizontalPosition, verticalPosition + childHeight);
horizontalPosition -= childWidth + lp.horizontalSpacing + freeSizeSpacing;
horizontalPosition -= childWidth + freeSizeSpacing;
} else {
if (horizontalPosition + childWidth > width) {
horizontalPosition = getPaddingLeft();
verticalPosition += line_height;
}
child.layout(horizontalPosition, verticalPosition, horizontalPosition + childWidth, verticalPosition + childHeight);
horizontalPosition += childWidth + lp.horizontalSpacing + freeSizeSpacing;
horizontalPosition += childWidth + freeSizeSpacing;
}
}
}
}
private int getFreeSizeSpacing(int width, LayoutParams lp, int childWidth) {
int freeSizeSpacing;
int itemsCount = width / (childWidth + lp.horizontalSpacing);
if (itemsCount > 1 && horizontalAutoSpacing) {
freeSizeSpacing = (width % childWidth / (itemsCount - 1)) + lp.horizontalSpacing;
} else if (!horizontalAutoSpacing) {
freeSizeSpacing = lp.horizontalSpacing;
} else {
if (isLayoutRtl) {
if (horizontalPosition - childWidth < l) {
horizontalPosition = width - getPaddingRight();
verticalPosition += line_height;
}
child.layout(horizontalPosition - childWidth, verticalPosition, horizontalPosition, verticalPosition + childHeight);
horizontalPosition -= childWidth + lp.horizontalSpacing;
} else {
if (horizontalPosition + childWidth > width) {
horizontalPosition = getPaddingLeft();
verticalPosition += line_height;
}
child.layout(horizontalPosition, verticalPosition, horizontalPosition + childWidth, verticalPosition + childHeight);
horizontalPosition += childWidth + lp.horizontalSpacing;
}
freeSizeSpacing = (width % childWidth / itemsCount) + lp.horizontalSpacing;
}
return freeSizeSpacing;
}
public static class LayoutParams extends ViewGroup.LayoutParams {
final int horizontalSpacing;
final int verticalSpacing;
/**
* @param horizontalSpacing Pixels between items, horizontally
* @param verticalSpacing Pixels between items, vertically
*/
public LayoutParams(int horizontalSpacing, int verticalSpacing) {
super(0, 0);
this.horizontalSpacing = horizontalSpacing;
this.verticalSpacing = verticalSpacing;
}
}
}