现在的位置: 首页 > 综合 > 正文

android播放器(music player)源码分析2(BaseExpandableListAdapter, SimpleCursorTreeAdapter

2012年12月03日 ⁄ 综合 ⁄ 共 2147字 ⁄ 字号 评论关闭

1) SimpleExpandableListAdapter BaseExpandableListAdapter

  其构造函数接受八个参数

Java代码  
  1. this(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, lastChildLayout, childFrom, childTo);  

 

 

一般在使用中 expandedGroupLayout, collapsedGroupLayout 是一个参数生成的, childLayout, lastChildLayout 也是一个参数生成的。之所以区别它们是由于存在这样的需求:展示不同动作下不同的 view (展开和合起)、展示不同组之间最后一个孩子节点的现实布局。

 

这里需要注意一点在 BaseExpandableListAdapter 中,如果使用以下类似 newGroupView 方法

Java代码  
  1. public View newGroupView(boolean isExpanded, ViewGroup parent) {   
  2.   
  3.  return mInflater.inflate((isExpanded) ? mExpandedGroupLayout : mCollapsedGroupLayout,   
  4.   
  5. parent, false);  

 

 

在绑定其内容时要注意以下几点:

  1. 其绑定的内容一定要和 groupPosition 相对应,其内容最好不要动态生成否则会导致不稳定的绑定。类似如果是要绑定孩子节点的内容,则要和 groupPosition childPosition 一一对应。并且每次调用都需要重写绑定一次,不能根据 convertView 进行判定。( convertView getChildView 等方法里面的参数)
  2. 重写 getChild 方法时需要返回自己所需的数据结构,视情况定义自己的数据结构。
  3. mInflater.inflate 方法在实例化一个 layout 时,可能会创建了别的 group layout ,(这个暂时没有弄清楚为什么会有这样的原因,帮忙补充一下)。可是当使用   TextView textView = new TextView(Class.this) 时,不会产生这样的问题,每次绑定内容时 convertView 一定是空的。

 

 

2 SimpleCursorTreeAdapter ResourceCursorTreeAdapter

   该类接受一个 cursor 作为参数,而 musicplayer 中则接受一个 null 值。这里使用 AsyncQueryHandler 生成内部的 cursor

   其流程如下:

  1. 首先创建在适配其中创建 AsyncQueryHandler ,此时 cursor 为空
  2. 在主程序中调用适配器的 AsyncQueryHandler 开始一个 sql 语句,此时在重载的 completeXX 方法中将生成的 cursor 赋给适配器

 

Java代码  
  1. @Override  
  2. protected void onQueryComplete(int token, Object cookie, Cursor cursor) {   
  3.     //Log.i("@@@", "query complete: " + cursor.getCount() + "   " + mActivity);   
  4.     if (cursor != null) {   
  5.         cursor = mActivity.mergedCursor(cursor);   
  6.     }   
  7.     mActivity.init(cursor);   
  8. }  

 

 

      3.适配器自动调用 get bind 方法生成布局。在绑定其内容时要注意使用了 cursor 不能够自定义 group child 的大小,其大小由 cursor 指定

抱歉!评论已关闭.