注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

BaseExpandableListAdapter  

2010-06-03 15:31:09|  分类: 数据适配器 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
public abstract class
BaseExpandableListAdapter
    extends Object
    implements ExpandableListAdapter HeterogeneousExpandableList
Class Overview
Base class for a ExpandableListAdapter used to provide data and Views from some data to an expandable list view.
Adapters inheriting this class should verify that the base implementations of getCombinedChildId(long, long) 
and getCombinedGroupId(long) are correct in generating unique IDs from the group/children IDs. 

    BaseExpandableListAdapter实现了接口ExpandableListAdapter
    BaseExpandableListAdapter的子类
        CursorTreeAdapter
            ResourceCursorTreeAdapter
                SimpleCursorTreeAdapter

BaseExpandableListAdapter主要实现了ExpandableListAdapter接口的四个方法
getCombinedChildId(long, long)
    用于产生child的id。注意id一定要是唯一.在BaseExpandableListAdapter实现里把最高位置成了1。也就是说该id始终是个负数
getCombinedGroupId(long)
    用于产生group的id。注意id一定要是唯一.在BaseExpandableListAdapter实现里把最高位置成了0。也就是说该id始终是个正数
void        onGroupCollapsed(int groupPosition)
    Called when a group is collapsed.
Group收缩时调用。好像只是个监听接口。当Group收缩时,ExpandableListView回调用。
void     onGroupExpanded(int groupPosition)
    Called when a group is expanded.
    Group展开时调用But how to use?
    Group展开时调用。好像只是个监听接口。当Group展开时,ExpandableListView回调用
注意:对于onGroupCollapsed和onGroupExpanded,没发现BaseExpandableListAdapter在两个函数中实现什么功能。
可能只是个空实现。目的就为了让用户不再浪费时间去做个空实现。
还未实现的ExpandableListAdapter接口方法有六个:
abstract Object        getChild(int groupPosition, int childPosition)
    Gets the data associated with the given child within the given group.
abstract long     getChildId(int groupPosition, int childPosition)
    Gets the ID for the given child within the given group.
abstract View     getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    Gets a View that displays the data for the given child within the given group.
abstract int     getChildrenCount(int groupPosition)
    Gets the number of children in a specified group.
abstract Object        getGroup(int groupPosition)
    Gets the data associated with the given group.
abstract int     getGroupCount()
    Gets the number of groups.
abstract long     getGroupId(int groupPosition)
    Gets the ID for the group at the given position.
abstract View     getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    Gets a View that displays the given group.
实例1
HelloExpandableListAdapter.java文件
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnChildClickListener;
public class HelloExpandableListAdapter extends Activity {
    private String mMailboxList[]={ 
            "Inbox", "Sentbox", "Draft", 
            "Outbox", "Email inbox", "Conversation",
            "Trash", "My folders"
            };
    
    final static  String mLoading = "Loading message...";
        
    private String mSender[][] = { {"Andrew1", "Andrew2", "Andrew3", "Andrew4","Andrew5", "Andrew6"}, 
            {"Jeff1", "Jeff2", "Jeff3", "Jeff4", "Jeff5", "Jeff6", "Jeff7"},
            {"ED1", "ED2", "ED3", "ED4", "ED5"}, 
            {"Rachel1", "Rachel2", "Rachel3", "Rachel4"},
            {"Sean1", "Sean2", "Sean3", "Sean4", "Sean5"},
            {"Karen1", "Karen2", "Karen3", "Karen4", "Karen5", "Karen6", "Karen7"},
            {"Lisa1", "Lisa2", "Lisa3", "Lisa4", "Lisa5"},
            {"Rain1", "Rain2", "Rain3", "Rain4", "Rain5"}};
    private String mMailContext[][] = { {"I'm going to go to watch movie!", "Hello....", "Nice to meet you", "Call me plz..", "hi",  "Call me plz.." }, 
            {"hello" , "Call me plz.." , "hello", "I'm going to go to watch movie!", "Hello....", "Nice to meet you", "Call me plz.."},
            {"hi",  "Call me plz.." , "hi" , "Call me plz.." , "hi"},
            {"I'm going to go to watch movie!", "Hello....", "Nice to meet you", "Call me plz.."}, 
            {"hi",  "Call me plz.." , "hi" , "Call me plz.." , "hi"},
            {"12312412","12414124","12412421","458645754","dfgsdgsdf","sdfgdfgh","sdfgdsfgh"},
            {"vclkj","lkvoajh","alskdjfoivj","jvlkjasldkf","79837214"},
            {"975432","kbvhuoisj","4362435,","342532","236dgasdt"}};    

    private String mDate[][] = { {"11/22/09", "11/23/08", "09/11/07", "02/03/05", "00/09/09", "11/11/11"}, 
            {"12/12/12", "12/25/09", "01/03/09", "11/22/09", "11/23/08","09/11/07", "02/03/05"}, 
            {"00/09/09", "11/11/11", "12/12/12", "12/25/09", "01/03/09"},
            {"11/22/09", "11/23/08", "09/11/07", "02/03/05"}, 
            {"00/09/09", "11/11/11", "12/12/12", "12/25/09", "01/03/09"},
            {"12/12/12", "12/25/09", "01/03/09", "11/22/09", "11/23/08","09/11/07", "02/03/05"},
            {"00/09/09", "11/11/11", "12/12/12", "12/25/09", "01/03/09"},
            {"00/09/09", "11/11/11", "12/12/12", "12/25/09", "01/03/09"}};
    ExpandableListAdapter mAdapter;
    protected ArrayList<GroupItem> groupList = new ArrayList<GroupItem>(); 
    ArrayList<ArrayList<ChildItem>> childrenList=new ArrayList<ArrayList<ChildItem>>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.ExpandableListView01);
        
        // initializing Expanded mailbox list data
        for(int i = 0; i < mMailboxList.length; i++) {
            GroupItem item = new GroupItem(mMailboxList[i]);
            
            groupList.add(item);
            ArrayList<ChildItem> childItems=new ArrayList<ChildItem>();
            ChildItem items = new ChildItem( mLoading, null, null);
            childItems.add(items);  
            childrenList.add(childItems);
        }
        
        // Set up our adapter
        mAdapter = new ExpandedMailboxListAdapter(this,groupList,childrenList);
        expandableListView.setAdapter(mAdapter);
        expandableListView.setGroupIndicator(null); // Group indicator disable
        
        expandableListView.setOnGroupClickListener(new OnGroupClickListener(){
               public boolean onGroupClick(ExpandableListView mExpandableList, View arg1, 
                    final int groupPosition, long id) {          
                    //这里的id就是getGroupId中得到的           
                 Log.i("hubin","G:"+groupPosition+" id:"+id);
                   new CountDownTimer(3000, 50) {
                        public void onTick(long millisUntilFinished) {
                        
                        }
                        public void onFinish() {
                            expandableListChange( groupPosition );                            
                        }
                    }.start();                               
                return false;
            }
          
        });
        expandableListView.setOnChildClickListener(new OnChildClickListener(){
            public boolean onChildClick(ExpandableListView  parent, View  v, int groupPosition, int childPosition, long id)
            {
            //这里的id就是getChildId中得到的
                Log.i("hubin","c:"+groupPosition+" id:"+id);
                return false;
            }
        }
        );
    }

    private void expandableListChange(int groupPosition){
        ArrayList<ChildItem> childrenItemList=childrenList.get(groupPosition);
        childrenItemList.clear();
        
for (int i = 0; i < mSender[groupPosition].length; i++) {
            ChildItem items = new ChildItem( mSender[groupPosition][i]mMailContext[groupPosition][i]mDate[groupPosition][i]);            
            childrenItemList.add(i,items);
        }
        
        ((BaseExpandableListAdapter)mAdapter).notifyDataSetChanged();
    }
}
    class ExpandedMailboxListAdapter extends BaseExpandableListAdapter {
    
    private TextView mMailbox;
    private ImageView mMailboxIcon;
    
    Context context;
    ArrayList<GroupItem> groupList;
    ArrayList<ArrayList<ChildItem>> childrenList;
    ExpandedMailboxListAdapter(Context context,ArrayList<GroupItem> groupList,ArrayList<ArrayList<ChildItem>> childrenList)
    {
        this.context=context;
        this.groupList=groupList;
        this.childrenList=childrenList;

    }
    /* 
     * (non-Javadoc)
     * @see android.widget.ExpandableListAdapter#getChild(int, int)
     */

    public Object getChild(int arg0, int arg1) {    
        return null;
    }

    public long getChildId(int groupPosition, int childPosition) {
         return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
         return childrenList.get(groupPosition).size();
    }
    final static  String mLoading = "Loading message...";
    /**
     * Get child view and set child data.
     * convertView is reused view
     */

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LinearLayout mTodoView = null;
        TextView mailSender;
        TextView content;
        TextView subDate;
        ProgressBar progress;
        
if(convertView==null){
            LayoutInflater vi = null;
                   vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   mTodoView =(LinearLayout)vi.inflate(R.layout.listlayout_child, parent, false);  
               }
               else{
                   mTodoView = (LinearLayout)convertView;
               }        
        
        progress = (ProgressBar)mTodoView.findViewById(R.id.child_progressbar);
        mailSender = (TextView)mTodoView.findViewById(R.id.child_text1);
        content = (TextView)mTodoView.findViewById(R.id.child_text2);
        subDate = (TextView)mTodoView.findViewById(R.id.child_text3);            
       
 ChildItem childItem=childrenList.get(groupPosition).get(childPosition);
        if(childItem.getSender() == mLoading){
            progress.setVisibility(View.VISIBLE);
            progress.setIndeterminate(true);
            mailSender.setText(childItem.getSender());
        }else{
            progress.setVisibility(View.GONE);
            mailSender.setText(childItem.getSender());
        }
        
       
 if( childItem.getContent() == null ){
            content.setVisibility(View.GONE);
        }
else{
            content.setVisibility(View.VISIBLE);
            content.setText(childItem.getContent());
        }
        
        
if(childItem.getDate()== null){
            subDate.setVisibility(View.GONE);
        }
else{
            subDate.setVisibility(View.VISIBLE);
            subDate.setText(childItem.getDate());
        }            
        
        
return mTodoView;
    }
    /*
     * (non-Javadoc)
     * @see android.widget.ExpandableListAdapter#getGroup(int)
     */

    public Object getGroup(int groupPosition) {
        return null;
    }

    public int getGroupCount() {
        return groupList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    /**
     * Get Group view and set group data.
     * convertView is reused view
     */

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout mTodoView = null;
        
if(convertView==null){
            LayoutInflater vi = null;
                   vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   mTodoView =(LinearLayout)vi.inflate(R.layout.listlayout_group, parent, false);  
               }
               
else{
                   mTodoView = (LinearLayout)convertView;
               }
        mMailbox = (TextView)mTodoView.findViewById(R.id.text1);
       
 mMailbox.setText(groupList.get(groupPosition).getMailbox());
        
        return mTodoView;
    }

    // Indicates whether the child and group IDs are stable
    public boolean hasStableIds() {            
        return true;
    }
    
    // Whether the child at the specified position is selectable.
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    public long getCombinedChildId(long groupId, long childId)
    {
        long res=super.getCombinedChildId(groupId, childId);
        Log.i("hubin","cc:"+res);
        return res;
    }
    public long   getCombinedGroupId(long groupId)
    {
        long res=super.getCombinedGroupId(groupId);
        Log.i("hubin","cg:"+res);
        return res;
    }
    public void onGroupCollapsed(int groupPosition)
    {
        Log.i("hubin","Collapsed");
    }
    public void onGroupExpanded(int groupPosition)
    {
        Log.i("hubin","Expanded");
    }
 }

/**
 * Group Item class
 * init Group item data in listlayoutsample12sml_group.xml
 * Group item has own child item's reference
 */

class GroupItem {      
    String mMailbox;
    public GroupItem(String Mailbox){           
        mMailbox = Mailbox;
    }        

    public String getMailbox() { 
        return mMailbox;        
    } 
}

/**
 * Child Item class
 * init child item data in listlayoutsample12sml_child.xml
 */

 class ChildItem { 
    private String mSender; 
    private String mContext; 
    private String mDate;
    
    public ChildItem( String Sender, String Context, String Date){
        mSender = Sender;
        mContext = Context;           
        mDate = Date;                    
    }

    public String getSender() { 
        return mSender;        
    } 
    
    public String getContent() { 
        return mContext;        
    } 

    public String getDate() { 
        return mDate; 
    }  
}
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ExpandableListView android:id="@+id/ExpandableListView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
listlayout_group.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
      android:paddingBottom="8dip" 
      android:orientation="horizontal"
      android:paddingTop="5dip" 
      android:layout_height="65.33dp" 
      android:paddingLeft="4.66dp" 
      android:paddingRight="13.33dp">    
     <TextView 
         android:id="@+id/text1"
         android:layout_marginLeft="6dip"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
        android:singleLine="true"        
        android:gravity="center_vertical"
          android:layout_gravity="center_vertical" 
          />  
</LinearLayout>
listlayout_child.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
      android:orientation="vertical"
      android:paddingRight="13.33dp" 
      android:layout_height="65.33dp" 
      android:paddingLeft="31.9dp" 
      android:gravity="center_vertical">
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_height="wrap_content" 
          android:layout_width="fill_parent" 
          android:orientation="horizontal">
          <ProgressBar 
              android:id="@+id/child_progressbar" 
            android:layout_width="32dp" 
            android:layout_height="32dp"            
            android:layout_marginRight="5dp"                
            android:minHeight="32dp"
            android:maxHeight="32dp"
            android:minWidth="32dp"
            android:maxWidth="32dp"                        
            android:indeterminateOnly="false">
        </ProgressBar>          
          <TextView 
              android:id="@+id/child_text1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:singleLine="true"/>
      </LinearLayout>
      
      <LinearLayout 
          android:orientation="horizontal" 
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:layout_marginTop="6dip">
          <TextView 
              android:id="@+id/child_text2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"              
              android:singleLine="true"              
              android:layout_weight="1"/>
          <TextView 
              android:id="@+id/child_text3"
              android:layout_height="wrap_content"              
              android:layout_width="wrap_content"/>              
      </LinearLayout>
      
</LinearLayout>


  评论这张
 
阅读(3322)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017