Monday August 18, 2008 at 4:42 PM
Earlier today thelatest Android 0.9 SDK was released, and it’s packed full of wonderful changes. As you play around, you might see ListViews split into sections using separating headers. (Example shown on the right is the browser settings list.)
There isn’t an easy way of creating these separated lists, so I’ve put together SeparatedListAdapter which does it quickly. To summarize, we’re creating a new BaseAdapter that can contain several other Adapters, each with their own section headers.
First let’s create some simple XML layouts to be used for our lists: first the header view, then two item views that we’ll use later for the individual lists. (Thanks to Romain Guy for helping me find existing styles to keep these XML layouts nice and tidy.)
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/list_header_title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingTop="2dip"
- android:paddingBottom="2dip"
- android:paddingLeft="5dip"
- style="?android:attr/listSeparatorTextViewStyle"/>
-
- <TextView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/list_item_title"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingTop="10dip"
- android:paddingBottom="10dip"
- android:paddingLeft="15dip"
- android:textAppearance="?android:attr/textAppearanceLarge"
- />
-
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:paddingTop="10dip"
- android:paddingBottom="10dip"
- android:paddingLeft="15dip"
- >
- <TextView
- android:id="@+id/list_complex_title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- />
- <TextView
- android:id="@+id/list_complex_caption"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceSmall"
- />
- LinearLayout>
Now let’s create the actual SeparatedListAdapter class which provides a single interface to multiple sections of other Adapters. After using addSection() to construct the child sections, you can easily use ListView.setAdapter() to present the now-separated list to users.
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 <>
Here’s the source for SeparatedListAdapter:
- publicclassSeparatedListAdapterextendsBaseAdapter {
-
- publicfinalMapsections =newLinkedHashMap();
- publicfinalArrayAdapterheaders;
- publicfinalstaticintTYPE_SECTION_HEADER =0;
-
- publicSeparatedListAdapter(Context context) {
- headers =newArrayAdapter(context, R.layout.list_header);
- }
-
- publicvoidaddSection(String section, Adapter adapter) {
- this.headers.add(section);
- this.sections.put(section, adapter);
- }
-
- publicObject getItem(intposition) {
- for(Object section :this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- intsize = adapter.getCount() +1;
-
- if(position ==0)returnsection;
- if(position <>returnadapter.getItem(position -1);
-
- position -= size;
- }
- returnnull;
- }
-
- publicintgetCount() {
- inttotal =0;
- for(Adapter adapter :this.sections.values())
- total += adapter.getCount() +1;
- returntotal;
- }
-
- publicintgetViewTypeCount() {
- inttotal =1;
- for(Adapter adapter :this.sections.values())
- total += adapter.getViewTypeCount();
- returntotal;
- }
-
- publicintgetItemViewType(intposition) {
- inttype =1;
- for(Object section :this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- intsize = adapter.getCount() +1;
-
- if(position ==0)returnTYPE_SECTION_HEADER;
- if(position <>returntype + adapter.getItemViewType(position -1);
-
- position -= size;
- type += adapter.getViewTypeCount();
- }
- return-1;
- }
-
- publicbooleanareAllItemsSelectable() {
- returnfalse;
- }
-
- publicbooleanisEnabled(intposition) {
- return(getItemViewType(position) != TYPE_SECTION_HEADER);
- }
-
- @Override
- publicView getView(intposition, View convertView, ViewGroup parent) {
- intsectionnum =0;
- for(Object section :this.sections.keySet()) {
- Adapter adapter = sections.get(section);
- intsize = adapter.getCount() +1;
-
- if(position ==0)returnheaders.getView(sectionnum, convertView, parent);
- if(position <>returnadapter.getView(position -1, convertView, parent);
-
- position -= size;
- sectionnum++;
- }
- returnnull;
- }
-
- @Override
- publiclonggetItemId(intposition) {
- returnposition;
- }
-
- }
分享到:
相关推荐
### ASP.NET - Module 4: Separating Code from Content #### 概述 在ASP.NET开发过程中,将代码与内容分离是一种常见的做法,它能够提高代码的可维护性、可读性和可重用性。本模块将详细介绍如何有效地实现这一...
key-value store with a performance-oriented data layout that separates keys from values to minimize I/O amplification. The design of WiscKey is highly SSD optimized, leveraging both the sequential and...
- **Separate Interface and Implementation**: Explanation of separating the interface (header files) from the implementation (source files). **Programming Project 10.3**: This project likely involves ...
Knockout MVC is a library for ASP.NET MVC ... such as regions and complex bindings, as well as how to use the MVMM design pattern to create powerful sites separating the model, logic, and view layers.
In this paper, we propose a simple and efficient separation method with a radial varying phase. In the method, specific radial varying phases are designed and modulated for different OAM modes. The ...
机器学习技法 02_Large-Margin_Separating_Hyperplane_14-17.mp4
Chapter 7, Separating Concerns with Typed Services, discusses how it’s bad practice to allow our logic to live in our components. In such a case, Angular allows you to provide API methods via ...
"神经网络技术在ASTER影像中的土地表面发射率和温度分离" 基于ASTER影像,提出了一个神经网络技术来分离土地表面发射率和温度。该技术通过建立四个辐射传输方程式,涉及六个未知数(平均大气温度、土地表面温度和四...
在本篇论文“用带限制签名的描述逻辑概念分离数据示例_Separating Data Examples by Description Logic Concepts with Restricted Signatures”中,研究者探讨了如何在存在本体的情况下,使用描述逻辑(Description ...
"Separating Axis Theorem"(分离轴定理)是一种广泛应用于2D刚体碰撞检测的理论。本教程将深入探讨如何扩展这一方法以实现更复杂的碰撞响应。 分离轴定理的基本思想是,如果两个对象不相交,那么存在至少一条直线...
### 数据库权限分离模型在数据保护中的应用 #### 摘要 本文介绍了一种针对数据保护的数据库权限分离模型,旨在通过将传统用户权限分解为子组来增强数据库的安全性。该方法基于最小特权原则,该原则是设计安全...
Project headers are headers included in the project itself and not part of any external framework or library. #### C/C++ Compiler Build Settings ##### ALWAYS_SEARCH_USER_PATHS (Always Search User ...
藏经阁——基于分层压缩的异构存储中的热冷数据分离 在阿里巴巴的业务场景中,例如联系人与聊天、AI机器人、风险控制、账单处理、物流追踪以及交易额(GMV)等,普遍存在大量的数据存储需求。这些场景的一个共同特征...
在探究玉米秸秆皮穰分离设备的设计与试验这一主题中,我们可以归纳出以下知识点: 1. 玉米秸秆的应用价值:玉米秸秆是一种丰富的可再生资源,主要由外皮、芯和叶组成。其外皮中含有大量适于造纸和制造胶合板的优质...
碰撞检测通常采用分离轴定理(Separating Axis Theorem)来判断两个几何形状是否相交,并确定接触点。一旦检测到碰撞,就需要调整每个台球的速度,使得它们按照物理规则反弹。 此外,游戏状态的管理也是关键。这...
Sculpture takes you to the next level of abstraction by separating the problem from the implementation technology. Sculpture takes you to the domain specific programming by adding your specifications...