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.setPaddingRelative(36, 0, 0, 0);
    122             // Set the text alignment
    123             textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
    124             return textView;
    125         }
    126 
    127         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
    128                 View convertView, ViewGroup parent) {
    129             TextView textView = getGenericView();
    130             textView.setText(getChild(groupPosition, childPosition).toString());
    131             return textView;
    132         }
    133 
    134         public Object getGroup(int groupPosition) {
    135             return groups[groupPosition];
    136         }
    137 
    138         public int getGroupCount() {
    139             return groups.length;
    140         }
    141 
    142         public long getGroupId(int groupPosition) {
    143             return groupPosition;
    144         }
    145 
    146         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    147                 ViewGroup parent) {
    148             TextView textView = getGenericView();
    149             textView.setText(getGroup(groupPosition).toString());
    150             return textView;
    151         }
    152 
    153         public boolean isChildSelectable(int groupPosition, int childPosition) {
    154             return true;
    155         }
    156 
    157         public boolean hasStableIds() {
    158             return true;
    159         }
    160 
    161     }
    162 }
    163