Android > listview - Adapter.getViewÀÇ ¼º´É ³ôÀ̱â - ViewHolder ¸¦ »ç¿ëÇ϶ó
µî·ÏÀÏ : 2017-07-05 18:49
Á¶È¸¼ö : 53,179
Android¿¡¼ ListView¶ó µé¾îÀÖ´Â ÆäÀÌÁö°¡ ¿¸±¶§ ´Ù¸¥ÆäÀÌÁö¿¡ ºñÇؼ ¸¹ÀÌ ´À¸®°Ô ¶ß´Â °æ¿ì°¡ ÀÖ½À´Ï´Ù.
±Ã±ÝÇؼ AdapterÀÇ getView()ÂÊ¿¡ ·Î±×¸¦ Âï¾îºÃ´õ´Ï. adapter¿¡ Àü´ÞÇÑ dataÀÇ °³¼ö * 3¹øÀÇ È£ÃâÀÌ ÀϾ°í ÀÖ¾ú½À´Ï´Ù.
±¸±Û½ÅÀÌ ¾Æ·¡ÀÇ Æ÷½ºÆ®¸¦ ´øÁ®Áּ̽À´Ï´Ù.;;
Âü°í : ¾Èµå·ÎÀ̵å ListView±¸Çö½Ã AdapterÀÇ getView Áߺ¹ È£Ãâ ¹®Á¦
Áï, layout¸¦ Á¤ÀÇÇÑ xml¿¡¼ ListView¿Í °¨½Î°í ÀÖ´Â LayoutÀÇ layout_height / layout_width¸¦ match_parent·Î º¯°æÇ϶ó! ¶ó´Â ³»¿ëÀ̾ú½À´Ï´Ù.
attribute¸¦ ¼öÁ¤Çϴϱî Àߵdz׿ä.
±×¹Û¿¡µµ ListViewÀÇ ¼º´É°ü·Ã À̽´¿Í °ü·ÃµÈ Æ÷½ºÆõµ ¸î°¡Áö ¹ß°ßÇؼ Á¤¸®ÇصӴϴÙ.
......
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = mInflater.inflate(resource, null);
}
((ImageView)view.findViewById(R.id.book_image)).setImageBitmap(getBitmap(getItem(position).getCover()));
((TextView)view.findViewById(R.id.title)).setText(getItem(position).getTitle());
((TextView)view.findViewById(R.id.author)).setText(getItem(position).getAuthor());
((TextView)view.findViewById(R.id.publisher)).setText(getItem(position).getPublisher());
//Log.d(TAG, "position = " + position + ", publisher = " + getItem(position).getPublisher());
// ÀÌ ÀÁÙÀÇ ÄÚµåÀÇ ·Î±×¸¦ º¸´Ï °¢ position °ªÀÌ 3¹ø¾¿ ÂïÈ÷°í ÀÖ´õ±º¿ä....;
return view;
}
......
Áß¿ä Æ÷ÀÎÆ®´Â getView()¿¡ ³Ñ¾î¿À´Â convertView°¡ nullÀ϶§ inflate ÇØÁà¶ó. ¶ó´Â °ÍÀÔ´Ï´Ù.
ÀÌÀ¯´Â ListView°¡ 6~7°³ÀÇ Á¤µµ ListView ³»ºÎ¿¡ Ç¥½ÃµÉ view¸¦ ij½ÃÇؼ recycleÇÏ´Â ¹æ½ÄÀ¸·Î ¿òÁ÷À̱⠶§¹®À̶ó´Â±º¿ä.
Áï, recycle µÈ °´Ã¼°¡ ³Ñ¾î¿Ã °æ¿ì inflate ÇÒ ÇÊ¿ä¾øÀÌ ³»¿ë¸¸ ¹Ù²ã¼ return ÇÏ¸é µÈ´Ù´Â°ÅÁö¿ä.
±×¹Û¿¡µµ Âü°íÇÑ ºí·Î±×¿¡´Â “ViewHolder”¸¦ »ç¿ëÇؼ findViewById()ÀÇ È½¼ö¸¦ ÁÙÀÌ´Â ¹æ¹ýµµ ÀÖ¾ú½À´Ï´Ù.
recycleµÇ´Ï±î view°¡ nullÀÌ ¾Æ´Ò°æ¿ì ´Ù½Ã findÇÏÁö ¾Ê°í setTag() / getTag()¸¦ ÀÌ¿ëÇØ ViewHolder¸¦ view¿¡
³Ö¾îµÎ°í Àç»ç¿ëÇÏ´Â ¹æ½ÄÀÔ´Ï´Ù.
À§ Äڵ忡 Àû¿ëÇÏ¸é ¾Æ·¡Ã³·³ µË´Ï´Ù.
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
view = mInflater.inflate(resource, null);
holder = new ViewHolder();
holder.bookImage = (ImageView)view.findViewById(R.id.book_image);
holder.title = (TextView)view.findViewById(R.id.title);
holder.author = (TextView)view.findViewById(R.id.author);
holder.publish = (TextView)view.findViewById(R.id.publisher);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.bookImage.setImageBitmap(getBitmap(getItem(position).getCover()));
holder.title.setText(getItem(position).getTitle());
holder.author.setText(getItem(position).getAuthor());
holder.publish.setText(getItem(position).getPublisher());
return view;
}
static class ViewHolder {
ImageView bookImage;
TextView title;
TextView author;
TextView publish;
}