What is the main purpose of setTag() getTag() methods of View?

前端 未结 7 616
醉话见心
醉话见心 2020-11-22 01:25

What is the main purpose of such methods as setTag() and getTag() of View type objects?

Am I right in thinking that I can ass

相关标签:
7条回答
  • 2020-11-22 02:05

    This is very useful for custom ArrayAdapter using. It is some kind of optimization. There setTag used as reference to object that references on some parts of layout (that displaying in ListView) instead of findViewById.

    static class ViewHolder {
        TextView tvPost;
        TextView tvDate;
        ImageView thumb;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        if (convertView == null) {
            LayoutInflater inflater = myContext.getLayoutInflater();
            convertView = inflater.inflate(R.layout.postitem, null);
    
            ViewHolder vh = new ViewHolder();
            vh.tvPost = (TextView)convertView.findViewById(R.id.postTitleLabel);
            vh.tvDate = (TextView)convertView.findViewById(R.id.postDateLabel);
            vh.thumb = (ImageView)convertView.findViewById(R.id.postThumb);
            convertView.setTag(vh);
        }
                ....................
    }
    
    0 讨论(0)
提交回复
热议问题