android viewholder模式,Android ViewHolder模式
static class ViewHolder {
TextView text;
ImageView icon;
}
可以看到它只是一个静态类,它的作用就在于减少不必要的调用findViewById
完整的官方例子,官方例子中convertView 也是避免inflating View。
然后把对底下的控件引用存在ViewHolder里面,再在View.setTag(holder)把它放在view里,下次就可以直接取了。
效率相差多少?看这篇文章:Android开发之ListView 适配器(Adapter)优化
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import com.example.android.apis.R;
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
public class List14 extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] DATA = Cheeses.sCheeseStrings;
}
android viewholder模式,Android ViewHolder模式相关推荐
- android - 简易launcher - RecyclerView画廊模式
一.项目代码地址:https://github.com/lingchen1854/Simple-Launch 二.效果图: gif动图可以进git查看,csdn上传失败. 三.代 ...
- Android 架构之 MVC 架构模式
前言 由于 MVP.MVVM.组件化架构的兴起,MVC 架构在 android 中的应用变得越来越少,但 MVC 是基础,理解好 MVC 才能更好的理解 MVP,MVVM,因为后两种都是基于 MVC ...
- Android架构篇-4 架构模式MVVM
Android架构篇-4 架构模式MVVM MVVM原理 #mermaid-svg-CJmTYPxP5GkKNMic .label{font-family:'trebuchet ms', verdan ...
- Android系统移植与调试之-------如何修改Android设备添加重启、飞行模式、静音模式等功能(一)...
1.首先先来看一下修改前后的效果对比图 修改之后的图片 确认重启界面 具体的修改内容在下一篇中具体介绍. Android系统移植与调试之------->如何修改Android设备添加重启.飞行模 ...
- Android学习 StateMachine与State模式
2019独角兽企业重金招聘Python工程师标准>>> 一 State模式 意图: 允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类.(Objects for ...
- 【Android 逆向】selinux 进程保护 ( selinux 进程保护 | 宽容模式 Permissive | 强制模式 Enforcing )
文章目录 一.selinux 进程保护 二.宽容模式与强制模式 一.selinux 进程保护 selinux 进程保护 一旦开启后 , 其它进程不能调试指定的进程 ; Android 5.0 及之后的 ...
- Android中的设计模式-状态模式
原文出处:http://www.linuxidc.com/Linux/2015-04/116013.htm 状态模式说明 "状态模式允许一个对象在其内部状态改变的时候改变其行为.这个对象看上 ...
- Android中常见的MVC模式
MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller). MVC模式的目的就是实现Web系统的职能分工. Model层实现系统中的业务 ...
- android官方夜间模式,Android夜间模式实践
前言 由于项目需要,近段时间开发的夜间模式功能.主流的方案如下: 1.通过切换theme实现 2.通过resource id映射实现 3.通过Android Support Library的实现 方案 ...
- android eng 模式,android 4.0 编译模式 eng - user 时遇到的有关问题
android 4.0 编译模式 eng -- > user 时遇到的问题 关键词:android 4.0 user eng camera nand 串口 平台信息: 内核:linu ...
最新文章
- 图论之tarjan缩点
- iOS系类教程之用instruments来检验你的app
- 全栈深度学习第3期: 怎样科学管理实验数据?
- 解读系统资质审批的相关政策
- coco2d-js 多屏适配相关API
- dda算法画直线_深度学习算法第一讲感知机数学原理解析及实现
- MyBatis框架generatorSqlmapCustom自动生成及下载方法
- 小米8 微信支付 java_小米微信WAP支付SDK接入指南
- 【mmdetection3d】——学习配置文件
- excel规划求解功能总结
- redfish_Redfish和Ansible的带外管理
- 视频教程-2021软考软件设计师--基础知识培训视频-软考
- 废旧手机改造成好玩的天气暗示相框
- 图文笔记,带你走进《未来简史》(11-15)
- 【PR】如何处理视频在最后时声音逐渐变小
- JAVA体育用品在线商城系统-springboot【数据库设计、源码、开题报告】
- 移动增值业务随笔(2)
- 腾讯欲全资收购搜狗,目的是什么?
- arduino+A4889+步进电机
- 2021龙川隆师中学高考成绩查询入口,2020重庆高考成绩查询系统入口官网
热门文章
- 怎么通过大网给服务器传文件在哪里,高速传输文件的办法
- Android UI个性style开源组件
- thinkphp 使用 kindEditor
- logistic模型 matlab,logistic模型MATLAB代码
- 一体机怎么修复音频服务器,多媒体教学一体机没有声音是怎么办?
- android6.0华为刷机包,华为畅享6官方rom刷机包_华为畅享6原版系统包_升级包
- 计算机网络自顶向下 1
- 深度linux 安装qq游戏,在Deepin系统下用Playonlinux完全可以运行QQ游戏大厅
- C# winform推荐波形图表控件scottplot
- mysql 主键B+Tree 3层存2000W行数据