• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.android.contacts.widget;
18 
19 import com.android.contacts.R;
20 
21 import android.app.ListActivity;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.TextView;
31 
32 /**
33  * An activity that demonstrates various use cases for the {@link PinnedHeaderListView}.
34  * If we decide to move PinnedHeaderListView to the framework, this class could go
35  * to API demos.
36  */
37 public class PinnedHeaderListDemoActivity extends ListActivity {
38 
39     public final static class TestPinnedHeaderListAdapter extends PinnedHeaderListAdapter {
40 
TestPinnedHeaderListAdapter(Context context)41         public TestPinnedHeaderListAdapter(Context context) {
42             super(context);
43             setPinnedPartitionHeadersEnabled(true);
44         }
45 
46         private String[] mHeaders;
47         private int mPinnedHeaderCount;
48 
setHeaders(String[] headers)49         public void setHeaders(String[] headers) {
50             this.mHeaders = headers;
51         }
52 
53         @Override
newHeaderView(Context context, int partition, Cursor cursor, ViewGroup parent)54         protected View newHeaderView(Context context, int partition, Cursor cursor,
55                 ViewGroup parent) {
56             LayoutInflater inflater = LayoutInflater.from(context);
57             return inflater.inflate(R.layout.list_section, null);
58         }
59 
60         @Override
bindHeaderView(View view, int parition, Cursor cursor)61         protected void bindHeaderView(View view, int parition, Cursor cursor) {
62             TextView headerText = (TextView)view.findViewById(R.id.header_text);
63             headerText.setText(mHeaders[parition]);
64         }
65 
66         @Override
newView(Context context, int partition, Cursor cursor, int position, ViewGroup parent)67         protected View newView(Context context, int partition, Cursor cursor, int position,
68                 ViewGroup parent) {
69             LayoutInflater inflater = LayoutInflater.from(context);
70             return inflater.inflate(android.R.layout.simple_list_item_1, null);
71         }
72 
73         @Override
bindView(View v, int partition, Cursor cursor, int position)74         protected void bindView(View v, int partition, Cursor cursor, int position) {
75             TextView text = (TextView)v.findViewById(android.R.id.text1);
76             text.setText(cursor.getString(1));
77         }
78 
79         @Override
getPinnedHeaderView(int viewIndex, View convertView, ViewGroup parent)80         public View getPinnedHeaderView(int viewIndex, View convertView, ViewGroup parent) {
81             LayoutInflater inflater = LayoutInflater.from(getContext());
82             View view = inflater.inflate(R.layout.list_section, parent, false);
83             view.setFocusable(false);
84             view.setEnabled(false);
85             bindHeaderView(view, viewIndex, null);
86             return view;
87         }
88 
89         @Override
getPinnedHeaderCount()90         public int getPinnedHeaderCount() {
91             return mPinnedHeaderCount;
92         }
93     }
94 
95     private Handler mHandler = new Handler();
96 
97     @Override
onCreate(Bundle bundle)98     protected void onCreate(Bundle bundle) {
99         super.onCreate(bundle);
100 
101         setContentView(R.layout.pinned_header_list_demo);
102 
103         final TestPinnedHeaderListAdapter adapter = new TestPinnedHeaderListAdapter(this);
104 
105         Bundle extras = getIntent().getExtras();
106         int[] counts = extras.getIntArray("counts");
107         String[] names = extras.getStringArray("names");
108         boolean[] showIfEmpty = extras.getBooleanArray("showIfEmpty");
109         boolean[] hasHeader = extras.getBooleanArray("headers");
110         int[] delays = extras.getIntArray("delays");
111 
112         if (counts == null || names == null || showIfEmpty == null || delays == null) {
113             throw new IllegalArgumentException("Missing required extras");
114         }
115 
116         adapter.setHeaders(names);
117         for (int i = 0; i < counts.length; i++) {
118             adapter.addPartition(showIfEmpty[i], names[i] != null);
119             adapter.mPinnedHeaderCount = names.length;
120         }
121         setListAdapter(adapter);
122         for (int i = 0; i < counts.length; i++) {
123             final int sectionId = i;
124             final Cursor cursor = makeCursor(names[i], counts[i]);
125             mHandler.postDelayed(new Runnable() {
126 
127                 public void run() {
128                     adapter.changeCursor(sectionId, cursor);
129 
130                 }
131             }, delays[i]);
132         }
133     }
134 
makeCursor(String name, int count)135     private Cursor makeCursor(String name, int count) {
136         MatrixCursor cursor = new MatrixCursor(new String[]{"_id", name});
137         for (int i = 0; i < count; i++) {
138             cursor.addRow(new Object[]{i, name + "[" + i + "]"});
139         }
140         return cursor;
141     }
142 }
143