Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.view;
     18 
     19 import android.app.ExpandableListActivity;
     20 import android.os.Bundle;
     21 import android.view.ContextMenu;
     22 import android.view.Gravity;
     23 import android.view.MenuItem;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.view.ContextMenu.ContextMenuInfo;
     27 import android.widget.AbsListView;
     28 import android.widget.BaseExpandableListAdapter;
     29 import android.widget.ExpandableListAdapter;
     30 import android.widget.ExpandableListView;
     31 import android.widget.TextView;
     32 import android.widget.Toast;
     33 import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
     34 
     35 import com.example.android.apis.R;
     36 
     37 /**
     38  * Demonstrates expandable lists using a custom {@link ExpandableListAdapter}
     39  * from {@link BaseExpandableListAdapter}.
     40  */
     41 public class ExpandableList1 extends ExpandableListActivity {
     42 
     43     ExpandableListAdapter mAdapter;
     44 
     45     @Override
     46     public void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48 
     49         // Set up our adapter
     50         mAdapter = new MyExpandableListAdapter();
     51         setListAdapter(mAdapter);
     52         registerForContextMenu(getExpandableListView());
     53     }
     54 
     55     @Override
     56     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
     57         menu.setHeaderTitle("Sample menu");
     58         menu.add(0, 0, 0, R.string.expandable_list_sample_action);
     59     }
     60 
     61     @Override
     62     public boolean onContextItemSelected(MenuItem item) {
     63         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
     64 
     65         String title = ((TextView) info.targetView).getText().toString();
     66 
     67         int type = ExpandableListView.getPackedPositionType(info.packedPosition);
     68         if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
     69             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
     70             int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
     71             Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
     72                     Toast.LENGTH_SHORT).show();
     73             return true;
     74         } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
     75             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
     76             Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
     77             return true;
     78         }
     79 
     80         return false;
     81     }
     82 
     83     /**
     84      * A simple adapter which maintains an ArrayList of photo resource Ids.
     85      * Each photo is displayed as an image. This adapter supports clearing the
     86      * list of photos and adding a new photo.
     87      *
     88      */
     89     public class MyExpandableListAdapter extends BaseExpandableListAdapter {
     90         // Sample data set.  children[i] contains the children (String[]) for groups[i].
     91         private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
     92         private String[][] children = {
     93                 { "Arnold", "Barry", "Chuck", "David" },
     94                 { "Ace", "Bandit", "Cha-Cha", "Deuce" },
     95                 { "Fluffy", "Snuggles" },
     96                 { "Goldy", "Bubbles" }
     97         };
     98 
     99         public Object getChild(int groupPosition, int childPosition) {
    100             return children[groupPosition][childPosition];
    101         }
    102 
    103         public long getChildId(int groupPosition, int childPosition) {
    104             return childPosition;
    105         }
    106 
    107         public int getChildrenCount(int groupPosition) {
    108             return children[groupPosition].length;
    109         }
    110 
    111         public TextView getGenericView() {
    112             // Layout parameters for the ExpandableListView
    113             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    114                     ViewGroup.LayoutParams.MATCH_PARENT, 64);
    115 
    116             TextView textView = new TextView(ExpandableList1.this);
    117             textView.setLayoutParams(lp);
    118             // Center the text vertically
    119             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    120             // Set the text starting position
    121             textView.setPadding(36, 0, 0, 0);
    122             return textView;
    123         }
    124 
    125         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
    126                 View convertView, ViewGroup parent) {
    127             TextView textView = getGenericView();
    128             textView.setText(getChild(groupPosition, childPosition).toString());
    129             return textView;
    130         }
    131 
    132         public Object getGroup(int groupPosition) {
    133             return groups[groupPosition];
    134         }
    135 
    136         public int getGroupCount() {
    137             return groups.length;
    138         }
    139 
    140         public long getGroupId(int groupPosition) {
    141             return groupPosition;
    142         }
    143 
    144         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    145                 ViewGroup parent) {
    146             TextView textView = getGenericView();
    147             textView.setText(getGroup(groupPosition).toString());
    148             return textView;
    149         }
    150 
    151         public boolean isChildSelectable(int groupPosition, int childPosition) {
    152             return true;
    153         }
    154 
    155         public boolean hasStableIds() {
    156             return true;
    157         }
    158 
    159     }
    160 }
    161