------

[ AD ] Port Monitor ( Try to use a Best WebSite Monitoring Tool )

------


구분자 헤더
 
  1. <!-- list_header.xml -->  
  2. <TextView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/list_header_title"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:paddingTop="2dip"  
  8.     android:paddingBottom="2dip"  
  9.     android:paddingLeft="5dip"  
  10.     style="?android:attr/listSeparatorTextViewStyle" />  
 단순 리스트
 
  1. <!-- list_item.xml -->  
  2. <TextView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/list_item_title"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:paddingTop="10dip"  
  8.     android:paddingBottom="10dip"  
  9.     android:paddingLeft="15dip"  
  10.     android:textAppearance="?android:attr/textAppearanceLarge"  
  11.     />  
  12.   
 복합 리스트
 
  1. <!-- list_complex.xml -->  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:orientation="vertical"  
  7.     android:paddingTop="10dip"  
  8.     android:paddingBottom="10dip"  
  9.     android:paddingLeft="15dip"  
  10.     >  
  11.     <TextView  
  12.         android:id="@+id/list_complex_title"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:textAppearance="?android:attr/textAppearanceLarge"  
  16.         />  
  17.     <TextView  
  18.         android:id="@+id/list_complex_caption"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:textAppearance="?android:attr/textAppearanceSmall"  
  22.         />  
  23. </LinearLayout>  



하나의 Interface로 여러개의 영역 제공하는 SeparatedListAdapter

addSection()를 사용한 후에 child영역을 만든다.

Now let’s create the actual SeparatedListAdapter class which provides a single interface to multiple sections of other Adapters. 

이제는 SeperatedListAdaper를 만들어 보자. 그것은 하나의 인터페이스로 다른 어댑터의 여러개의 섹션을 제공한다. 

After using addSection() to construct the child sections, you can easily use ListView.setAdapter() to present the now-separated list to users.

addSection()를 사용하고 자식섹션을 생성한다, 쉽게 ListViewsetAdaper()를 사용해서 사용자에게 분리된 리스트를 보여준다.


As for the Adapter internals, to correctly find the selected item among the child Adapters, we walk through subtracting from the original position until we find either a header (position = 0) or item in the current child Adapter (position < size).



Here’s the source for SeparatedListAdapter: 아래쪽에 SeparatedListAdapter을 사용하는 소스가 있다.

 

public class SeparatedListAdapter extends BaseAdapter {  

  1.   
  2.     public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();  
  3.     public final ArrayAdapter<String> headers;  
  4.     public final static int TYPE_SECTION_HEADER = 0;  
  5.   
  6.     public SeparatedListAdapter(Context context) {  
  7.         headers = new ArrayAdapter<String>(context, R.layout.list_header);  
  8.     }  
  9.   
  10.     public void addSection(String section, Adapter adapter) {  
  11.         this.headers.add(section);  
  12.         this.sections.put(section, adapter);  
  13.     }  
  14.   
  15.     public Object getItem(int position) {  
  16.         for(Object section : this.sections.keySet()) {  
  17.             Adapter adapter = sections.get(section);  
  18.             int size = adapter.getCount() + 1;  
  19.   
  20.             // check if position inside this section  , 섹션 안쪽의 위치인가? 
  21.             if(position == 0return section;  
  22.             if(position < size) return adapter.getItem(position - 1);  
  23.   
  24.             // otherwise jump into next section  , 아니면 다음 섹션으로 넘어간다
  25.             position -= size;  
  26.         }  
  27.         return null;  
  28.     }  
  29.   
  30.     public int getCount() {  
  31.         // total together all sections, plus one for each section header  
  32.         int total = 0;  
  33.         for(Adapter adapter : this.sections.values())  
  34.             total += adapter.getCount() + 1;  
  35.         return total;  
  36.     }  
  37.   
  38.     public int getViewTypeCount() {  
  39.         // assume that headers count as one, then total all sections  
  40.         int total = 1;  
  41.         for(Adapter adapter : this.sections.values())  
  42.             total += adapter.getViewTypeCount();  
  43.         return total;  
  44.     }  
  45.   
  46.     public int getItemViewType(int position) {  
  47.         int type = 1;  
  48.         for(Object section : this.sections.keySet()) {  
  49.             Adapter adapter = sections.get(section);  
  50.             int size = adapter.getCount() + 1;  
  51.   
  52.             // check if position inside this section, 섹션 안쪽의 위치인가?    
  53.             if(position == 0return TYPE_SECTION_HEADER;  
  54.             if(position < size) return type + adapter.getItemViewType(position - 1);  
  55.   
  56.             // otherwise jump into next section  , 아니면 다음 섹션으로 넘어간다
  57.             position -= size;  
  58.             type += adapter.getViewTypeCount();  
  59.         }  
  60.         return -1;  
  61.     }  
  62.   
  63.     public boolean areAllItemsSelectable() {  
  64.         return false;  
  65.     }  
  66.   
  67.     public boolean isEnabled(int position) {  
  68.         return (getItemViewType(position) != TYPE_SECTION_HEADER);  
  69.     }  
  70.   
  71.     @Override  
  72.     public View getView(int position, View convertView, ViewGroup parent) {  
  73.         int sectionnum = 0;  
  74.         for(Object section : this.sections.keySet()) {  
  75.             Adapter adapter = sections.get(section);  
  76.             int size = adapter.getCount() + 1;  
  77.   
  78.             // check if position inside this section  , 섹션 안쪽의 위치인가?    
  79.             if(position == 0return headers.getView(sectionnum, convertView, parent);  
  80.             if(position < size) return adapter.getView(position - 1, convertView, parent);  
  81.   
  82.             // otherwise jump into next section  
  83.             position -= size;  
  84.             sectionnum++;  
  85.         }  
  86.         return null;  
  87.     }  
  88.   
  89.     @Override  
  90.     public long getItemId(int position) {  
  91.         return position;  
  92.     }  
  93.   
  94. }  
 

As expected, it correctly prevents the section headers from being selected, and seamlessly stiches together the various Adapters.

This approach also uses convertView correctly as long as the child Adapters return getItemViewType() and getViewTypeCount() normally. No special changes are needed for an Adapter to become a child.

Now let’s use SeparatedListAdapter in some example code. We use the XML layouts defined earlier to create an ArrayAdapter and an advanced two-row SimpleAdapter, and then add both as sections to our SeparatedListAdapter.

 
  1. public class ListSample extends Activity {  
  2.   
  3.     public final static String ITEM_TITLE = "title";  
  4.     public final static String ITEM_CAPTION = "caption";  
  5.   
  6.     public Map<String,?> createItem(String title, String caption) {  
  7.         Map<String,String> item = new HashMap<String,String>();  
  8.         item.put(ITEM_TITLE, title);  
  9.         item.put(ITEM_CAPTION, caption);  
  10.         return item;  
  11.     }  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle icicle) {  
  15.         super.onCreate(icicle);  
  16.   
  17.         List<Map<String,?>> security = new LinkedList<Map<String,?>>();  
  18.         security.add(createItem("Remember passwords""Save usernames and passwords for Web sites"));  
  19.         security.add(createItem("Clear passwords""Save usernames and passwords for Web sites"));  
  20.         security.add(createItem("Show security warnings""Show warning if there is a problem with a site's security"));  
  21.   
  22.         // create our list and custom adapter  
  23.         SeparatedListAdapter adapter = new SeparatedListAdapter(this);  
  24.         adapter.addSection("Array test"new ArrayAdapter<String>(this,  
  25.             R.layout.list_item, new String[] { "First item""Item two" }));  
  26.         adapter.addSection("Security"new SimpleAdapter(this, security, R.layout.list_complex,  
  27.             new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));  
  28.   
  29.         ListView list = new ListView(this);  
  30.         list.setAdapter(adapter);  
  31.         this.setContentView(list);  
  32.   
  33.     }  
  34.   
  35. }  

The resulting interface behaves just like the browser preferences list,

and you could easily create other custom Adapters to insert into the various sections,

such as including icons or checkboxes.

'0.일반개발' 카테고리의 다른 글

EditText 사용하기  (0) 2010.09.14
안드로이드 웹서버 통신 HTTP  (0) 2010.09.14
안드로이드 타원형 박스  (0) 2010.09.14
날짜 안드로이드 자바 일수 계산  (0) 2010.09.14
How to Display Thumbnails of Images  (0) 2010.09.14

+ Recent posts