安卓笔记侠

专注安卓开发

导航

TagCloudView云标签的灵活运用

这两天做了一个项目,发现标签不能更改任意一个标签的字体的颜色,需求如同置前标签,然后就对tagcloudeview稍做修改做了这么一个demo。不为别的,只为以后自己用的时候方便拷贝。

先看效果图: 
示意图 
这两天做了一个项目,需求如同置前标签,然后就对tagcloudeview稍做修改做了这么一个demo。不为别的,只为以后自己用的时候方便拷贝。

云标签开源地址https://github.com/kingideayou/TagCloudView 
在源码里面加了两个方法

 1 /**修改某些位置定点颜色**/
 2     public void setTagsByPosition(HashMap<Integer, Boolean> positions, List<String> tagList){
 3         this.tags = tagList;
 4         this.removeAllViews();
 5         if (tags != null && tags.size() > 0) {
 6             for (int i = 0; i < tags.size(); i++) {
 7                 TextView tagView = (TextView) mInflater.inflate(mTagResId, null);
 8                 if (mTagResId == DEFAULT_TAG_RESID) {
 9                     tagView.setBackgroundResource(mBackground);
10                     tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
11                     if (positions.get(i)){
12                         tagView.setTextColor(mSeclectTagColor);
13                     }else{
14                         tagView.setTextColor(mTagColor);
15                     }
16                 }
17                 LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
18                 tagView.setLayoutParams(layoutParams);
19                 tagView.setText(tags.get(i));
20                 tagView.setTag(TYPE_TEXT_NORMAL);
21                 final int finalI = i;
22                 tagView.setOnClickListener(new OnClickListener() {
23                     @Override
24                     public void onClick(View v) {
25                         if (onTagClickListener != null) {
26                             onTagClickListener.onTagClick(finalI);
27                         }
28                     }
29                 });
30                 addView(tagView);
31             }
32         }
33         postInvalidate();
34     }
35     /**最前面的修改颜色**/
36     public void setTagsByLength(int length,List<String> tagList){
37         this.tags = tagList;
38         this.removeAllViews();
39         if (tags != null && tags.size() > 0) {
40 
41             for (int i = 0; i < tags.size(); i++) {
42                 TextView tagView = (TextView) mInflater.inflate(mTagResId, null);
43                 if (mTagResId == DEFAULT_TAG_RESID) {
44                     tagView.setBackgroundResource(mBackground);
45                     tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
46                     if (i >= length){
47                         tagView.setTextColor(mTagColor);
48                     }else{
49                         tagView.setTextColor(mSeclectTagColor);
50                     }
51                 }
52                 LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
53                 tagView.setLayoutParams(layoutParams);
54                 tagView.setText(tags.get(i));
55                 tagView.setTag(TYPE_TEXT_NORMAL);
56                 final int finalI = i;
57                 tagView.setOnClickListener(new OnClickListener() {
58                     @Override
59                     public void onClick(View v) {
60                         if (onTagClickListener != null) {
61                             onTagClickListener.onTagClick(finalI);
62                         }
63                     }
64                 });
65                 addView(tagView);
66             }
67         }
68         postInvalidate();
69     }

一目了然的方法,所以不多做解释 
另外加了一个选中字体颜色的全局常量,和一个int变量

 private static final int SELCECT_TEXT_COLOR = R.color.yellow_bg;//选中后的标签颜色
    private int mSeclectTagColor;

在styles.xml中给TagCloudView增加了一个选中字体颜色的attr

  <attr name="tcvSeclecTextColor" format="reference" />

剩下就是运用的地方

不多说,直接上代码

  1 public class MainActivity extends AppCompatActivity {
  2     private TagCloudView normalTagView;//标准
  3     private TagCloudView selectTagUseView;//置前
  4     private TagCloudView positionsView;//定点
  5     private List<String> AllTagsNormal = new ArrayList<>(0);//整个标签存放集合
  6     private List<String> AllTagsSelect = new ArrayList<>(0);//整个标签存放集合
  7     private List<String> selectTags = new ArrayList<>(0);//选中的标签
  8     private List<String> notSelectTags = new ArrayList<>(0);//未选中的标签
  9 
 10     private List<String> AllTagsPosition = new ArrayList<>(0);//整个标签存放集合
 11     private HashMap<Integer, Boolean> map = new HashMap<>(0);//记录选择的位置
 12 
 13     @Override
 14     protected void onCreate(Bundle savedInstanceState) {
 15         super.onCreate(savedInstanceState);
 16         setContentView(R.layout.activity_main);
 17         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 18         normalTagView = (TagCloudView) findViewById(R.id.normalTag);
 19         selectTagUseView = (TagCloudView) findViewById(R.id.selcetTagUse);
 20         positionsView = (TagCloudView) findViewById(R.id.positionsTag);
 21         setSupportActionBar(toolbar);
 22         for (int i = 0; i < 15; i++) {
 23             AllTagsNormal.add("普通标签" + i);
 24             AllTagsSelect.add("置前标签" + i);
 25             AllTagsPosition.add("定点标签" + i);
 26             map.put(i, false);
 27         }
 28         normalTagView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
 29             @Override
 30             public void onTagClick(int position) {
 31                 Snackbar.make(normalTagView, AllTagsNormal.get(position), Snackbar.LENGTH_LONG)
 32                         .setAction("Action", null).show();
 33             }
 34         });
 35         selectTagUseView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
 36             @Override
 37             public void onTagClick(int position) {
 38                 if (selectTags.contains(AllTagsSelect.get(position))) {//如果选中的里面有 就删掉 扔到未选中的里面去
 39                     selectTags.remove(position);
 40                     notSelectTags.add(AllTagsSelect.get(position));
 41                 } else {
 42                     selectTags.add(AllTagsSelect.get(position));//
 43                     notSelectTags.remove(position - selectTags.size() + 1);
 44                 }
 45                 Snackbar.make(selectTagUseView, AllTagsSelect.get(position), Snackbar.LENGTH_LONG)
 46                         .setAction("Action", null).show();
 47 
 48                 AllTagsSelect.clear();//清空,重新装数据
 49                 AllTagsSelect.addAll(selectTags);
 50                 AllTagsSelect.addAll(notSelectTags);
 51                 bindSelectUseView(selectTags.size());
 52 
 53             }
 54         });
 55         positionsView.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
 56             @Override
 57             public void onTagClick(int position) {
 58                 bindPositionView(position);
 59                 Snackbar.make(positionsView, AllTagsPosition.get(position), Snackbar.LENGTH_LONG)
 60                         .setAction("Action", null).show();
 61             }
 62         });
 63         normalTagView.setTags(AllTagsNormal);
 64 
 65 
 66         int selectLength = 4;
 67         bindSelectUseView(selectLength);
 68 
 69 
 70         //用一个hashmap存放当前位置是否需要变色
 71         bindPositionView(3);
 72         bindPositionView(6);
 73         bindPositionView(9);
 74 
 75     }
 76 
 77     /**
 78      * 定点标签记录和view变化
 79      **/
 80     private void bindPositionView(int position) {
 81         for (int i = 0; i < AllTagsPosition.size(); i++) {
 82             if (i == position) {
 83                 if (map.get(i)) {
 84                     map.put(i, false);
 85                 } else {
 86                     map.put(i, true);
 87                 }
 88             } else {
 89                 if (map.get(i)) {
 90                     map.put(i, true);
 91                 } else {
 92                     map.put(i, false);
 93                 }
 94             }
 95         }
 96         positionsView.setTagsByPosition(map, AllTagsPosition);
 97         for (int i = 0; i < AllTagsPosition.size(); i++) {
 98             if (map.get(i)) {
 99                 positionsView.getChildAt(i).setBackgroundResource(R.drawable.edit_style_yellow);
100             }
101         }
102     }
103 
104     /**
105      * 选中标签的运用
106      **/
107     private void bindSelectUseView(int selectLength) {
108         selectTagUseView.setTagsByLength(selectLength, AllTagsSelect);
109         selectTags.clear();
110         notSelectTags.clear();
111         for (int i = 0; i < AllTagsSelect.size(); i++) {
112             if (i < selectLength) {
113                 selectTags.add(AllTagsSelect.get(i));//选中的存放入集合
114                 selectTagUseView.getChildAt(i).setBackgroundResource(R.drawable.edit_style_yellow);
115             } else {
116                 notSelectTags.add(AllTagsSelect.get(i));//未选中的存放入集合
117             }
118         }
119     }
120 
121     @Override
122     public boolean onCreateOptionsMenu(Menu menu) {
123         // Inflate the menu; this adds items to the action bar if it is present.
124         getMenuInflater().inflate(R.menu.menu_main, menu);
125         return true;
126     }
127 
128     @Override
129     public boolean onOptionsItemSelected(MenuItem item) {
130         // Handle action bar item clicks here. The action bar will
131         // automatically handle clicks on the Home/Up button, so long
132         // as you specify a parent activity in AndroidManifest.xml.
133         int id = item.getItemId();
134 
135         //noinspection SimplifiableIfStatement
136         if (id == R.id.action_settings) {
137             return true;
138         }
139 
140         return super.onOptionsItemSelected(item);
141     }
142 }

项目下载地址:https://github.com/a12a15a05/TagCloudViewDemo

posted on 2018-02-08 16:28  安卓笔记侠  阅读(801)  评论(0编辑  收藏  举报