Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for RTL #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions library/src/main/java/me/gujun/android/taggroup/TagGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -1022,4 +1047,4 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
}
}
}
}
}