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

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

BaseExpandableListAdapter  

2010-05-18 20:10:31|  分类: 数据适配器 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

BaseExpandableListAdapter
extends Object
implements ExpandableListAdapter

Known Direct Subclasses
CursorTreeAdapter, SimpleExpandableListAdapter 

Known Indirect Subclasses
ResourceCursorTreeAdapter, SimpleCursorTreeAdapter 
实例1:
import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
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 com.sec.android.touchwiz.samples.R;


public class ListLayoutSample12Xml extends ExpandableListActivity {

    // Sample data set.    
    private String mMailboxList[]={ 
            "Inbox", "Sentbox", "Draft", 
            "Outbox", "Email inbox", "Conversation",
            "Trash", "My folders"
            };
    
    private 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..", "?????",  "Call me plz.." }, 
            {"?????" , "Call me plz.." , "?????", "I'm going to go to watch movie!!!!!!!!!!!!!!!!!!!!!!!!!!", "Hello....", "Nice to meet you", "Call me plz.."},
            {"?????",  "Call me plz.." , "?????" , "Call me plz.." , "?????"},
            {"I'm going to go to watch movie!!!!!!!!!!!!!!!!!!!!!!!!!!", "Hello....", "Nice to meet you", "Call me plz.."}, 
            {"?????",  "Call me plz.." , "?????" , "Call me plz.." , "?????"},
            {"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"}};
    
    // child count ??? loading...? ?? ? group? child 1?? ????? ???
    // init value of childdata count, group data have "Loading messag..." child data, so the init value is 1 
    private int mChildCount[] = {1,1,1,1,1,1,1,1};

    ExpandableListView mExpandableList;
    ExpandableListAdapter mAdapter;
    
    protected ArrayList<GroupItem> mGroupdata = new ArrayList<GroupItem>();    


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mExpandableList = getExpandableListView();   
        
        // initializing Expanded mailbox list data
        for(int i = 0; i < mMailboxList.length; i++) {
            GroupItem item = new GroupItem(i, mMailboxList);
            
            mGroupdata.add(item);            
   
            ChildItem items = new ChildItem( mLoading, null, null);
            item.mChilddata.add(items);           
        }
        
        // Set up our adapter
        mAdapter = new ExpandedMailboxListAdapter();
        setListAdapter(mAdapter);
        mExpandableList.setGroupIndicator(null); // Group indicator disable
        
        mExpandableList.setOnGroupClickListener(new OnGroupClickListener(){
               public boolean onGroupClick(ExpandableListView mExpandableList, View arg1, 
                    final int groupPosition, long id) {                   
                 
                   new CountDownTimer(3000, 50) {
                        public void onTick(long millisUntilFinished) {
                        
                        }
                        public void onFinish() {
                            expandableListChange( groupPosition );                            
                        }
                    }.start();                               
                return false;
            }
          
        });
    }
 
    /**
     * Expandable list? child data? set?? child data? set ??? method
     * Set Child list data of Expandable list
     * Remove first list item and readd item
     * @param groupPosition
     */
    private void expandableListChange(int groupPosition){
        
        mGroupdata.get(groupPosition).mChilddata.remove(0);
        for (int i = 0; i < mSender[groupPosition].length; i++) {
            ChildItem items = new ChildItem( mSender[groupPosition], mMailContext[groupPosition], mDate[groupPosition]);            
            mGroupdata.get(groupPosition).mChilddata.add(i,items);
            mChildCount[groupPosition]= mSender[groupPosition].length;
        }
        
        ((BaseExpandableListAdapter)mAdapter).notifyDataSetChanged();
    }

    /**
     *    BaseExpandableList Adpater ?? ?? expandable list adpater ?? 
     *    Define ExpandableMailboxListAdpater by inherit BaseExpandableListAdpater
     */
    public class ExpandedMailboxListAdapter extends BaseExpandableListAdapter {
        
        private TextView mMailbox;
        private ImageView mMailboxIcon;
        private TextView mMailSender;
        private TextView mContext;
        private TextView mSubDate;
        private ProgressBar mProgress;
        
        LinearLayout mTodoView = null;        

        /* 
         * (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 mChildCount[groupPosition];
        }
 
        /**
         * Get child view and set child data.
         * convertView is reused view
         */
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            //int i=0;
            if(convertView==null){
                LayoutInflater vi = null;
                     vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     mTodoView =(LinearLayout)vi.inflate(R.layout.listlayoutsample12xml_child, parent, false);  
                 }
                 else{
                     mTodoView = (LinearLayout)convertView;
                 }        
            
            mProgress = (ProgressBar)mTodoView.findViewById(R.id.child_progressbar);
            mMailSender = (TextView)mTodoView.findViewById(R.id.child_text1);
            mContext = (TextView)mTodoView.findViewById(R.id.child_text2);
            mSubDate = (TextView)mTodoView.findViewById(R.id.child_text3);            
            
            
            /**
             * Visibility ??? Gone?? set? ?? ??? Visible? set ??? ??? Convetview? ??????
             * Gone ??? ?? ?? ????? view ???? ??. ??? GONE??? ???? Visible ?? ?????
             * 
             * If user set Visibility to Gone in specific case, user have to set Visible in other case.
             * Because Convertview is reused with prefer visibility attribute.
             */
            if(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getSender() == mLoading){
                mProgress.setVisibility(View.VISIBLE);
                mProgress.setIndeterminate(true);
                mMailSender.setText(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getSender());
            }else{
                mProgress.setVisibility(View.GONE);
                mMailSender.setText(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getSender());
            }
            
            if( mGroupdata.get(groupPosition).mChilddata.get(childPosition).getContext() == null ){
                mContext.setVisibility(View.GONE);
            }else{
                mContext.setVisibility(View.VISIBLE);
                mContext.setText(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getContext());
            }
            
            if(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getDate()== null){
                mSubDate.setVisibility(View.GONE);
            }else{
                mSubDate.setVisibility(View.VISIBLE);
                mSubDate.setText(mGroupdata.get(groupPosition).mChilddata.get(childPosition).getDate());
            }            
            
            return mTodoView;
        }
        /*
         * (non-Javadoc)
         * @see android.widget.ExpandableListAdapter#getGroup(int)
         */
        public Object getGroup(int groupPosition) {
            return null;
        }

        public int getGroupCount() {
            return mMailboxList.length;
        }

        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) {
            
            if(convertView==null){
                LayoutInflater vi = null;
                     vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     mTodoView =(LinearLayout)vi.inflate(R.layout.listlayoutsample12xml_group, parent, false);  
                 }
                 else{
                     mTodoView = (LinearLayout)convertView;
                 }
            mMailboxIcon = (ImageView)mTodoView.findViewById(R.id.image1);
            mMailbox = (TextView)mTodoView.findViewById(R.id.text1);
            
            mMailboxIcon.setImageDrawable(mGroupdata.get(groupPosition).getImage());
            mMailbox.setText(mGroupdata.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;
        }    
     }
    
    /**
     * Group Item class
     * init Group item data in listlayoutsample12sml_group.xml
     * Group item has own child item's reference
     */
    private class GroupItem {      
        private String mMailbox; 
        private Drawable mMailboxIcon;      
        private ArrayList<ChildItem> mChilddata = new ArrayList<ChildItem>();
        
        public GroupItem(int i, String Mailbox){           
            mMailbox = Mailbox;
            
            switch(i%4) {
            case 0 :                 
                mMailboxIcon = getResources().getDrawable(R.drawable.listlayoutsample12xml_image1);
                break;
            case 1:                 
                mMailboxIcon = getResources().getDrawable(R.drawable.listlayoutsample12xml_image2);
                break;
            case 2:
                mMailboxIcon = getResources().getDrawable(R.drawable.listlayoutsample12xml_image3);
                break;
            case 3:
                mMailboxIcon = getResources().getDrawable(R.drawable.listlayoutsample12xml_image4);
                break;
            }
        }        

        public String getMailbox() { 
            return mMailbox;        
        } 
  
        public Drawable getImage() { 
            return mMailboxIcon; 
        }  
    }
  
    /**
     * Child Item class
     * init child item data in listlayoutsample12sml_child.xml
     */
    private 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 getContext() { 
            return mContext;        
        } 
  
        public String getDate() { 
            return mDate; 
        }  
    }     
}
  评论这张
 
阅读(807)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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