Skip to content

Commit

Permalink
According to words' width to add rows. (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshengmu authored and MortimerGoro committed May 9, 2019
1 parent 7de587e commit 13808e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Expand Up @@ -29,10 +29,10 @@ public class AutoCompletionView extends FrameLayout {
private View mSeparator;
private int mKeyWidth;
private int mKeyHeight;
private int mLineWidth;
private int mLineHeight;
private UIButton mExtendButton;
private int mExtendedHeight;
private final int kMaxItemsPerLine = 15;
private ArrayList<Words> mExtraItems = new ArrayList<>();
private boolean mIsExtended;
private Delegate mDelegate;
Expand Down Expand Up @@ -73,6 +73,7 @@ private void initialize(Context aContext) {
}
});
mKeyWidth = mKeyHeight = WidgetPlacement.pixelDimension(getContext(), R.dimen.autocompletion_widget_button_size);
mLineWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.autocompletion_widget_line_width);
mLineHeight = WidgetPlacement.pixelDimension(getContext(), R.dimen.autocompletion_widget_line_height);
mExtendedHeight = mLineHeight * 6;
setFocusable(false);
Expand Down Expand Up @@ -124,16 +125,22 @@ public void setItems(List<Words> aItems) {
}

int n = 0;
int currentWidth = 0;

for (Words item : aItems) {
if (n < kMaxItemsPerLine) {
mFirstLine.addView(createButton(item, clickHandler));
UITextButton textBtn = createButton(item, clickHandler);
textBtn.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
currentWidth += textBtn.getMeasuredWidth();

if (currentWidth < mLineWidth) {
mFirstLine.addView(textBtn);
} else {
mExtraItems.add(item);
}
n++;
}

mExtendButton.setVisibility(n >= kMaxItemsPerLine ? View.VISIBLE : View.GONE);
mExtendButton.setVisibility(currentWidth >= mLineWidth ? View.VISIBLE : View.GONE);
}

public boolean isExtended() {
Expand All @@ -152,14 +159,22 @@ public boolean isExtended() {

private void layoutExtendedItems() {
int index = 0;
int currentWidth = 0;
LinearLayout current = createRow();

for (Words item: mExtraItems) {
current.addView(createButton(item, clickHandler));
index++;
if (index > kMaxItemsPerLine) {
UITextButton textBtn = createButton(item, clickHandler);
textBtn.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
currentWidth += textBtn.getMeasuredWidth();

if (currentWidth < mLineWidth) {
current.addView(textBtn);
index++;
} else {
mExtendContent.addView(current);
current = createRow();
index = 0;
currentWidth = 0;
}
}
if (index > 0) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/dimen.xml
Expand Up @@ -103,6 +103,7 @@
<dimen name="no_internet_z_distance" format="float" type="dimen">2.5</dimen>

<!-- Autocompletion Widget -->
<dimen name="autocompletion_widget_line_width">530dp</dimen>
<dimen name="autocompletion_widget_line_height">36dp</dimen>
<dimen name="autocompletion_widget_extended_height">160dp</dimen>
<dimen name="autocompletion_widget_margin">4dp</dimen>
Expand Down

0 comments on commit 13808e7

Please sign in to comment.