View.setTag()与View.getTag()的作用

这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用
Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;

if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mWidgetsSwitchApp
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.app_gallery_item, null);
                vh = new ViewHolder();
                vh.view1 = (ImageView) convertView.findViewById(R.id.view1);
                vh.view2 = (ImageView) convertView.findViewById(R.id.view2);
                vh.view3= (ImageView) convertView.findViewById(R.id.view3);
                vh.view4 = (ImageView) convertView.findViewById(R.id.view4);
                convertView.setTag(vh);
        } else {
                vh = (ViewHolder) convertView.getTag();
        }
//其他的代码可以直接使用 vh.view1、vh.view2、vh.view3 、vh.view4

}

View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。

可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button;

public class Main extends Activity {

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
Button button1 = (Button) findViewById(R.id.Button01); 
Button button2 = (Button) findViewById(R.id.Button02); 
Button button3 = (Button) findViewById(R.id.Button03); 
Button button4 = (Button) findViewById(R.id.Button04); 
MyListener listener = new MyListener(); 
button1.setTag(1); 
button1.setOnClickListener(listener); 
button2.setTag(2); 
button2.setOnClickListener(listener); 
button3.setTag(3); 
button3.setOnClickListener(listener); 
button4.setTag(4); 
button4.setOnClickListener(listener); 
}

public class MyListener implements View.OnClickListener {

@Override 
public void onClick(View v) { 
int tag = (Integer) v.getTag(); 
switch (tag) { 
case 1: 
System.out.println("button1 click"); 
break; 
case 2: 
System.out.println("button2 click"); 
break; 
case 3: 
System.out.println("button3 click"); 
break; 
case 4: 
System.out.println("button4 click"); 
break; 

}

}

时间: 2024-10-14 18:56:22

View.setTag()与View.getTag()的作用的相关文章

自定义Adapter中getView( )中使用View.setTag()和不使用的区别。

首先来看使用Tag的情况. @Override public View getView(int position, View view, ViewGroup group) { ViewHolder holder = new ViewHolder(); if(view==null){ view = inflater.inflate(R.layout.note_list_item, null);//加载列表项的布局文件. holder.title = (TextView)view.findViewB

view.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)

转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用. setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法. 官方的api文档中提到:" The specified key should be an id declared in the resources of the applica

View.setTag(key,object)异常:The key must be an application-specific resource id.

07-11 13:43:26.184: E/AndroidRuntime(10229): FATAL EXCEPTION: main07-11 13:43:26.184: E/AndroidRuntime(10229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txrj.sms/com.txrj.sms.activity.CreateMessageActivity}: java.lang.Ill

小程序 给最外层view设置百分之百高度不起作用

<view class="content"> <view class="today"> <view class="info"> <view class="temp">{{today.wendu}}℃</view> <view class="weather">{{today.todayInfo.type}} {{today.todayIn

Move To View、Align With View、Align View to Selected三者的区别

Unity GameObject菜单栏下有3个和View(此View指显示Scene面板虚拟相机(后面简称Scene Camera)的视角,而非真实Camera的视角 )相关的选项:Move To View.Align With View和Align View to Selected,其作用分别为: Move To View:将选中的gameObject移动到Scene Camera视野的正中间. Align With View:将选中的gameObject与Scene Camera的Trans

Controller将Model数据传给View层,View层应该如何处理?

首先,我们在Model层中添加一个Person类. namespace MVCTest.Models{    public class Person    {        public string IdCard { get; set; }        public string Name { get; set; }        public string Sex { get; set; }        public int Age { get; set; }    }} 在Contro

UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x7f9c28c9d740>) 出现此类错误的时候很是头疼,因为这是 iOS9.0之类才会出现的错误,具体表现就是当 UISearchBar 出现的时候控制台就会输入此类的信息: 其实也很好解决,可以

猫猫学iOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:项目代码 这个Demo用的几个控制器分别画了不通的xib,随便拖拽了几个空间,主要是几个按钮的切换,主要代码展示下: // // NYViewController.m // 控制器的view的显示 // // Created by apple on 14-10-10. // Copyright (c) 2014年 heima. All rights

Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中(swift)

Don't Put Into Your View Controller别把View创建的代码放在VC中html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; } .CodeMirror-lines { padding: 4px 0px; } .CodeMirror pre { paddin