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 android.widget.gridview; 18 19 import android.view.KeyEvent; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.widget.BaseAdapter; 23 import android.widget.GridView; 24 import android.widget.ListAdapter; 25 26 import android.util.GridScenario; 27 28 import java.util.ArrayList; 29 30 /** 31 * A grid with vertical spacing between rows 32 */ 33 public class GridDelete extends GridScenario { 34 @Override init(Params params)35 protected void init(Params params) { 36 params.setStartingSelectionPosition(-1) 37 .setMustFillScreen(false) 38 .setNumItems(1001) 39 .setNumColumns(4) 40 .setItemScreenSizeFactor(0.20) 41 .setVerticalSpacing(20); 42 } 43 44 45 46 @Override createAdapter()47 protected ListAdapter createAdapter() { 48 return new DeleteAdapter(getInitialNumItems()); 49 } 50 51 52 53 54 @Override onKeyDown(int keyCode, KeyEvent event)55 public boolean onKeyDown(int keyCode, KeyEvent event) { 56 if (keyCode == KeyEvent.KEYCODE_DEL) { 57 GridView g = getGridView(); 58 ((DeleteAdapter)g.getAdapter()).deletePosition(g.getSelectedItemPosition()); 59 return true; 60 } else { 61 return super.onKeyDown(keyCode, event); 62 } 63 } 64 65 66 67 68 private class DeleteAdapter extends BaseAdapter { 69 70 private ArrayList<Integer> mData; 71 DeleteAdapter(int initialNumItems)72 public DeleteAdapter(int initialNumItems) { 73 super(); 74 mData = new ArrayList<Integer>(initialNumItems); 75 76 int i; 77 for (i=0; i<initialNumItems; ++i) { 78 mData.add(new Integer(10000 + i)); 79 } 80 81 } 82 deletePosition(int selectedItemPosition)83 public void deletePosition(int selectedItemPosition) { 84 if (selectedItemPosition >=0 && selectedItemPosition < mData.size()) { 85 mData.remove(selectedItemPosition); 86 notifyDataSetChanged(); 87 } 88 89 } 90 getCount()91 public int getCount() { 92 return mData.size(); 93 } 94 getItem(int position)95 public Object getItem(int position) { 96 return mData.get(position); 97 } 98 getItemId(int position)99 public long getItemId(int position) { 100 return mData.get(position); 101 } 102 getView(int position, View convertView, ViewGroup parent)103 public View getView(int position, View convertView, ViewGroup parent) { 104 int desiredHeight = getDesiredItemHeight(); 105 return createView(mData.get(position), parent, desiredHeight); 106 } 107 } 108 } 109