¾È±Ô °øºÎ¹æ

Android > listview - Ä¿½ºÅÒ¸®½ºÆ®ºä - Tag »ç¿ë

µî·ÏÀÏ : 2017-07-05 18:46 Á¶È¸¼ö : 53,192

¿À´ÃÀº ÇöÀç±îÁö ¶Ç´Â ¾ÕÀ¸·Îµµ ¾Èµå·ÎÀÌµå ¾îÇÃÀ» °³¹ßÇÒ ¶§ °¡Àå ¸¹ÀÌ ÇؾßÇÒ ÀÛ¾÷ÀÎ Custom ListView¸¦ ¸¸µå´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¾Ë¾Æº¾½Ã´Ù.

´õ ³ª¾Æ°¡ Tag¸¦ »ç¿ëÇÏ¿© °¢°¢ÀÇ ¸®½ºÆ®¸¶´ÙÀÇ µ¥ÀÌÅ͸¦ °¡Á®¿Í ÇൿÀ» ÇÒ ¼ö ÀÖ°Ô Á¤ÀÇ ÇÏ´Â ¹æ¹ýÀ» ¼Ò°³ÇÏ°Ú½À´Ï´Ù.

Àú¿Í ÇÔ²² ÀÌ ±ÛÀ» ³¡±îÁö º¸½Å´Ù¸é, ¿©·¯ ºÐµµ Custom ListView¸¦ ÀÚÀ¯ÀÚÁ¦·Î »ç¿ëÀÌ °¡´ÉÇÒ °ÍÀ̶ó°í ±»°Ô ¹Ï½À´Ï´Ù.

¿À´ÃÀÇ ¸ñÇ¥´Â ´ÙÀ½ÀÇ È­¸éÀ¸·Î ±¸¼ºµÇ´Â ¸®½ºÆ®ºä ¸¸µå´Â °ÍÀÔ´Ï´Ù.



À§¿¡¼­ ÅëÈ­ ¹öÆ°(¿À¸¥ÂÊ)À» Ŭ¸¯½Ã¿¡ ¸®½ºÆ®¿¡ ÀÔ·ÂµÈ ÀüÈ­¹øÈ­¸¦ Toast·Î ¶ç¾î ÁÖ´Â °ÍÀÌ ¸ñÇ¥ÀÔ´Ï´Ù.

Custom ListViewÀÇ Á¦ÀÛ´Ü°è´Â ´ÙÀ½°ú °°½À´Ï´Ù.
 
	1. Custom ListView Layout Á¤ÀÇÇϱâ
	2. ListViewÀÇ ÇÑ ItemÀÇ Á¤º¸¸¦ ÀúÀåÇÒ Class Á¤ÀÇÇϱâ
	3. ListView¸¦ ±×·Á ÁÙ Adapter Á¤ÀÇÇϱâ
	4. »ç¿ëÇϱâ


ÀúÀÇ °æ¿ì´Â ¸ñÇ¥¿Í °°Àº ¸®½ºÆ® ¾ÆÀÌÅÆ ·¹À̾ƿôÀ» ¸¸µé±â À§Çؼ­ list_item_layout.xml À» »ý¼ºÇÏ¿© ÀÛ¼ºÇÏ¿´½À´Ï´Ù.

1. Custom ListView Layout Á¤ÀÇÇϱâ

listview_item.xml
	<?xml version="1.0" encoding="utf-8"?>
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:padding="10dp" >

		<ImageView
			android:id="@+id/user_icon"
			android:layout_width="60dp"
			android:layout_height="60dp"
			android:layout_alignParentLeft="true"
			android:layout_alignParentTop="true"
			android:contentDescription="@string/notify" />

		<LinearLayout
			android:layout_width="wrap_content"
			android:layout_height="60dp"
			android:layout_marginLeft="10dp"
			android:layout_toRightOf="@+id/user_icon"
			android:orientation="vertical" >

			<TextView
				android:id="@+id/user_name"
				style="@style/small_text01"
				android:layout_width="match_parent"
				android:layout_height="0dp"
				android:layout_gravity="center_vertical"
				android:layout_weight="1"
				android:textSize="18sp" />

			<TextView
				android:id="@+id/user_phone_number"
				android:layout_width="match_parent"
				android:layout_height="0dp"
				android:layout_gravity="center_vertical"
				android:layout_weight="1"
				android:textSize="18sp" />
		</LinearLayout>

		<ImageButton
			android:id="@+id/btn_send"
			android:layout_width="40dp"
			android:layout_height="40dp"
			android:layout_alignParentRight="true"
			android:layout_centerVertical="true"
			android:background="@drawable/btn_send"
			android:contentDescription="@string/send" />
	</RelativeLayout>
	


2. ListViewÀÇ ÇÑ ItemÀÇ Á¤º¸¸¦ ÀúÀåÇÒ Class Á¤ÀÇÇϱâ
ÀÌÁ¦ ¸®½ºÆ®ºäÀÇ ¾ÆÀÌÅÛ ÇϳªÇϳªÀÇ Á¤º¸¸¦ ÀúÀåÇÒ Å¬·¡½º¸¦  ¿¡ »ý¼ºÇØ ÁÖ¸é µË´Ï´Ù.

ÇöÀçÀÇ ¸ñÇ¥¸¦ ÀÌ·ç±â À§Çؼ­ ¸®½ºÆ®ºä¿¡ °¡Á®¿Í¾ß ÇÒ Á¤º¸µéÀº »ç¿ëÀÚ¾ÆÀÌÄÜ, À̸§ ±×¸®°í ÀüÈ­¹øÈ£°¡ ÀÖ½À´Ï´Ù.

À̸¦ ÀúÀåÇÒ ÇϳªÀÇ Å¬·¡½º¸¦ »ý¼ºÇØÁÖ¸é µË´Ï´Ù. ÀÌ Å¬·¡½º´Â Á¤º¸¸¦ ÀúÀå(Set)ÇÒ »ý¼ºÀÚ¿Í Á¤º¸¸¦ °¡Á®¿Ã Getter¸¦ ¸¸µé¾î ÁÖ¼Å¾ß ÇÕ´Ï´Ù.

ListView_User.java
		public class ListView_User {
			private Drawable mUserIcon;
			private String mUserName;
			private String mUserPhoneNumber;
			
			ListView_User(Drawable userIcon, String userName, String userPhoneNumber) {
				mUserIcon = userIcon;
				mUserName = userName;
				mUserPhoneNumber = userPhoneNumber;		
			}
			
			public Drawable getUserIcon(){
				return mUserIcon;
			}
			
			public String getUserName() {
				return mUserName;
			}
			
			public String  getUserPhoneNumber() {
				return mUserPhoneNumber;
			}
		}
		


3. Adapter »ý¼ºÇϱâ

Áö±Ý »ý¼ºÇÒ Adpater´Â ListView¿¡ »ý¼ºÇÑ LayoutÀ» ¸ÂÃß¾î ºÙ¿©ÁÖ´Â ³à¼®À̶ó°í »ý°¢ÇÏ¸é µË´Ï´Ù. AdapterÀÇ ÇൿÀº ´ÙÀ½°ú °°½À´Ï´Ù. 
		1. ¸®½ºÆ®ÀÇ ÇÑ ¾ÆÀÌÅÛ¿¡ ´ëÇÑ Á¤º¸¸¦ °¡Áö°í Àִ Ŭ·¹½º ÀνºÅϽº¸¦ ¹Þ¾Æ¿É´Ï´Ù.
		2. inflater¸¦ »ç¿ëÇÏ¿© ¹Ì¸® Á¤ÀÇÇصРÇϳªÀÇ ¾ÆÀÌÅÛ ·¹À̾ƿôÀ» °¡Á® ¿É´Ï´Ù.
		3. "1"¿¡¼­ °¡Á®¿Â °´Ã¼ÀÇ Á¤º¸¸¦ "2"¿¡¼­ °¡Á®¿Â ·¹À̾ƿô¿¡ ³Ö¾î ÁÝ´Ï´Ù.
		4. ºä¸¦ ¹ÝȯÇØÁØ´Ù.



Adapter¸¦ »ý¼ºÇϱâ À§Çؼ­´Â ÀûÇÕÇÑ adapter¸¦ »ó¼Ó¹Þ¾Æ¾ß ÇÕ´Ï´Ù.

»ý¼ºÀÚ¸¦ »ý¼ºÇØÁֽðí, getViewÇÔ¼ö¸¦ ÀÛ¼ºÇØÁÖ¸é µË´Ï´Ù. 

BaseAdapter¸¦ »ç¿ëÇÑ´Ù¸é µ¥ÀÌÅ͸¦ ÀúÀåÇÒ ¹è¿­À» Çϳª ¸¸µé°í ¹è¿­¿¡ Á¤º¸¸¦ ÀúÀåÇÏ´Â ÇÔ¼ö¸¦ »ý¼ºÇÕ´Ï´Ù.

¶Ç ÀÚµ¿»ý¼ºµÈ ÇÔ¼öÀÇ ¸î¸î ºÎºÐÀ» ü¿ö ÁÝ´Ï´Ù. ¼Ò½ºÄڵ忡 ÀÚ¼¼ÇÑ ÁÖ¼®À» ´Þ¾Ò½À´Ï´Ù. È®ÀÎÇØÁÖ¼¼¿ä^^.

ListView_Adapter.java
		public class ListView_Adapter extends BaseAdapter implements OnClickListener {

			// Activity¿¡¼­ °¡Á®¿Â °´Ã¼Á¤º¸¸¦ ÀúÀåÇÒ º¯¼ö
			private ListView_User mUser;
			private Context mContext;

			// ListView ³»ºÎ ViewµéÀ» °¡¸£Å³ º¯¼öµé
			private ImageView imgUserIcon;
			private TextView tvUserName;
			private TextView tvUserPhoneNumber;
			private ImageButton btnSend;

			// ¸®½ºÆ® ¾ÆÀÌÅÛ µ¥ÀÌÅ͸¦ ÀúÀåÇÒ ¹è¿­
			private ArrayList<ListView_User> mUserData;

			public ListView_Adapter(Context context) {
				super();
				mContext = context;
				mUserData = new ArrayList<ListView_User>();
			}

			@Override
			/**
			 * @return ¾ÆÀÌÅÛÀÇ ÃÑ °³¼ö¸¦ ¹Ýȯ
			 */
			public int getCount() {
				// TODO Auto-generated method stub
				return mUserData.size();
			}

			@Override
			/**
			 * @return ¼±ÅÃµÈ ¾ÆÀÌÅÛÀ» ¹Ýȯ
			 */
			public ListView_User getItem(int position) {
				// TODO Auto-generated method stub
				return mUserData.get(position);
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			/**
			 * getView
			 * 
			 * @param position - ÇöÀç ¸î ¹ø°·Î ¾ÆÀÌÅÛÀÌ Ãß°¡µÇ°í ÀÖ´ÂÁö Á¤º¸¸¦ °®°í ÀÖ´Ù.
			 * @param convertView - ÇöÀç »ç¿ëµÇ°í ÀÖ´Â ¾î¶² ·¹À̾ƿôÀ» °¡Áö°í ÀÖ´ÂÁö Á¤º¸¸¦ °®°í ÀÖ´Ù.
			 * @param parent - ÇöÀç ºäÀÇ ºÎ¸ð¸¦ ÁöĪÇÏÁö¸¸ Ưº°È÷ »ç¿ëµÇÁö´Â ¾Ê´Â´Ù.
			 * @return ¸®½ºÆ® ¾ÆÀÌÅÛÀÌ ÀúÀåµÈ convertView
			 */
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				View v = convertView;

				// ¸®½ºÆ® ¾ÆÀÌÅÛÀÌ »õ·Î Ãß°¡µÉ °æ¿ì¿¡´Â v°¡ null°ªÀÌ´Ù.
				// view´Â ¾î´À Á¤µµ »ý¼ºµÈ µÚ¿¡´Â Àç»ç¿ëÀÌ ÀϾ±â ¶§¹®¿¡ È¿À²À» À§Çؼ­ ÇØÁØ´Ù.
				if (v == null) {
					// inflater¸¦ ÀÌ¿ëÇÏ¿© »ç¿ëÇÒ ·¹À̾ƿôÀ» °¡Á®¿É´Ï´Ù.
					v = ((LayoutInflater) mContext
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
							.inflate(R.layout.listview_item, null);

					// ·¹À̾ƿôÀÌ ¸Þ¸ð¸®¿¡ ¿Ã¶ó¿Ô±â ¶§¹®¿¡ À̸¦ ÀÌ¿ëÇÏ¿© Æ÷ÇÔµÈ ºäµéÀ» ÂüÁ¶ÇÒ ¼ö ÀÖ½À´Ï´Ù.
					imgUserIcon = (ImageView) v.findViewById(R.id.user_icon);
					tvUserName = (TextView) v.findViewById(R.id.user_name);
					tvUserPhoneNumber = (TextView) v
							.findViewById(R.id.user_phone_number);
					btnSend = (ImageButton) v.findViewById(R.id.btn_send);
				}

				// ¹Þ¾Æ¿Â position °ªÀ» ÀÌ¿ëÇÏ¿© ¹è¿­¿¡¼­ ¾ÆÀÌÅÛÀ» °¡Á®¿Â´Ù.
				mUser = getItem(position);

				// Tag¸¦ ÀÌ¿ëÇÏ¿© µ¥ÀÌÅÍ¿Í ºä¸¦ ¹­½À´Ï´Ù.
				btnSend.setTag(mUser);

				// µ¥ÀÌÅÍÀÇ ½ÇÁ¸ ¿©ºÎ¸¦ ÆǺ°ÇÕ´Ï´Ù.
				if (mUser != null) {
					// µ¥ÀÌÅÍ°¡ ÀÖ´Ù¸é °®°í ÀÖ´Â Á¤º¸¸¦ ºä¿¡ ¾Ë¸Â°Ô ¹èÄ¡½Ãŵ´Ï´Ù.
					if (mUser.getUserIcon() != null) {
						imgUserIcon.setImageDrawable(mUser.getUserIcon());
					}
					tvUserName.setText(mUser.getUserName());
					tvUserPhoneNumber.setText(mUser.getUserPhoneNumber());
					btnSend.setOnClickListener(this);
				}
				// ¿Ï¼ºµÈ ¾ÆÀÌÅÛ ºä¸¦ ¹ÝȯÇÕ´Ï´Ù.
				return v;
			}

			// µ¥ÀÌÅ͸¦ Ãß°¡ÇÏ´Â °ÍÀ» À§Çؼ­ ¸¸µé¾î ÁØ´Ù.
			public void add(ListView_User user) {
				mUserData.add(user);
			}

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// Tag¸¦ ÀÌ¿ëÇÏ¿© Data¸¦ °¡Á®¿É´Ï´Ù.
				ListView_User clickItem = (ListView_User) v.getTag();

				switch (v.getId()) {
				case R.id.btn_send:
					Toast.makeText(mContext, clickItem.getUserPhoneNumber(),
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}


4. »ç¿ëÇϱâ

ÀÚ ÀÌÁ¦ ÇÊ¿äÇÑ ÀÛ¾÷ÀÌ ¸ðµÎ ³¡³µ½À´Ï´Ù. ÀÌÁ¦ »ç¿ëÇϱ⸸ ÇÏ¸é µË´Ï´Ù. »ç¿ë¹ýÀº ´ÙÀ½°ú °°½À´Ï´Ù.

»ç¿ëÇÒ Activity ¶Ç´Â fragmentÀÇ ·¹À̾ƿô¿¡ ¸®½ºÆ®ºä Çϳª¸¦ Ãß°¡½ÃÄÑ ÁÝ´Ï´Ù.

listview.xml
		<?xml version="1.0" encoding="utf-8"?>
		<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
			android:layout_width="match_parent"
			android:layout_height="match_parent">

			<ListView
				android:id="@+id/user_list"
				android:layout_width="match_parent"
				android:layout_height="wrap_content">
			</ListView>
				
		</RelativeLayout>


´ÙÀ½À¸·Î´Â Activity ¶Ç´Â Fragment¿¡¼­ ´ÙÀ½ÀÇ °úÁ¤À» ÇØÁÖ½Ã¸é µË´Ï´Ù.
 

		 1. Adapter »ý¼º
		 2. ListView ÂüÁ¶ ÈÄ Adapter ºÙÀ̱â
		 3. µ¥ÀÌÅÍ Ãß°¡
		 4. µ¥ÀÌÅÍ º¯°æÀÌ ³¡³µ´Ù´Â °ÍÀ» Adapter¿¡°Ô ¾Ë·ÁÁØ´Ù.


 

		public class ListView_Main extends Activity {

			private ListView userList;
			private ListView_Adapter adapter;

			/** Called when the activity is first created. */
			@Override
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);

				// TODO Auto-generated method stub
				setContentView(R.layout.listview);

				// Adapter »ý¼º
				adapter = new ListView_Adapter(getApplicationContext());
				// ¸®½ºÆ®ºä ÂüÁ¶ ¹× Adapter´Þ±â
				userList = (ListView) findViewById(R.id.user_list);
				userList.setAdapter(adapter);

				// Data Ãß°¡
				ListView_User u1 = new ListView_User(getResources().getDrawable(
						R.drawable.icon_user1), "±è¾¾", "010-1234-5678");
				adapter.add(u1);

				ListView_User u2 = new ListView_User(getResources().getDrawable(
						R.drawable.icon_user1), "À̾¾", "010-8765-4321");
				adapter.add(u2);

				ListView_User u3 = new ListView_User(getResources().getDrawable(
						R.drawable.icon_user1), "¹Ú¾¾", "010-0000-0000");
				adapter.add(u3);

				// Data°¡ º¯°æ µÇÀÖÀ½À» ¾Ë·ÁÁØ´Ù.
				adapter.notifyDataSetChanged();
			}

		}
		




 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.