TagCloudView雲標籤的靈活運用,

來源:互聯網
上載者:User

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                     @Override24                     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                     @Override59                     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     @Override122     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     @Override129     public boolean onOptionsItemSelected(MenuItem item) {130         // Handle action bar item clicks here. The action bar will131         // automatically handle clicks on the Home/Up button, so long132         // as you specify a parent activity in AndroidManifest.xml.133         int id = item.getItemId();134 135         //noinspection SimplifiableIfStatement136         if (id == R.id.action_settings) {137             return true;138         }139 140         return super.onOptionsItemSelected(item);141     }142 }

項目:https://github.com/a12a15a05/TagCloudViewDemo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.