您的位置:首页 > 其它

分组ListView使用技巧

2013-03-11 10:51 393 查看
前言:

ListView通常显示比较大的数据量.例如,“通讯录”应用程序使用的 ListView 包含所有您的联系人。在此中Activity中每个联系人代表一个单一的item view。这种模式是很方便,因为在同一时间它显示在屏幕上的几次接触。换句话说,它为用户提供大型概述了他/她的联系人。但是,使用一个 ListView 部件是远远不够的......

如果让你开发了一个随机顺序显示所有联系人的通讯列表;解决的办法是理解和正常秩序中的所有联系人进行排序:按字母顺序排列。在另外的顺序,它通常是一个很好的做法,第几组数据。在“通讯录”应用程序实例,它归结为每个英文字母部分。

方法1:使用不同类型的视图ListView和更具体的adapter可以处理几种类型view。如果查询适配器接口,你会发现它包含两个具体方法:

getViewTypeCount()返回类型view AdapterView管理。大部分时间此方法返回1,因为所有项目的ListView类似。在这种情况下,返回2,ListView 的将处理两种类型的view:经常项目视图和属于分隔查看.

getItemViewType(INT)必须返回0(含)之间的一个整数getViewTypeCount() (inclusive )。给定的数字表示该类型的视图,在给定的位置。例如,我们可以确保返回的值是经常项目的小号0 和1的分隔符.

优势

让你管理itmes的几种类型

很容易理解

缺点

几乎没有大量代码。

在一个特定的位置获得该item可能有困难。比方说,我们有[S1,C1,C2,S2,C3,C4,C5] SN是N次的分离器和CN N次接触。第五次接触实际存储在我们的数据数组的第7次。这意味着你不能访问,不知道多少部分数据包含在N的前面接触到您的阵列中的N次接触。

方法2:利用GONE Visibility另一种方式的ListView sectioning 是使用的视图类的visibility属性。Android是能够动态测量和布局item view。在ListView的渲染系统,这两次传球被执行,只有当一个视图需要显示。换句话说,默认情况下, 一个ListView item view高度是可变的。巧妙设置分隔符的visiable。该算法是相当简单的,分隔符必须是View.VISIBLE时,该项目是第一个适配器或如果当前项目是在一个比前一个不同的组。如果没有这些条件进行了验证,我们将设置View.GONE的分隔。图形下面总结的

View Code

/*
* Copyright (C) 2011 Cyril Mottier (http://www.cyrilmottier.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.miss.sos.util;

import java.lang.ref.WeakReference;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

/**
* A particular {@link AsyncQueryHandler} allowing clients to be notified via a
* listener. The {@link NotifyingAsyncQueryHandler} also make sure no strong
* reference is kept on the given listener (as it is often a Context).
*
* @author Cyril Mottier
*/
public class NotifyingAsyncQueryHandler extends AsyncQueryHandler {

private WeakReference<NotifyingAsyncQueryListener> mListener;

/**
* Client may use this to listen to completed query operations.
*
* @author Cyril Mottier
*/
public interface NotifyingAsyncQueryListener {
void onQueryComplete(int token, Object cookie, Cursor cursor);
}

public NotifyingAsyncQueryHandler(ContentResolver resolver, NotifyingAsyncQueryListener listener) {
super(resolver);
setQueryListener(listener);
}

/**
* Assign the given {@link NotifyingAsyncQueryListener}.
*/
public void setQueryListener(NotifyingAsyncQueryListener listener) {
mListener = (listener != null) ? new WeakReference<NotifyingAsyncQueryListener>(listener) : null;
}

public void clearQueryListener() {
mListener = null;
}

public void startQuery(Uri uri, String[] projection) {
startQuery(-1, null, uri, projection, null, null, null);
}

public void startQuery(Uri uri, String[] projection, String sortOrder) {
startQuery(-1, null, uri, projection, null, null, sortOrder);
}

@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
final NotifyingAsyncQueryListener listener = (mListener == null) ? null : mListener.get();
if (listener != null) {
listener.onQueryComplete(token, cookie, cursor);
} else if (cursor != null) {
cursor.close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: