BaseExpandableListAdapter

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; 
        }  
    }     
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值