先声明一下ViewHolder在Android自定义的适配器中使用。目的:优化资源,节省空间,避免重复绘制view而引起的不必要的内存损耗。

我自己以前的写法:

[html] view plaincopy
  1. public class PlateAdapter extends BaseAdapter {
  2. private List<Plate> list;
  3. private Context context;
  4. public PlateAdapter(List<Plate> list, Context context) {
  5. super();
  6. this.list = list;
  7. this.context = context;
  8. }
  9. @Override
  10. public int getCount() {
  11. // TODO Auto-generated method stub
  12. return list.size();
  13. }
  14. @Override
  15. public Object getItem(int arg0) {
  16. // TODO Auto-generated method stub
  17. return list.get(arg0);
  18. }
  19. @Override
  20. public long getItemId(int arg0) {
  21. // TODO Auto-generated method stub
  22. return arg0;
  23. }
  24. @Override
  25. public View getView(int arg0, View arg1, ViewGroup arg2) {
  26. // TODO Auto-generated method stub
  27. if(arg1 == null){
  28. arg1 = LayoutInflater.from(context).inflate(R.layout.select_car_type_list_item, null);
  29. }
  30. TextView text = (TextView)arg1.findViewById(R.id.text);
  31. text.setText(list.get(arg0).getPlateType());
  32. return arg1;
  33. }
  34. }

学习过ViewHolder之后的写法:

[html] view plaincopy
  1. @SuppressWarnings("unused")
  2. public class NoticeAdapter  extends BaseAdapter{
  3. private Context _context;
  4. private List<ExamNotice> _list;
  5. public NoticeAdapter(Context context, List<ExamNotice> list) {
  6. super();
  7. this._context = context;
  8. this._list = list;
  9. }
  10. public void set_list(List<ExamNotice> _list) {
  11. this._list = _list;
  12. }
  13. @Override
  14. public int getCount() {
  15. return _list.size();
  16. }
  17. @Override
  18. public Object getItem(int arg0) {
  19. return _list.get(arg0);
  20. }
  21. @Override
  22. public long getItemId(int arg0) {
  23. return arg0;
  24. }
  25. @Override
  26. public View getView(int position, View convertView, ViewGroup parent) {
  27. Holder holder;
  28. if(null==convertView){
  29. convertView=View.inflate(_context, R.layout.notice_item, null);
  30. holder=new Holder();
  31. holder.studyPlanName=(TextView)convertView.findViewById(R.id.xxjh_item_name);
  32. holder.studyPlanDate=(TextView)convertView.findViewById(R.id.xxjh_item_date);
  33. convertView.setTag(holder);
  34. }else{
  35. holder=(Holder)convertView.getTag();
  36. }
  37. ExamNotice notice=(ExamNotice) getItem(position);
  38. holder.studyPlanName.setText(notice.getNoticeTitle());
  39. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  40. String noticeDate = sdf.format(notice.getNoticeDate());
  41. holder.studyPlanDate.setText(noticeDate);
  42. return convertView;
  43. }
  44. private static class Holder{
  45. public TextView studyPlanName,studyPlanDate;
  46. }
  47. }

看一下官方的API:

[html] view plaincopy
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.android.apis.view;
  17. import android.app.ListActivity;
  18. import android.content.Context;
  19. import android.os.Bundle;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.BaseAdapter;
  24. import android.widget.TextView;
  25. import android.widget.ImageView;
  26. import android.graphics.BitmapFactory;
  27. import android.graphics.Bitmap;
  28. import com.example.android.apis.R;
  29. /**
  30. * Demonstrates how to write an efficient list adapter. The adapter used in this example binds
  31. * to an ImageView and to a TextView for each row in the list.
  32. *
  33. * To work efficiently the adapter implemented here uses two techniques:
  34. * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
  35. * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
  36. *
  37. * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
  38. * getView(). This data structures contains references to the views we want to bind data to, thus
  39. * avoiding calls to findViewById() every time getView() is invoked.
  40. */
  41. public class List14 extends ListActivity {
  42. private static class EfficientAdapter extends BaseAdapter {
  43. private LayoutInflater mInflater;
  44. private Bitmap mIcon1;
  45. private Bitmap mIcon2;
  46. public EfficientAdapter(Context context) {
  47. // Cache the LayoutInflate to avoid asking for a new one each time.
  48. mInflater = LayoutInflater.from(context);
  49. // Icons bound to the rows.
  50. mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
  51. mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
  52. }
  53. /**
  54. * The number of items in the list is determined by the number of speeches
  55. * in our array.
  56. *
  57. * @see android.widget.ListAdapter#getCount()
  58. */
  59. public int getCount() {
  60. return DATA.length;
  61. }
  62. /**
  63. * Since the data comes from an array, just returning the index is
  64. * sufficent to get at the data. If we were using a more complex data
  65. * structure, we would return whatever object represents one row in the
  66. * list.
  67. *
  68. * @see android.widget.ListAdapter#getItem(int)
  69. */
  70. public Object getItem(int position) {
  71. return position;
  72. }
  73. /**
  74. * Use the array index as a unique id.
  75. *
  76. * @see android.widget.ListAdapter#getItemId(int)
  77. */
  78. public long getItemId(int position) {
  79. return position;
  80. }
  81. /**
  82. * Make a view to hold each row.
  83. *
  84. * @see android.widget.ListAdapter#getView(int, android.view.View,
  85. *      android.view.ViewGroup)
  86. */
  87. public View getView(int position, View convertView, ViewGroup parent) {
  88. // A ViewHolder keeps references to children views to avoid unneccessary calls
  89. // to findViewById() on each row.
  90. ViewHolder holder;
  91. // When convertView is not null, we can reuse it directly, there is no need
  92. // to reinflate it. We only inflate a new View when the convertView supplied
  93. // by ListView is null.
  94. if (convertView == null) {
  95. convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
  96. // Creates a ViewHolder and store references to the two children views
  97. // we want to bind data to.
  98. holder = new ViewHolder();
  99. holder.text = (TextView) convertView.findViewById(R.id.text);
  100. holder.icon = (ImageView) convertView.findViewById(R.id.icon);
  101. convertView.setTag(holder);
  102. } else {
  103. // Get the ViewHolder back to get fast access to the TextView
  104. // and the ImageView.
  105. holder = (ViewHolder) convertView.getTag();
  106. }
  107. // Bind the data efficiently with the holder.
  108. holder.text.setText(DATA[position]);
  109. holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
  110. return convertView;
  111. }
  112. static class ViewHolder {
  113. TextView text;
  114. ImageView icon;
  115. }
  116. }
  117. @Override
  118. public void onCreate(Bundle savedInstanceState) {
  119. super.onCreate(savedInstanceState);
  120. setListAdapter(new EfficientAdapter(this));
  121. }
  122. private static final String[] DATA = Cheeses.sCheeseStrings;
  123. }

Android之ViewHolder用法相关推荐

  1. Android之Adapter用法总结

    本文转自http://kb.cnblogs.com/a/2328334/,转载请注明原出处. Android之Adapter用法总结 作者:Devin Zhang  来源:博客园  发布时间:2012 ...

  2. Android的Adapter用法总结

    Android之Adapter用法总结 1.Adapter概念   定义为将一个类的接口变换成客户端所期待的一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作.   在androi ...

  3. Android listview viewholder

    2019独角兽企业重金招聘Python工程师标准>>> Android ListView ViewHolder 利用adapter中的getView的 contentView 的复用 ...

  4. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

  5. android room表关联,Android Room的用法

    Android Room的用法 SQLite是Android内置的轻量级关系型数据库,但直接使用SQLite core包做数据库操作有以下劣势:需要编写长且重复的代码,这会很耗时且容易出错. 管理SQ ...

  6. android图片显示组件,Android可循环显示图像的Android Gallery组件用法实例

    本文实例分析了Android可循环显示图像的Android Gallery组件用法.分享给大家供大家参考,具体如下: Gallery组件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能 ...

  7. Android RxJava 基本用法

    Android RxJava 基本用法 RxJava 使用函数响应式编程方式,它可以简化项目,处理嵌套回调的异步事件. RxJava 依赖 这里以 RxJava 2.2.1 为例.在 build.gr ...

  8. 9.Android 万能ViewHolder

    9.Android 万能ViewHolder Android 万能ViewHolder ViewHolder的作用 万能ViewHolder的原理 在你的BaseListAdapter中可以这么实现 ...

  9. Android中富文本用法包括点击事件处理

    Android中富文本用法包括点击事件处理 关于 只需要富文本展示效果,不需要点击事件 展示富文本加点击事件 关于   最近项目需要中需要用到富文本展示及富文本点击功能,先去百度了一下,大多数的说法是 ...

最新文章

  1. 得到弹出菜单QMenu的高度
  2. runfile python_python文件执行路径问题
  3. 【springboot】禁用特定AutoConfiguration/自动配置类
  4. 学习并整理windows下编译nginx的步骤
  5. RabbitMQ从入门到精通
  6. 漫步数学分析二十六——积分方程与不动点
  7. 关于Adodb.Stream 的使用说明
  8. java for循环效率优化_java 优雅代码for循环 之性能调优
  9. Java基础---Java---网络编程---TCP、UDP、UDP-键盘录入方式数据、Socket、TCP复制文件、UDP-聊天
  10. webstorm 快捷键持续更新中...
  11. 74cms php在那个文件夹里面,74cms后台getshell
  12. 插件框架篇一之scrollbars
  13. Android屏幕旋转,赶紧学习一下
  14. 推荐系统:电商推荐系统架构
  15. 2k 幻14_ROG幻14经典版2K屏14寸
  16. Visual Studio 增加每行最多字符数限制参考线
  17. 记一次 .NET 某桌面奇侠游戏 非托管内存泄漏分析
  18. 【计组】懂了,时钟周期、机器周期、指令周期、总线周期、存储周期。
  19. 以下不属于C语言字符集的为,c语言第1章练习题答案
  20. matlab app设计步骤_1.1数学建模与MATLAB–MATLAB入门

热门文章

  1. python django报错 no such column:
  2. mysql和redis统计网站活跃度,最代码网站用户私信列表采用mysql union查询优化为Redis查询的经验和相关代码片段分享...
  3. 怎样实现两个线程共享一个集合_面试高频考察点:几种线程安全的Map解析
  4. c++ocx交互检测弹框_吉利几何C:2022杭州亚运会移动“明信片”!
  5. php 替换回车tab,PHP替换回车换行符的三种方法
  6. c 运行 java linux命令行参数,Linux下用命令行编译运行Java总结
  7. Python知识点6——函数
  8. react中高阶组件
  9. Git的stash操作
  10. [Spark][翻译]Spark 架构: Shuffle过程分析