diff --git a/library/src/main/java/me/gujun/android/taggroup/TagGroup.java b/library/src/main/java/me/gujun/android/taggroup/TagGroup.java index 5f2ae99..9f82552 100644 --- a/library/src/main/java/me/gujun/android/taggroup/TagGroup.java +++ b/library/src/main/java/me/gujun/android/taggroup/TagGroup.java @@ -134,6 +134,9 @@ public class TagGroup extends ViewGroup { /** Listener used to handle tag click event. */ private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener(); + /** If RTL is forced, default is false */ + private boolean isRTL; + public TagGroup(Context context) { this(context, null); } @@ -173,6 +176,7 @@ public TagGroup(Context context, AttributeSet attrs, int defStyleAttr) { verticalSpacing = (int) a.getDimension(R.styleable.TagGroup_atg_verticalSpacing, default_vertical_spacing); horizontalPadding = (int) a.getDimension(R.styleable.TagGroup_atg_horizontalPadding, default_horizontal_padding); verticalPadding = (int) a.getDimension(R.styleable.TagGroup_atg_verticalPadding, default_vertical_padding); + isRTL = false; } finally { a.recycle(); } @@ -205,6 +209,13 @@ public void submitTag() { appendInputTag(); } } + + /** + * Call this to force RTL. + */ + public void setRTL(boolean _rtl){ + this.isRTL = _rtl; + } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @@ -267,6 +278,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { final int parentBottom = b - t - getPaddingBottom(); int childLeft = parentLeft; + int childRight = parentRight; int childTop = parentTop; int rowMaxHeight = 0; @@ -278,16 +290,29 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { final int height = child.getMeasuredHeight(); if (child.getVisibility() != GONE) { - if (childLeft + width > parentRight) { // Next line - childLeft = parentLeft; - childTop += rowMaxHeight + verticalSpacing; - rowMaxHeight = height; - } else { - rowMaxHeight = Math.max(rowMaxHeight, height); + if(isRTL){ + if (childRight - width < parentLeft) { // Next line + childRight = parentRight; + childTop += rowMaxHeight + verticalSpacing; + rowMaxHeight = height; + } else { + rowMaxHeight = Math.max(rowMaxHeight, height); + } + child.layout(childRight - width, childTop, childRight, childTop + height); + + childRight -= width + horizontalSpacing; + }else{ + if (childLeft + width > parentRight) { // Next line + childLeft = parentLeft; + childTop += rowMaxHeight + verticalSpacing; + rowMaxHeight = height; + } else { + rowMaxHeight = Math.max(rowMaxHeight, height); + } + child.layout(childLeft, childTop, childLeft + width, childTop + height); + + childLeft += width + horizontalSpacing; } - child.layout(childLeft, childTop, childLeft + width, childTop + height); - - childLeft += width + horizontalSpacing; } } } @@ -1022,4 +1047,4 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) { } } } -} \ No newline at end of file +}