參考Android setTag()與getTag(),與set多個(gè)setTag()
首先我們要知道setTag方法是干什么的,SDK解釋為
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是用標(biāo)示view的。Tag從本質(zhì)上來(lái)講是就是相關(guān)聯(lián)的view的額外的信息。它們經(jīng)常用來(lái)存儲(chǔ)一些view的數(shù)據(jù),這樣做非常方便而不用存入另外的單獨(dú)結(jié)構(gòu)。
Tag有一個(gè)特點(diǎn),綁定數(shù)據(jù)到指定控件(view),而不顯示出來(lái)。setTag是android的view類中很有用的一個(gè)方法,與setId()不同,findViewById找到的是對(duì)象本身。而setTag()取出來(lái)的是對(duì)象所指向的對(duì)象。
在同一個(gè)類中,存取數(shù)據(jù)很方便,不用數(shù)據(jù)庫(kù)建庫(kù),也比sharePreference更節(jié)約內(nèi)存。setTag(Object tag)方法比較簡(jiǎn)單。比如
<pre>
TextView tvExecutor = (TextView)findViewById(R.id.t);
tvExecutor.setTag(selectedUserMap);
</pre>
這個(gè)selectedUserMap 可以是Map<String, String> selectedUserMap這個(gè)對(duì)象。也可以是LinkedList<CarEntity> mList 這個(gè)對(duì)象??梢源鎯?chǔ)各種臨時(shí)數(shù)據(jù),但是還不能稱其為存儲(chǔ)方式。在開發(fā)中,我們可以使用setTag(),getTag(),存取數(shù)據(jù)。可不可以一個(gè)對(duì)象,設(shè)置多個(gè)Tag呢?答案是肯定的。
一、利用緩存convertView盡可能少實(shí)例化同樣結(jié)構(gòu)體的對(duì)象
<pre>
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
// setTag的妙用
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
……略
}
</pre>
二、onClick點(diǎn)擊事件中設(shè)置多個(gè)TAG
可以用在多個(gè)Button添加一個(gè)監(jiān)聽器,每個(gè)Button都設(shè)置不同的setTag。這個(gè)監(jiān)聽器就通過(guò)getTag來(lái)分辨是哪個(gè)Button 被按下。
<pre>
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;
}
}
}
}
</pre>