Skip to content

Commit

Permalink
feature: ItemWidthCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Dec 7, 2020
1 parent d460663 commit d524205
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Layout;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
Expand All @@ -32,25 +31,23 @@
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.layout.QMUIPriorityLinearLayout;
import com.qmuiteam.qmui.skin.QMUISkinLayoutInflaterFactory;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.LayoutInflaterCompat;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import static com.qmuiteam.qmui.layout.IQMUILayout.HIDE_RADIUS_SIDE_BOTTOM;

/**
Expand Down Expand Up @@ -498,6 +495,7 @@ public QMUIBottomSheetGridItemView create(@NonNull QMUIBottomSheet bottomSheet,
private ArrayList<QMUIBottomSheetGridItemModel> mSecondLineItems;
private ItemViewFactory mItemViewFactory = DEFAULT_ITEM_VIEW_FACTORY;
private OnSheetItemClickListener mOnSheetItemClickListener;
private QMUIBottomSheetGridLineLayout.ItemWidthCalculator mItemWidthCalculator = null;

public BottomGridSheetBuilder(Context context) {
super(context);
Expand Down Expand Up @@ -548,6 +546,11 @@ public BottomGridSheetBuilder setOnSheetItemClickListener(OnSheetItemClickListen
return this;
}

public BottomGridSheetBuilder setItemWidthCalculator(QMUIBottomSheetGridLineLayout.ItemWidthCalculator itemWidthCalculator) {
mItemWidthCalculator = itemWidthCalculator;
return this;
}

@Override
public void onClick(View v) {
if (mOnSheetItemClickListener != null) {
Expand Down Expand Up @@ -587,7 +590,7 @@ protected View onCreateContentView(@NonNull QMUIBottomSheet bottomSheet,
new LinearLayout.LayoutParams(wrapContent, wrapContent)));
}
}
return new QMUIBottomSheetGridLineLayout(mDialog, firstLines, secondLines);
return new QMUIBottomSheetGridLineLayout(mDialog, mItemWidthCalculator, firstLines, secondLines);
}

public interface OnSheetItemClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUIResHelper;

Expand All @@ -32,19 +34,47 @@

public class QMUIBottomSheetGridLineLayout extends LinearLayout {

private static ItemWidthCalculator DEFAULT_CALCULATOR = new ItemWidthCalculator() {
@Override
public int calculate(Context context, int width, int miniWidth, int itemCount, int paddingLeft, int paddingRight) {
final int parentSpacing = width - paddingLeft - paddingRight;
int itemWidth = miniWidth;
// there is no more space for the last one item. then stretch the item width
if (itemCount >= 3
&& parentSpacing - itemCount * itemWidth > 0
&& parentSpacing - itemCount * itemWidth < itemWidth) {
int count = parentSpacing / itemWidth;
itemWidth = parentSpacing / count;
}
// if there are more items. then show half of the first that is exceeded
// to tell user that there are more.
if (itemWidth * itemCount > parentSpacing) {
int count = (width - paddingLeft) / itemWidth;
itemWidth = (int) ((width - paddingLeft) / (count + .5f));
}
return itemWidth;
}
};

private int maxItemCountInLines;
private int miniItemWidth = -1;
private List<Pair<View, LinearLayout.LayoutParams>> mFirstLineViews;
private List<Pair<View, LinearLayout.LayoutParams>> mSecondLineViews;
private int linePaddingHor;
private int itemWidth;
private final ItemWidthCalculator mItemWidthCalculator;


public QMUIBottomSheetGridLineLayout(QMUIBottomSheet bottomSheet,
@Nullable ItemWidthCalculator widthCalculator,
List<Pair<View, LinearLayout.LayoutParams>> firstLineViews,
List<Pair<View, LinearLayout.LayoutParams>> secondLineViews) {
super(bottomSheet.getContext());
setOrientation(VERTICAL);
setGravity(Gravity.TOP);

mItemWidthCalculator = widthCalculator == null ? DEFAULT_CALCULATOR : widthCalculator;

int paddingTop = QMUIResHelper.getAttrDimen(
bottomSheet.getContext(), R.attr.qmui_bottom_sheet_grid_padding_top);
int paddingBottom = QMUIResHelper.getAttrDimen(
Expand Down Expand Up @@ -131,27 +161,14 @@ protected void measureChild(View child, int parentWidthMeasureSpec, int parentHe
super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
}


private int calculateItemWidth(int width, int calculateCount, int paddingLeft, int paddingRight) {
if (miniItemWidth == -1) {
miniItemWidth = QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_bottom_sheet_grid_item_mini_width);
}
return mItemWidthCalculator.calculate(getContext(), width, miniItemWidth, calculateCount, paddingLeft, paddingRight);
}

final int parentSpacing = width - paddingLeft - paddingRight;
int itemWidth = miniItemWidth;
// there is no more space for the last one item. then stretch the item width
if (calculateCount >= 3
&& parentSpacing - calculateCount * itemWidth > 0
&& parentSpacing - calculateCount * itemWidth < itemWidth) {
int count = parentSpacing / itemWidth;
itemWidth = parentSpacing / count;
}
// if there are more items. then show half of the first that is exceeded
// to tell user that there are more.
if (itemWidth * calculateCount > parentSpacing) {
int count = (width - paddingLeft) / itemWidth;
itemWidth = (int) ((width - paddingLeft) / (count + .5f));
}
return itemWidth;
public interface ItemWidthCalculator {
int calculate(Context context, int width, int miniWidth, int itemCount, int paddingLeft, int paddingRight);
}
}

0 comments on commit d524205

Please sign in to comment.