Home | History | Annotate | Download | only in expandingcells
      1 /*
      2  * Copyright (C) 2013 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.expandingcells;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 
     22 import java.util.ArrayList;
     23 import java.util.List;
     24 
     25 /**
     26  * This activity creates a listview whose items can be clicked to expand and show
     27  * additional content.
     28  *
     29  * In this specific demo, each item in a listview displays an image and a corresponding
     30  * title. These two items are centered in the default (collapsed) state of the listview's
     31  * item. When the item is clicked, it expands to display text of some varying length.
     32  * The item persists in this expanded state (even if the user scrolls away and then scrolls
     33  * back to the same location) until it is clicked again, at which point the cell collapses
     34  * back to its default state.
     35  */
     36 public class ExpandingCells extends Activity {
     37 
     38     private final int CELL_DEFAULT_HEIGHT = 200;
     39     private final int NUM_OF_CELLS = 30;
     40 
     41     private ExpandingListView mListView;
     42 
     43     @Override
     44     protected void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.activity_main);
     47 
     48         ExpandableListItem[] values = new ExpandableListItem[] {
     49                 new ExpandableListItem("Chameleon", R.drawable.chameleon, CELL_DEFAULT_HEIGHT,
     50                         getResources().getString(R.string.short_lorem_ipsum)),
     51                 new ExpandableListItem("Rock", R.drawable.rock, CELL_DEFAULT_HEIGHT,
     52                         getResources().getString(R.string.medium_lorem_ipsum)),
     53                 new ExpandableListItem("Flower", R.drawable.flower, CELL_DEFAULT_HEIGHT,
     54                         getResources().getString(R.string.long_lorem_ipsum)),
     55         };
     56 
     57         List<ExpandableListItem> mData = new ArrayList<ExpandableListItem>();
     58 
     59         for (int i = 0; i < NUM_OF_CELLS; i++) {
     60             ExpandableListItem obj = values[i % values.length];
     61             mData.add(new ExpandableListItem(obj.getTitle(), obj.getImgResource(),
     62                     obj.getCollapsedHeight(), obj.getText()));
     63         }
     64 
     65         CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.list_view_item, mData);
     66 
     67         mListView = (ExpandingListView)findViewById(R.id.main_list_view);
     68         mListView.setAdapter(adapter);
     69         mListView.setDivider(null);
     70     }
     71 }