¾È±Ô °øºÎ¹æ
	
	 ¾Èµå·ÎÀÌµå  > listview - äÆà UI ¿¹Á¦

	Ä«Åå °°Àº list ¸¸µé±â´Â Ä«ÅåÀÇ ´ëÈ­ ³»¿ªÃ³·³ ³ª¿Àµµ·Ï ¸¸µå´Â ¿¹Á¦ÀÔ´Ï´Ù.

	custom list¸¦ ¸¸µéÁÙ ¾Ë¾Æ¾ß ÇÏ°í custom list¿¡¼­ ¹è°æÀ» ³ªÀÎ ÆÐÄ¡¸¦ »ç¿ëÇÏ¸é µË´Ï´Ù.

	custom list¸¦ ¸¸µé·Á¸é ¸®½ºÆ®·Î »ç¿ëÇÒ xmlÀ» ÁغñÇØ¾ß Çϴµ¥¿ä

	¾Æ·¡¿Í °°ÀÌ ÁغñÇÏ¿´½À´Ï´Ù.

	ÇÙ½ÉÀº ¾çÂÊ¿¡ view¸¦ µÎ°í °¡¿îµ¥ ÅؽºÆ® ºä¸¦ Çϳª µÓ´Ï´Ù.
	
	viewÀÇ layout_weight¸¦ °¢°¢1·Î µÎ°í »ç¿ë¾ÈÇÏ´ÂÂÊÀ» GONE ó¸® ÇϸéµË´Ï´Ù.
	
	viewLeft.setVisibility(View.GONE);

	
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:orientation="horizontal" android:id="@+id/layout" android:gravity="center"> <View android:layout_width="wrap_content" android:layout_height="1dp" android:id="@+id/imageViewleft" android:background="#888888" android:layout_weight="1" android:layout_margin="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:text ="text" /> <View android:layout_width="wrap_content" android:layout_height="1dp" android:id="@+id/imageViewright" android:background="#888888" android:layout_weight="1" android:layout_margin="10dp" /> </LinearLayout>
¸®½ºÆ®¿¡ ¾ÆÀÌÅÛÀ» Ãß°¡ÇÒ¶§¿¡´Â ÀÎÀÚ¸¦ Çϳª ´õ µÖ¼­ ¿À¸¥ÂÊ ¿ÞÂÊ ¾îµð¿¡ µÑ°ÇÁö¸¦ °áÁ¤ÇÕ´Ï´Ù. Ä«Åå¿¡ º¸¸é ½Ã°£ º¯°æ¼±ÀÌ ³ª¿À´Â °æ¿ì°¡ Àִµ¥ ¿©±â¼­´Â 2¹ø ÇØ´ç °æ¿ì ó¸®ÀÔ´Ï´Ù. View¿¡ layout_height = 1dp·Î ¼³Á¤ÇÏ°í background»öÀ» °ËÀº»öÀ¸·Î ó¸®ÇÕ´Ï´Ù. - ¸®½ºÆ®¿¡ Ãß°¡ÇÒ¶§
m_Adapter.add("óÀ½",0); m_Adapter.add("ÀÌ°Ç ¹¹Áö",1); m_Adapter.add("´ëȭâÀÌ´Ù",0); m_Adapter.add("2015/10/8",2); m_Adapter.add("ÄðÄð",1); m_Adapter.add("ÄðÄðÄðÄð",0); m_Adapter.add("Àç¹ÌÀÖ°Ô",1);
- type¿¡ µû¸¥ ó¸®
if( m_List.get(position).type == 0 ) { text.setBackgroundResource(R.drawable.inbox2); layout.setGravity(Gravity.LEFT); viewRight.setVisibility(View.GONE); viewLeft.setVisibility(View.GONE); }else if(m_List.get(position).type == 1){ text.setBackgroundResource(R.drawable.outbox2); layout.setGravity(Gravity.RIGHT); viewRight.setVisibility(View.GONE); viewLeft.setVisibility(View.GONE); }else if(m_List.get(position).type == 2){ layout.setGravity(Gravity.CENTER); viewRight.setVisibility(View.VISIBLE); viewLeft.setVisibility(View.VISIBLE); }
- µ¿ÀÛÈ­¸é ÀÌÁ¦ºÎÅÍ ¸¸µå´Â ¹æ¹ýÀ» »ó¼¼ÇÏ°Ô ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù. 1. ê ¹é±×¶ó¿îµå À̹ÌÁö¸¦ ¸¸µì´Ï´Ù. (³ªÀÎÆÐÄ¡) res/drawable Æú´õ¸¦ ¸¸µé°í ¾Æ·¡À̹ÌÁö 3°³¸¦ º¹»çÇØ ³Ö½À´Ï´Ù.        inbox2.9.png (ÀԷ¸޼¼Áö1 ¹è°æÀ̹ÌÁö) outbox2.9.png (ÀԷ¸޼¼Áö2 ¹è°æÀ̹ÌÁö) datebg.9.png (³¯Â¥ ¸Þ¼¼Áö ¹è°æÀ̹ÌÁö) 2. activity_chatitem ¸¸µì´Ï´Ù. (·¹À̾ƿô) layout/activity_chatitem.xml ³»¿ëÀÔ´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:orientation="horizontal" android:id="@+id/layout" android:gravity="center"> <View android:layout_width="wrap_content" android:layout_height="1dp" android:id="@+id/imageViewleft" android:background="#888888" android:layout_weight="1" android:layout_margin="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:text ="text" /> <View android:layout_width="wrap_content" android:layout_height="1dp" android:id="@+id/imageViewright" android:background="#888888" android:layout_weight="1" android:layout_margin="10dp" /> </LinearLayout>
3. activity_main ¸¸µì´Ï´Ù. (·¹À̾ƿô) layout/activity_main.xml ³»¿ëÀÔ´Ï´Ù. äÆÃÀ» ÀÔ·ÂÇÒ ¼ö ÀÖ´Â EditText, Button µîÀÇ À§Á¬ÀÌ µé¾îÀÖ´Â Layout À̸ç À§¿¡¼­ ¸¸µç custom listview¸¦ Æ÷ÇÔ½Ãų Layout ÀÔ´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:id="@+id/im1" android:layout_height="wrap_content" android:layout_weight="1" > <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listView1" android:listSelector="#00000000" android:cacheColorHint="#00000000" android:divider="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:transcriptMode="alwaysScroll"/> </FrameLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#2F4F4F" android:layout_below="@id/im1" android:orientation="horizontal"> <TextView android:layout_width="20dp" android:layout_height="wrap_content" /> <EditText android:hint="¸Þ¼¼Áö¸¦ ÀÔ·ÂÇϼ¼¿ä" android:id="@+id/editText1" android:inputType="textMultiLine" android:maxLines="4" android:layout_width="200dp" android:layout_height="wrap_content"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Àü ¼Û" /> </LinearLayout> </LinearLayout>
3. MainActivity.java ¸¸µì´Ï´Ù.
public class MainActivity extends Activity { ListView m_ListView; CustomAdapter m_Adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Ä¿½ºÅÒ ¾î´ðÅÍ »ý¼º m_Adapter = new CustomAdapter(); // Xml¿¡¼­ Ãß°¡ÇÑ ListView ¿¬°á m_ListView = (ListView) findViewById(R.id.listView1); // ListView¿¡ ¾î´ðÅÍ ¿¬°á m_ListView.setAdapter(m_Adapter); m_Adapter.add("ÀÌ°Ç ¹¹Áö",1); m_Adapter.add("ÄðÄð",1); m_Adapter.add("ÄðÄðÄðÄð",0); m_Adapter.add("Àç¹ÌÀÖ°Ô",1); m_Adapter.add("³îÀڶ󱸳ªÈúÈú °¨»çÇÕ´Ï´Ù. µ¿Çع°°ú ¹éµÎ»êÀÌ ¸¶¸£°í ´âµµ·Ï ³îÀÚ ³îÀÚ ¿ì¸® ³îÀÚ",1); m_Adapter.add("Àç¹ÌÀÖ°Ô",1); m_Adapter.add("Àç¹ÌÀÖ°Ô",0); m_Adapter.add("2015/11/20",2); m_Adapter.add("Àç¹ÌÀÖ°Ô",1); m_Adapter.add("Àç¹ÌÀÖ°Ô",1); } }
Adapter ¿¡ ¿¬°áÇÏ¿© Ãß°¡ÇÏ´Â °£´ÜÇÑ ¿¹Á¦ ÀÔ´Ï´Ù. ½ÇÁ¦ 󸮴 CustomAdapter.java ÆÄÀÏ¿¡¼­´Â ¸ðµÎ ó¸®ÇÏ°Ô µË´Ï´Ù. ´ÙÀ½Àº CustomAdapter ¿¡¼­ ó¸®ÇÏ´Â ·ÎÁ÷À» º¸°Ú½À´Ï´Ù. 4. CustomAdapter.java ¸¸µì´Ï´Ù.
public class CustomAdapter extends BaseAdapter { public class ListContents{ String msg; int type; ListContents(String _msg,int _type) { this.msg = _msg; this.type = _type; } } private ArrayList m_List; public CustomAdapter() { m_List = new ArrayList(); } // ¿ÜºÎ¿¡¼­ ¾ÆÀÌÅÛ Ãß°¡ ¿äû ½Ã »ç¿ë public void add(String _msg,int _type) { m_List.add(new ListContents(_msg,_type)); } // ¿ÜºÎ¿¡¼­ ¾ÆÀÌÅÛ »èÁ¦ ¿äû ½Ã »ç¿ë public void remove(int _position) { m_List.remove(_position); } @Override public int getCount() { return m_List.size(); } @Override public Object getItem(int position) { return m_List.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final int pos = position; final Context context = parent.getContext(); TextView text = null; CustomHolder holder = null; LinearLayout layout = null; View viewRight = null; View viewLeft = null; // ¸®½ºÆ®°¡ ±æ¾îÁö¸é¼­ ÇöÀç È­¸é¿¡ º¸ÀÌÁö ¾Ê´Â ¾ÆÀÌÅÛÀº converView°¡ nullÀÎ »óÅ·Πµé¾î ¿È if ( convertView == null ) { // view°¡ nullÀÏ °æ¿ì Ä¿½ºÅÒ ·¹À̾ƿôÀ» ¾ò¾î ¿È LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.activity_chatitem, parent, false); layout = (LinearLayout) convertView.findViewById(R.id.layout); text = (TextView) convertView.findViewById(R.id.text); viewRight = (View) convertView.findViewById(R.id.imageViewright); viewLeft = (View) convertView.findViewById(R.id.imageViewleft); // Ȧ´õ »ý¼º ¹× Tag·Î µî·Ï holder = new CustomHolder(); holder.m_TextView = text; holder.layout = layout; holder.viewRight = viewRight; holder.viewLeft = viewLeft; convertView.setTag(holder); } else { holder = (CustomHolder) convertView.getTag(); text = holder.m_TextView; layout = holder.layout; viewRight = holder.viewRight; viewLeft = holder.viewLeft; } // Text µî·Ï text.setText(m_List.get(position).msg); if( m_List.get(position).type == 0 ) { text.setBackgroundResource(R.drawable.inbox2); layout.setGravity(Gravity.LEFT); viewRight.setVisibility(View.GONE); viewLeft.setVisibility(View.GONE); }else if(m_List.get(position).type == 1){ text.setBackgroundResource(R.drawable.outbox2); layout.setGravity(Gravity.RIGHT); viewRight.setVisibility(View.GONE); viewLeft.setVisibility(View.GONE); }else if(m_List.get(position).type == 2){ text.setBackgroundResource(R.drawable.datebg); layout.setGravity(Gravity.CENTER); viewRight.setVisibility(View.VISIBLE); viewLeft.setVisibility(View.VISIBLE); } // ¸®½ºÆ® ¾ÆÀÌÅÛÀ» ÅÍÄ¡ ÇßÀ» ¶§ À̺¥Æ® ¹ß»ý convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // ÅÍÄ¡ ½Ã ÇØ´ç ¾ÆÀÌÅÛ À̸§ Ãâ·Â Toast.makeText(context, "¸®½ºÆ® Ŭ¸¯ : "+m_List.get(pos), Toast.LENGTH_SHORT).show(); } }); // ¸®½ºÆ® ¾ÆÀÌÅÛÀ» ±æ°Ô ÅÍÄ¡ ÇßÀ»¶§ À̺¥Æ® ¹ß»ý convertView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // ÅÍÄ¡ ½Ã ÇØ´ç ¾ÆÀÌÅÛ À̸§ Ãâ·Â Toast.makeText(context, "¸®½ºÆ® ·Õ Ŭ¸¯ : "+m_List.get(pos), Toast.LENGTH_SHORT).show(); return true; } }); return convertView; } private class CustomHolder { TextView m_TextView; LinearLayout layout; View viewRight; View viewLeft; } }
ViewHolder ¿¡ ´ëÇÑ ¼Ò½º°¡ Æ÷ÇԵǾî ÀÖ½À´Ï´Ù. ViewHolder ´Â getViewÀÇ ¼Óµµ Çâ»óÀ» À§ÇØ »ç¿ëµË´Ï´Ù. ÇѹøÀÇ findViewByID ·Î Àç»ç¿ë Çϱâ À§ÇØ viewHolder¸¦ »ç¿ë ÇÕ´Ï´Ù. »ùÇüҽº ÀÔ´Ï´Ù.
chatui.zip