歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:View Tag 的使用

Android開發教程:View Tag 的使用

日期:2017/3/1 10:40:14   编辑:Linux編程

發現Tag在View中還是很有作用的屬性,API中這樣描述的:

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

Tag不像ID是用標示view的。Tag從本質上來講是就是相關聯的view的額外的信息。它們經常用來存儲一些view的數據,這樣做非常方便而不用存入另外的單獨結構。

下面這個例子是從eoe中看到的,給我很大啟發:

[java]
  1. public class FileListAdapter extends BaseAdapter {
  2. private LayoutInflater mInflater;
  3. private Bitmap mIcon_folder;
  4. private Bitmap mIcon_file;
  5. private Bitmap mIcon_image;
  6. private Bitmap mIcon_audio;
  7. private Bitmap mIcon_video;
  8. private Bitmap mIcon_apk;
  9. private List<String> items;
  10. private List<String> paths;
  11. private List<String> sizes;
  12. private int isZoom = 0;
  13. // MyAdapter的構造器
  14. public FileListAdapter(Context context, List<String> it, List<String> pa,
  15. List<String> si, int zm) {
  16. mInflater = LayoutInflater.from(context);
  17. items = it;
  18. paths = pa;
  19. sizes = si;
  20. isZoom = zm;
  21. mIcon_folder = BitmapFactory.decodeResource(context.getResources(),
  22. R.drawable.folder); // 文件夾的圖標
  23. mIcon_file = BitmapFactory.decodeResource(context.getResources(),
  24. R.drawable.file); // 文件的圖文件
  25. mIcon_image = BitmapFactory.decodeResource(context.getResources(),
  26. R.drawable.image); // 圖片的圖文件
  27. mIcon_audio = BitmapFactory.decodeResource(context.getResources(),
  28. R.drawable.audio); // 音頻的圖文件
  29. mIcon_video = BitmapFactory.decodeResource(context.getResources(),
  30. R.drawable.video); // 視頻的圖文件
  31. mIcon_apk = BitmapFactory.decodeResource(context.getResources(),
  32. R.drawable.apk); // apk文件
  33. }
  34. @Override
  35. public int getCount() {
  36. return items.size();
  37. }
  38. @Override
  39. public Object getItem(int position) {
  40. return items.get(position);
  41. }
  42. @Override
  43. public long getItemId(int position) {
  44. return position;
  45. }
  46. @Override
  47. public View getView(int position, View convertView, ViewGroup par) {
  48. Bitmap bitMap = null;
  49. ViewHolder holder = null;
  50. if (convertView == null) {
  51. // 使用自定義的list_items作為Layout
  52. convertView = mInflater.inflate(R.layout.file_list_items, null);
  53. // 初始化holder的text與icon
  54. holder = new ViewHolder();
  55. holder.f_title = ((TextView) convertView.findViewById(R.id.f_title));
  56. holder.f_text = ((TextView) convertView.findViewById(R.id.f_text));
  57. holder.f_icon = ((ImageView) convertView.findViewById(R.id.f_icon));
  58. convertView.setTag(holder);
  59. } else {
  60. holder = (ViewHolder) convertView.getTag();
  61. }
  62. File f = new File(paths.get(position).toString());
  63. // 設置文件或文件夾的文字與icon
  64. holder.f_title.setText(f.getName());
  65. String f_type = MyUtil.getMIMEType(f, false);
  66. if (f.isDirectory()) {
  67. holder.f_icon.setImageBitmap(mIcon_folder);
  68. holder.f_text.setText("");
  69. } else {
  70. holder.f_text.setText(sizes.get(position));
  71. if ("image".equals(f_type)) {
  72. if (isZoom == 1) {
  73. bitMap = MyUtil.fitSizePic(f);
  74. if (bitMap != null) {
  75. holder.f_icon.setImageBitmap(bitMap);
  76. } else {
  77. holder.f_icon.setImageBitmap(mIcon_image);
  78. }
  79. } else {
  80. holder.f_icon.setImageBitmap(mIcon_image);
  81. }
  82. bitMap = null;
  83. } else if ("audio".equals(f_type)) {
  84. holder.f_icon.setImageBitmap(mIcon_audio);
  85. } else if ("video".equals(f_type)) {
  86. holder.f_icon.setImageBitmap(mIcon_video);
  87. } else if ("apk".equals(f_type)) {
  88. holder.f_icon.setImageBitmap(mIcon_apk);
  89. } else {
  90. holder.f_icon.setImageBitmap(mIcon_file);
  91. }
  92. }
  93. return convertView;
  94. }
  95. /**
  96. * 不單獨寫get set可以提高效率 class ViewHolder
  97. * */
  98. private class ViewHolder {
  99. TextView f_title;
  100. TextView f_text;
  101. ImageView f_icon;
  102. }
  103. }
Copyright © Linux教程網 All Rights Reserved