Android上 ExpandableListActivity及CursorTreeAdapter的使用简介

来源:互联网 发布:淘宝情趣内衣模特真人 编辑:程序博客网 时间:2024/06/05 08:07

      ExpandableListeActivity顾名思义是ListActivity的一种,但与ListActivy不同的是,ExpandableListeActivity的展示效果是有可以呈现多级目录的ExpandableListView。下面我简单介绍一下如何使用ExpandableListeActivity。

      首先,我们在想要实现的Activity继承ExpandableListeActivity如下:

public classGroupListActivity extends ExpandableListActivity

在这个布局中我们用到了布局文件setContentView(R.layout.group_list)中的group_list.xml。让我们来看看group_list里都有什么

<RelativeLayoutxmlns:android=http://schemas.android.com/apk/res/android

android:layout_width="fill_parent"android:layout_height="fill_parent"

android:orientation="vertical">

<ExpandableListViewandroid:layout_width="fill_parent"

android:layout_height="wrap_content"android:id="@android:id/list"

android:layout_alignParentTop="true"></ExpandableListView>

</RelativeLayout>

         可见,与ListActivity类似,我们在使用ExpandableListView的布局文件里必须声明一个ExpandableListView的控件,其ID为list。这样我们在自己的Activity里才能找到这个ExpandableListview。与ExpandableListview对应的Adapter有几种。在此我们只介绍CursorTreeAdapter,因为这个属于不是很常用的Adapter,国内的介绍也很少。CursorTreeAdapter是BaseExpandableListAdapter的一个子类。其实现代码如下:

public class GroupListAdapter extends CursorTreeAdapter {

 

    private GroupViewCachecache;

    private ChildViewCachechildCache;

    public static final int LoadHead = 100;

    private DrawabledefaultDrawable;

    private GroupListPhotoCachephotoCache;

    private Contextctx;

 

    public GroupListAdapter(Cursor cursor, Context context,boolean autoRequery) {

       super(cursor, context, autoRequery);

       // TODO Auto-generatedconstructor stub

       this.photoCache = GroupListPhotoCache.getInstance(context,this);

       this.defaultDrawable = context.getResources().getDrawable(

              com.littlerain.ui.R.drawable.skinclassic_default_thumbnai);

       this.ctx = context;

    }

 

    @Override

    protected void bindChildView(View view, Context context, Cursor cursor,

           boolean isLastChild) {

       // TODO Auto-generatedmethod stub

       childCache = (ChildViewCache) view.getTag();

       // 显示名字

       cursor.copyStringToBuffer(cursor.getColumnIndex(Contacts.DISPLAY_NAME),

              childCache.nameBuffer);

       int size =childCache.nameBuffer.sizeCopied;

       childCache.nameView.setText(childCache.nameBuffer.data, 0, size);

 

       /* 显示头像 */

       long photoId = cursor.getLong(cursor.getColumnIndex(Contacts.PHOTO_ID));

       childCache.photoView.setImageDrawable(photoCache.getHeadImg(photoId)

              .getHead());

       final String lookupKey = cursor.getString(cursor

              .getColumnIndex(Contacts.LOOKUP_KEY));

       childCache.photoView.assignContactUri(Contacts.getLookupUri(

              cursor.getLong(cursor.getColumnIndex(Data.CONTACT_ID)),

              lookupKey));

       /* 显示自定义信息 */

       String ringstoneUri = null;

       ringstoneUri = cursor.getString(cursor

              .getColumnIndex(Contacts.CUSTOM_RINGTONE));

       if (ringstoneUri ==null) {

 

           childCache.info.setText("铃声:默认");

       } else {

 

           Cursor c = ctx.getContentResolver().query(Uri.parse(ringstoneUri),

                  new String[] { MediaColumns.TITLE },null,null,

                  null);

           if (c.moveToNext()) {

              childCache.info.setText("铃声:"

                     + c.getString(c

                            .getColumnIndex(MediaColumns.TITLE)));

           }

       }

       view.setId((int) cursor.getLong(cursor.getColumnIndex(Data.CONTACT_ID)));

    }

    @Override

    protected void bindGroupView(View view, Context context, Cursor cursor,

           boolean isExpanded) {

 

       cache = (GroupViewCache) view.getTag();

       cache.groupName.setText(cursor.getString(cursor

              .getColumnIndex(Groups.TITLE)));

       cache.groupCount.setText("["

              + ContactsManager.getInstance().getMemberCountByGroupId(

                     cursor.getString(cursor.getColumnIndex(BaseColumns._ID)))

              + "]");

       view.setId((int) cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)));

    }

    @Override

    protected Cursor getChildrenCursor(Cursor groupCursor) {

       // TODO Auto-generatedmethod stub

       Cursor c = ContactsManager.getInstance().getGroupMember(

              groupCursor.getString(groupCursor.getColumnIndex(BaseColumns._ID)));

       return c;

    }

    @Override

    protected View newChildView(Context context, Cursor cursor,

           boolean isLastChild, ViewGroup parent) {

       // TODO Auto-generatedmethod stub

       LayoutInflater inflater = LayoutInflater.from(context);

       View view = inflater.inflate(R.layout.contact_layout,null);

       ChildViewCache cache = new ChildViewCache();

       cache.nameView = (TextView) view

              .findViewById(com.littlerain.ui.R.id.contact_name);

       cache.photoView = (QuickContactBadge) view

              .findViewById(com.littlerain.ui.R.id.contact_head);

       cache.info = (TextView) view

              .findViewById(com.littlerain.ui.R.id.contact_info);

       view.setTag(cache);

       return view;

    }

    @Override

    protected ViewnewGroupView(Contextcontext, Cursor cursor,

           boolean isExpanded, ViewGroup parent) {

       // TODO Auto-generatedmethod stub

       LayoutInflater inflater = LayoutInflater.from(context);

       View view = inflater.inflate(R.layout.group_list_view,null);

       GroupViewCache cache = new GroupViewCache();

       cache.groupName = (TextView) view.findViewById(R.id.group_name);

       cache.groupCount = (TextView) view

              .findViewById(R.id.group_member_count);

       view.setTag(cache);

       return view;

    }

    final class GroupViewCache {

       public TextViewgroupName;

       public TextViewgroupCount;

    }

    final class ChildViewCache {

       public TextViewnameView;

       public QuickContactBadgephotoView;

       public TextViewinfo;

       public CharArrayBuffernameBuffer =new CharArrayBuffer(128);

    }

    @Override

    public boolean isChildSelectable(int groupPosition,int childPosition) {

       // TODO Auto-generatedmethod stub

       return true;

    }

}

需要实现的方法有bindChildView,bindGroupView,getChildrenCursor,newChildView,newGroupView。其中newChildView是每个分组里的子列表每个列表的view,newGroupView是每个分组的标题列表的view。BindChildView及bindGroupView是分别将数据render到相应的view上。getChildrenCursor是为子列表获取Cursor。之后在需要的Activity中使用以下代码即可:

GroupListAdapter adapter = newGroupListAdapter(groupCursor, this, true);

       setListAdapter(adapter);

       getExpandableListView().setCacheColorHint(0);//拖动时避免出现黑色

       getExpandableListView().setDivider(null);//去掉每项下面的黑线(分割线)

       getExpandableListView().setGroupIndicator(

              getResources().getDrawable(R.drawable.expander_ic_folder));//自定义下拉图标

以上只是对ExpandableListActivity作了一个入门的介绍,帮助大家快速上手。若要详细了解具体的每个方法,请自行查阅Android SDK。

from:http://blog.csdn.net/pku_android/article/details/7597282

0 0
原创粉丝点击