Skip to content

Commit

Permalink
Merge branch 'anhtuan23-Make-TagView-selectable'
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Dec 1, 2018
2 parents 49863ad + 9962bd0 commit 5eb7829
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 47 deletions.
17 changes: 9 additions & 8 deletions androidtagview/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
compileSdkVersion 28

defaultConfig {
minSdkVersion 9
targetSdkVersion 25
minSdkVersion 21
targetSdkVersion 28
versionCode 115
versionName "1.1.5"
}
Expand All @@ -19,8 +18,10 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'androidx.appcompat:appcompat:1.0.1'
}
apply from: 'https://raw.githubusercontent.com/whilu/AndroidPublishLibrary/master/project/library/bintray_publish.gradle'
//TODO fix bug apply from: 'https://raw.githubusercontent.com/whilu/AndroidPublishLibrary/master/project/library/bintray_publish.gradle'
//https://github.com/google/ExoPlayer/commit/13592dfb53ec99ce1fdb2744e34717dbdec9dea8
//https://github.com/davideas/FlexibleAdapter/issues/579
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ public static int[] onRandomBuild(){
int bgColor = Color.parseColor("#" + BG_COLOR_ALPHA + COLORS[random]);
int bdColor = Color.parseColor("#" + BD_COLOR_ALPHA + COLORS[random]);
int tColor = SHARP666666;
return new int[]{bgColor, bdColor, tColor};
int tColor2 = SHARP727272;
return new int[]{bgColor, bdColor, tColor, tColor2};
}

public static int[] onPureBuild(PURE_COLOR type){
String color = type == PURE_COLOR.CYAN ? CYAN : TEAL;
int bgColor = Color.parseColor("#" + BG_COLOR_ALPHA + color);
int bdColor = Color.parseColor("#" + BD_COLOR_ALPHA + color);
int tColor = SHARP727272;
return new int[]{bgColor, bdColor, tColor};
int tColor2 = SHARP666666;
return new int[]{bgColor, bdColor, tColor, tColor2};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.DrawableRes;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
Expand All @@ -33,8 +31,12 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.annotation.DrawableRes;
import androidx.customview.widget.ViewDragHelper;

import static co.lujun.androidtagview.Utils.dp2px;
import static co.lujun.androidtagview.Utils.sp2px;

Expand Down Expand Up @@ -144,6 +146,11 @@ public class TagContainerLayout extends ViewGroup {
*/
private int mTagBackgroundColor = Color.parseColor("#33F44336");

/**
* Selected TagView background color(default #33FF7669)
*/
private int mSelectedTagBackgroundColor = Color.parseColor("#33FF7669");

/**
* TagView text color(default #FF666666)
*/
Expand All @@ -159,6 +166,11 @@ public class TagContainerLayout extends ViewGroup {
*/
private boolean isTagViewClickable;

/**
* Whether TagView can selectable(default unselectable)
*/
private boolean isTagViewSelectable;

/**
* Tags
*/
Expand Down Expand Up @@ -319,6 +331,7 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mTagTextColor = attributes.getColor(R.styleable.AndroidTagView_tag_text_color, mTagTextColor);
mTagTextDirection = attributes.getInt(R.styleable.AndroidTagView_tag_text_direction, mTagTextDirection);
isTagViewClickable = attributes.getBoolean(R.styleable.AndroidTagView_tag_clickable, false);
isTagViewSelectable = attributes.getBoolean(R.styleable.AndroidTagView_tag_selectable, false);
mRippleColor = attributes.getColor(R.styleable.AndroidTagView_tag_ripple_color, Color.parseColor("#EEEEEE"));
mRippleAlpha = attributes.getInteger(R.styleable.AndroidTagView_tag_ripple_alpha, mRippleAlpha);
mRippleDuration = attributes.getInteger(R.styleable.AndroidTagView_tag_ripple_duration, mRippleDuration);
Expand Down Expand Up @@ -507,7 +520,7 @@ private int[] onUpdateColorFactory() {
} else if (mTheme == ColorFactory.PURE_CYAN) {
colors = ColorFactory.onPureBuild(ColorFactory.PURE_COLOR.CYAN);
} else {
colors = new int[]{mTagBackgroundColor, mTagBorderColor, mTagTextColor};
colors = new int[]{mTagBackgroundColor, mTagBorderColor, mTagTextColor, mSelectedTagBackgroundColor};
}
return colors;
}
Expand Down Expand Up @@ -552,7 +565,7 @@ private void initTagView(TagView tagView, int position) {
int[] colors;
if (mColorArrayList != null && mColorArrayList.size() > 0) {
if (mColorArrayList.size() == mTags.size() &&
mColorArrayList.get(position).length >= 3) {
mColorArrayList.get(position).length >= 4) {
colors = mColorArrayList.get(position);
} else {
throw new RuntimeException("Illegal color list!");
Expand All @@ -564,6 +577,7 @@ private void initTagView(TagView tagView, int position) {
tagView.setTagBackgroundColor(colors[0]);
tagView.setTagBorderColor(colors[1]);
tagView.setTagTextColor(colors[2]);
tagView.setTagSelectedBackgroundColor(colors[3]);
tagView.setTagMaxLength(mTagMaxLength);
tagView.setTextDirection(mTagTextDirection);
tagView.setTypeface(mTagTypeface);
Expand All @@ -573,6 +587,7 @@ private void initTagView(TagView tagView, int position) {
tagView.setHorizontalPadding(mTagHorizontalPadding);
tagView.setVerticalPadding(mTagVerticalPadding);
tagView.setIsViewClickable(isTagViewClickable);
tagView.setIsViewSelectable(isTagViewSelectable);
tagView.setBdDistance(mTagBdDistance);
tagView.setOnTagClickListener(mOnTagClickListener);
tagView.setRippleAlpha(mRippleAlpha);
Expand Down Expand Up @@ -606,6 +621,21 @@ private void onRemoveTag(int position) {
// TODO, make removed view null?
}

private void onRemoveConsecutiveTags(List<Integer> positions) {
int smallestPosition = Collections.min(positions);
for (int position : positions) {
if (position < 0 || position >= mChildViews.size()) {
throw new RuntimeException("Illegal position!");
}
mChildViews.remove(smallestPosition);
removeViewAt(smallestPosition);
}
for (int i = smallestPosition; i < mChildViews.size(); i++) {
mChildViews.get(i).setTag(i);
}
// TODO, make removed view null?
}

private int[] onGetNewPosition(View view) {
int left = view.getLeft();
int top = view.getTop();
Expand Down Expand Up @@ -802,6 +832,16 @@ public void removeTag(int position) {
postInvalidate();
}

/**
* Remove TagView in multiple consecutive positions.
*
*
*/
public void removeConsecutiveTags(List<Integer> positions) {
onRemoveConsecutiveTags(positions);
postInvalidate();
}

/**
* Remove all TagViews.
*/
Expand All @@ -821,6 +861,82 @@ public void setOnTagClickListener(TagView.OnTagClickListener listener) {
invalidateTags();
}

/**
* Toggle select a tag
*
* @param position
*/
public void toggleSelectTagView(int position) {
if (isTagViewSelectable){
TagView tagView = ((TagView)mChildViews.get(position));
if (tagView.getIsViewSelected()){
tagView.deselectView();
} else {
tagView.selectView();
}
}
}

/**
* Select a tag
*
* @param position
*/
public void selectTagView(int position) {
if (isTagViewSelectable)
((TagView)mChildViews.get(position)).selectView();
}

/**
* Deselect a tag
*
* @param position
*/
public void deselectTagView(int position) {
if (isTagViewSelectable)
((TagView)mChildViews.get(position)).deselectView();
}

/**
* Return selected TagView positions
*
* @return list of selected positions
*/
public List<Integer> getSelectedTagViewPositions() {
List<Integer> selectedPositions = new ArrayList<>();
for (int i = 0; i < mChildViews.size(); i++){
if (((TagView)mChildViews.get(i)).getIsViewSelected()){
selectedPositions.add(i);
}
}
return selectedPositions;
}

/**
* Return selected TagView text
*
* @return list of selected tag text
*/
public List<String> getSelectedTagViewText() {
List<String> selectedTagText = new ArrayList<>();
for (int i = 0; i < mChildViews.size(); i++){
TagView tagView = (TagView)mChildViews.get(i);
if ((tagView.getIsViewSelected())){
selectedTagText.add(tagView.getText());
}
}
return selectedTagText;
}

/**
* Return number of child tags
*
* @return size
*/
public int size() {
return mChildViews.size();
}

/**
* Get TagView text.
*
Expand Down Expand Up @@ -1101,6 +1217,24 @@ public void setIsTagViewClickable(boolean clickable) {
this.isTagViewClickable = clickable;
}

/**
* Get TagView is selectable.
*
* @return
*/
public boolean getIsTagViewSelectable() {
return isTagViewSelectable;
}

/**
* Set TagView is selectable
*
* @param selectable
*/
public void setIsTagViewSelectable(boolean selectable) {
this.isTagViewSelectable= selectable;
}

/**
* Get TagView border width.
*
Expand Down
Loading

0 comments on commit 5eb7829

Please sign in to comment.