04 커스텀어댑터뷰 (Custom Adapter View)
커스텀어댑터뷰 (Custom Adapter View) 커스텀어댑터뷰 (Custom Adatper View) 란? u 어댑터뷰의항목하나는단순한문자열이나이미지뿐만아니라, 임의의뷰가될수 있음 이미지뷰 u 커스텀어댑터뷰설정절차 1 2 항목을위한 XML 레이아웃정의 어댑터정의 3 어댑터를생성하고어댑터뷰객체에연결
1. 항목 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="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iconitem" 이미지뷰 android:layout_width="@dimen/icon_size" android:layout_height="@dimen/icon_size" android:scaletype="centerinside" android:padding="@dimen/icon_padding" android:layout_gravity="center_vertical" android:layout_weight="1" /> 1/2 https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/res/layout/item.xml
1. 항목 XML 레이아웃정의 <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2"> <TextView android:id="@+id/textitem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/coloraccent" android:textsize="@dimen/list_item_text_size1" android:padding="@dimen/list_item_padding" /> <TextView android:id="@+id/textitem2" /> </LinearLayout> </LinearLayout> 이미지뷰 2/2 https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/res/layout/item.xml
2. 어댑터정의 class MyItem { int micon; // image resource String nname; // text String nage; // text MyItem(int aicon, String aname, String aage) { micon = aicon; nname = aname; nage = aage; class MyAdapter extends BaseAdapter { private Context mcontext; private int mresource; private > mitems = new ArrayList<MyItem>(); ArrayList<MyItem public MyAdapter(Context context, int resource, ArrayList<MyItem> items) { mcontext = context; mitems = items; mresource = resource; public int getcount() { return mitems.size(); public Object getitem(int position) { return mitems.get(position); public long getitemid(int position) { return position; 1/2 https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/java/com/kwanwoo/android/customadapterview/myadapter.java
2. 어댑터정의 public View getview(int position, View convertview, ViewGroup parent) { if (convertview == null) { LayoutInflater inflater = (LayoutInflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(mresource, parent,false); // Set Icon ImageView icon = (ImageView) convertview.findviewbyid(r.id.iconitem); icon.setimageresource(mitems.get(position).micon); // Set Text 01 TextView name = (TextView) convertview.findviewbyid(r.id.textitem1); name.settext(mitems.get(position).nname); // Set Text 02 TextView age = (TextView) convertview.findviewbyid(r.id.textitem2); age.settext(mitems.get(position).nage); return convertview; 2/2 https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/java/com/kwanwoo/android/customadapterview/myadapter.java
3. 어댑터생성및연결 public class MainActivity extends AppCompatActivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // 데이터원본준비 ArrayList<MyItem> data = new ArrayList<MyItem>(); data.add(new MyItem(R.drawable.sample_0, "Bella", "1")); data.add(new MyItem(R.drawable.sample_1, "Charlie", "2")); // 어댑터생성 MyAdapter adapter = new MyAdapter(this, R.layout.item, data); // 어댑터연결 ListView list = (ListView) findviewbyid(r.id.listview); list.setadapter(adapt); https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/java/com/kwanwoo/android/customadapterview/mainactivity.java
항목클릭이벤트처리 u AdapterView 의항목이클릭될때, 호출되는 callback method 의인터페이스 public static interface AdapterView.OnItemClickListener { abstract void onitemclick(adapterview<?> parent, View view, int position, long id); 파라미터 parent view position id 설명클릭이벤트가발생된 AdapterView 실제클릭된 AdapterView 안의 View 어댑터내에서클릭된항목 / 뷰의위치클릭된항목의 id
항목클릭이벤트처리 MyAdapter adapter = new MyAdapter(this, R.layout.item, data); listview.setonitemclicklistener(new AdapterView.OnItemClickListener() { public void onitemclick(adapterview<?> parent, View vclicked, int position, long id) { // 클릭된뷰 (vclicked) 안에서 id 가 textitem1 인의레이블을얻어옴 // String name = (String)((TextView)vClicked.findViewById(R.id.textItem1)).getText(); // 어댑터 (adapter) 내의 position 위치의항목의 nname 값을읽어옴 String name = ((MyItem)adapter.getItem(position)).nName; ); Toast.makeText(MainActivity.this, name + " selected", Toast.LENGTH_SHORT).show(); https://github.com/kwanulee/android/blob/master/examples/customadapterview/app/src/main/java/com/kwanwoo/android/customadapterview/mainactivity.java#l37-l45