Skip to content

Commit

Permalink
添加水平反向排列属性(RTL)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukey7 committed Jun 3, 2017
1 parent b9979ec commit 7f0be92
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 15 deletions.
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.dl7.taglayout"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dl7.taglayout">

<application
android:name=".MyApplication"
Expand Down Expand Up @@ -28,6 +28,8 @@
</activity>
<activity android:name=".TestActivity">
</activity>
<activity android:name=".TagReverseActivity">
</activity>
</application>

</manifest>
4 changes: 3 additions & 1 deletion app/src/main/java/com/dl7/taglayout/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MainActivity extends AppCompatActivity {

private final String[] mTagWords = new String[]{
"不同边框形状的标签", "单选和多选标签", "可编辑的标签", "动画效果的换一换标签",
"TagView的一些其它用途"
"TagView的一些其它用途", "水平反向排列(RTL)"
};
private TagLayout mTagLayout;

Expand All @@ -38,6 +38,8 @@ public void onTagClick(int position, String text, @TagView.TagMode int tagMode)
startActivity(new Intent(MainActivity.this, TagChangeActivity.class));
} else if (mTagWords[4].equals(text)) {
startActivity(new Intent(MainActivity.this, TagViewActivity.class));
} else if (mTagWords[5].equals(text)) {
startActivity(new Intent(MainActivity.this, TagReverseActivity.class));
}
}
});
Expand Down
67 changes: 67 additions & 0 deletions app/src/main/java/com/dl7/taglayout/TagReverseActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.dl7.taglayout;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.dl7.tag.TagLayout;
import com.dl7.tag.TagView;
import com.dl7.taglayout.utils.TagWordFactory;
import com.dl7.taglayout.utils.ToastUtils;

public class TagReverseActivity extends AppCompatActivity implements TagView.OnTagClickListener, TagView.OnTagLongClickListener {

private TagLayout mTagLayout1;
private TagLayout mTagLayout2;
private TagLayout mTagLayout3;
private TagView mTagDel;
private TagView mTagAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tag_reverse);
initView();
}

private void initView() {
mTagLayout1 = (TagLayout) findViewById(R.id.tag_layout_1);
mTagLayout2 = (TagLayout) findViewById(R.id.tag_layout_2);
mTagLayout3 = (TagLayout) findViewById(R.id.tag_layout_3);
mTagDel = (TagView) findViewById(R.id.tag_del);
mTagAdd = (TagView) findViewById(R.id.tag_add);
mTagLayout1.setTagClickListener(this);
mTagLayout1.setTagLongClickListener(this);
mTagLayout2.setTagClickListener(this);
mTagLayout2.setTagLongClickListener(this);
mTagLayout3.setTagClickListener(this);
mTagLayout3.setTagLongClickListener(this);

mTagAdd.setTagClickListener(new TagView.OnTagClickListener() {
@Override
public void onTagClick(int position, String text, @TagView.TagMode int tagMode) {
String word = TagWordFactory.provideTagWord();
mTagLayout1.addTag(word);
mTagLayout2.addTag(word);
mTagLayout3.addTag(word);
}
});
mTagDel.setTagClickListener(new TagView.OnTagClickListener() {
@Override
public void onTagClick(int position, String text, @TagView.TagMode int tagMode) {
mTagLayout1.deleteTag(0);
mTagLayout2.deleteTag(0);
mTagLayout3.deleteTag(0);
}
});
}

@Override
public void onTagClick(int position, String text, @TagView.TagMode int tagMode) {
ToastUtils.showToast(text);
}

@Override
public void onTagLongClick(int position, String text, @TagView.TagMode int tagMode) {
ToastUtils.showToast("长按:" + text);
}
}
128 changes: 128 additions & 0 deletions app/src/main/res/layout/activity_tag_reverse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dl7.taglayout.TagReverseActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:text="圆角矩形(彩色、一行固定3个):"
android:layout_margin="3dp"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:background="#dddddd"/>

<com.dl7.tag.TagLayout
android:id="@+id/tag_layout_1"
style="@style/TagLayout.RandomColor"
app:tag_layout_fit_num="3"
app:tag_layout_horizontal_reverse="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="wrap_content"
android:text="圆弧形(按压效果):"
android:layout_margin="3dp"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:background="#dddddd"/>

<com.dl7.tag.TagLayout
android:id="@+id/tag_layout_2"
style="@style/TagLayout.Arc"
app:tag_layout_press_feedback="true"
app:tag_layout_horizontal_reverse="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="wrap_content"
android:text="直角矩形(彩色):"
android:layout_margin="3dp"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:background="#dddddd"/>

<com.dl7.tag.TagLayout
android:id="@+id/tag_layout_3"
style="@style/TagLayout.Rect"
app:tag_layout_random_color="true"
app:tag_layout_horizontal_reverse="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

</ScrollView>

<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dddddd"/>

<FrameLayout
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="wrap_content">

<com.dl7.tag.TagView
android:id="@+id/tag_del"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
app:tag_text="删除"
app:tag_border_color="@android:color/holo_red_light"
app:tag_bg_color="@android:color/white"
app:tag_bg_color_check="@android:color/holo_red_light"
app:tag_border_color_check="@android:color/holo_red_light"
app:tag_text_color_check="@android:color/white"
app:tag_icon="@mipmap/ic_delete"
app:tag_press_feedback="true"
app:tag_text_color="@android:color/holo_red_light"/>

<com.dl7.tag.TagView
android:id="@+id/tag_add"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp"
app:tag_text="添加"
app:tag_border_color="@android:color/holo_green_light"
app:tag_bg_color="@android:color/white"
app:tag_bg_color_check="@android:color/holo_green_light"
app:tag_border_color_check="@android:color/holo_green_light"
app:tag_text_color_check="@android:color/white"
app:tag_icon="@mipmap/ic_add"
app:tag_press_feedback="true"
app:tag_text_color="@android:color/holo_green_light"/>
</FrameLayout>
</LinearLayout>
4 changes: 2 additions & 2 deletions taglayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 105
versionName "1.0.5"
versionCode 106
versionName "1.0.6"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
43 changes: 33 additions & 10 deletions taglayout/src/main/java/com/dl7/tag/TagLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class TagLayout extends ViewGroup {
private TagEditView mFitTagEditView;
// 使能随机颜色
private boolean mEnableRandomColor;
// 是否反转水平布局
private boolean mIsHorizontalReverse;


public TagLayout(Context context) {
Expand Down Expand Up @@ -125,6 +127,7 @@ private void _init(Context context, AttributeSet attrs, int defStyleAttr) {
mTagHorizontalPadding = (int) a.getDimension(R.styleable.TagLayout_tag_view_horizontal_padding, MeasureUtils.dp2px(context, 5f));
mTagVerticalPadding = (int) a.getDimension(R.styleable.TagLayout_tag_view_vertical_padding, MeasureUtils.dp2px(context, 5f));
mIconPadding = (int) a.getDimension(R.styleable.TagLayout_tag_view_icon_padding, MeasureUtils.dp2px(context, 3f));
mIsHorizontalReverse = a.getBoolean(R.styleable.TagLayout_tag_layout_horizontal_reverse, false);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -216,7 +219,13 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 当前布局使用的top坐标
int curTop = getPaddingTop();
// 当前布局使用的left坐标
int curLeft = getPaddingLeft();
int curLeft;
final int maxRight = mAvailableWidth + getPaddingLeft();
if (mIsHorizontalReverse) {
curLeft = maxRight;
} else {
curLeft = getPaddingLeft();
}
int maxHeight = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
Expand All @@ -230,16 +239,30 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
// 超过一行做换行操作
if (width + curLeft > mAvailableWidth + getPaddingLeft()) {
curLeft = getPaddingLeft();
// 计算top坐标,要加上垂直间隙
curTop += maxHeight + mVerticalInterval;
maxHeight = child.getMeasuredHeight();
if (mIsHorizontalReverse) {
curLeft -= width;
if (getPaddingLeft() > curLeft) {
curLeft = maxRight - width;
curLeft = Math.max(curLeft, getPaddingLeft());
// 计算top坐标,要加上垂直间隙
curTop += maxHeight + mVerticalInterval;
maxHeight = child.getMeasuredHeight();
}
// 设置子视图布局
child.layout(curLeft, curTop, curLeft + width, curTop + height);
curLeft -= mHorizontalInterval;
} else {
if (width + curLeft > maxRight) {
curLeft = getPaddingLeft();
// 计算top坐标,要加上垂直间隙
curTop += maxHeight + mVerticalInterval;
maxHeight = child.getMeasuredHeight();
}
// 设置子视图布局
child.layout(curLeft, curTop, curLeft + width, curTop + height);
// 计算left坐标,要加上水平间隙
curLeft += width + mHorizontalInterval;
}
// 设置子视图布局
child.layout(curLeft, curTop, curLeft + width, curTop + height);
// 计算left坐标,要加上水平间隙
curLeft += width + mHorizontalInterval;
}
}

Expand Down
1 change: 1 addition & 0 deletions taglayout/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<attr name="tag_layout_vertical_interval" format="dimension"/>
<attr name="tag_layout_horizontal_interval" format="dimension"/>
<attr name="tag_layout_fit_num" format="integer"/>
<attr name="tag_layout_horizontal_reverse" format="boolean"/>
<!-- TagView -->
<attr name="tag_view_bg_color" format="color"/>
<attr name="tag_view_border_color" format="color"/>
Expand Down

0 comments on commit 7f0be92

Please sign in to comment.