這兩天做了一個(gè)項(xiàng)目,發(fā)現(xiàn)標(biāo)簽不能更改任意一個(gè)標(biāo)簽的字體的顏色,需求如同置前標(biāo)簽电湘,然后就對(duì)tagcloudeview稍做修改做了這么一個(gè)demo。不為別的鹅经,只為以后自己用的時(shí)候方便拷貝寂呛。
先看效果圖:
這兩天做了一個(gè)項(xiàng)目,需求如同置前標(biāo)簽瘾晃,然后就對(duì)tagcloudeview稍做修改做了這么一個(gè)demo贷痪。不為別的,只為以后自己用的時(shí)候方便拷貝蹦误。
云標(biāo)簽開(kāi)源地址https://github.com/kingideayou/TagCloudView
本項(xiàng)目開(kāi)源地址:https://github.com/a12a15a05/TagCloudViewDemo
在源碼里面加了兩個(gè)方法
/**修改某些位置定點(diǎn)顏色**/
public void setTagsByPosition(HashMap<Integer, Boolean> positions, List<String> tagList){
this.tags = tagList;
this.removeAllViews();
if (tags != null && tags.size() > 0) {
for (int i = 0; i < tags.size(); i++) {
TextView tagView = (TextView) mInflater.inflate(mTagResId, null);
if (mTagResId == DEFAULT_TAG_RESID) {
tagView.setBackgroundResource(mBackground);
tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
if (positions.get(i)){
tagView.setTextColor(mSeclectTagColor);
}else{
tagView.setTextColor(mTagColor);
}
}
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tagView.setLayoutParams(layoutParams);
tagView.setText(tags.get(i));
tagView.setTag(TYPE_TEXT_NORMAL);
final int finalI = i;
tagView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onTagClickListener != null) {
onTagClickListener.onTagClick(finalI);
}
}
});
addView(tagView);
}
}
postInvalidate();
}
/**最前面的修改顏色**/
public void setTagsByLength(int length,List<String> tagList){
this.tags = tagList;
this.removeAllViews();
if (tags != null && tags.size() > 0) {
for (int i = 0; i < tags.size(); i++) {
TextView tagView = (TextView) mInflater.inflate(mTagResId, null);
if (mTagResId == DEFAULT_TAG_RESID) {
tagView.setBackgroundResource(mBackground);
tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
if (i >= length){
tagView.setTextColor(mTagColor);
}else{
tagView.setTextColor(mSeclectTagColor);
}
}
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tagView.setLayoutParams(layoutParams);
tagView.setText(tags.get(i));
tagView.setTag(TYPE_TEXT_NORMAL);
final int finalI = i;
tagView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onTagClickListener != null) {
onTagClickListener.onTagClick(finalI);
}
}
});
addView(tagView);
}
}
postInvalidate();
}
一目了然的方法劫拢,所以不多做解釋 另外加了一個(gè)選中字體顏色的全局常量,和一個(gè)int變量
private static final int SELCECT_TEXT_COLOR = R.color.yellow_bg;//選中后的標(biāo)簽顏色
private int mSeclectTagColor;
在styles.xml中給TagCloudView增加了一個(gè)選中字體顏色的attr
<attr name="tcvSeclecTextColor" format="reference" />
剩下就是運(yùn)用的地方不多說(shuō)强胰,直接上代碼
public class MainActivity extends AppCompatActivity {
private TagCloudView normalTagView;//標(biāo)準(zhǔn)
private TagCloudView selectTagUseView;//置前
private TagCloudView positionsView;//定點(diǎn)
private List<String> AllTagsNormal = new ArrayList<>(0);//整個(gè)標(biāo)簽存放集合
private List<String> AllTagsSelect = new ArrayList<>(0);//整個(gè)標(biāo)簽存放集合
private List<String> AllTagsPosition = new ArrayList<>(0);//整個(gè)標(biāo)簽存放集合
private List<String> selectTags = new ArrayList<>(0);//選中的標(biāo)簽
private List<String> notSelectTags = new ArrayList<>(0);//未選中的標(biāo)簽
private HashMap<Integer, Boolean> map = new HashMap<>(0);//記錄選擇的位置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
normalTagView = (TagCloudView) findViewById(R.id.normalTag);
selectTagUseView = (TagCloudView) findViewById(R.id.selcetTagUse);
positionsView = (TagCloudView) findViewById(R.id.positionsTag);
setSupportActionBar(toolbar);
for (int i = 0; i < 15; i++) {
AllTagsNormal.add("普通標(biāo)簽" + i);
AllTagsSelect.add("置前標(biāo)簽" + i);
AllTagsPosition.add("定點(diǎn)標(biāo)簽" + i);
map.put(i, false);
}
normalTagView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
@Override
public void onTagClick(int position) {
Snackbar.make(normalTagView, AllTagsNormal.get(position), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
selectTagUseView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
@Override
public void onTagClick(int position) {
if (selectTags.contains(AllTagsSelect.get(position))) {//如果選中的里面有 就刪掉 扔到未選中的里面去
selectTags.remove(position);
notSelectTags.add(AllTagsSelect.get(position));
} else {
selectTags.add(AllTagsSelect.get(position));//
notSelectTags.remove(position - selectTags.size() + 1);
}
Snackbar.make(selectTagUseView, AllTagsSelect.get(position), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
AllTagsSelect.clear();//清空舱沧,重新裝數(shù)據(jù)
AllTagsSelect.addAll(selectTags);
AllTagsSelect.addAll(notSelectTags);
bindSelectUseView(selectTags.size());
}
});
positionsView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
@Override
public void onTagClick(int position) {
bindPositionView(position);
Snackbar.make(positionsView, AllTagsPosition.get(position), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
normalTagView.setTags(AllTagsNormal);
int selectLength = 4;
bindSelectUseView(selectLength);
//用一個(gè)hashmap存放當(dāng)前位置是否需要變色
bindPositionView(3);
bindPositionView(6);
bindPositionView(9);
}
/**
* 定點(diǎn)標(biāo)簽記錄和view變化
**/
private void bindPositionView(int position) {
for (int i = 0; i < AllTagsPosition.size(); i++) {
if (i == position) {
if (map.get(i)) {
map.put(i, false);
} else {
map.put(i, true);
}
} else {
if (map.get(i)) {
map.put(i, true);
} else {
map.put(i, false);
}
}
}
positionsView.setTagsByPosition(map, AllTagsPosition);
for (int i = 0; i < AllTagsPosition.size(); i++) {
if (map.get(i)) {
positionsView.getChildAt(i).setBackgroundResource(R.drawable.edit_style_yellow);
}
}
}
/**
* 選中標(biāo)簽的運(yùn)用
**/
private void bindSelectUseView(int selectLength) {
selectTagUseView.setTagsByLength(selectLength, AllTagsSelect);
selectTags.clear();
notSelectTags.clear();
for (int i = 0; i < AllTagsSelect.size(); i++) {
if (i < selectLength) {
selectTags.add(AllTagsSelect.get(i));//選中的存放入集合
selectTagUseView.getChildAt(i).setBackgroundResource(R.drawable.edit_style_yellow);
} else {
notSelectTags.add(AllTagsSelect.get(i));//未選中的存放入集合
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}