Home | History | Annotate | Download | only in view
      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 package com.example.android.apis.view;
     17 
     18 import com.example.android.apis.R;
     19 
     20 import android.app.Activity;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.ResolveInfo;
     24 import android.os.Bundle;
     25 import android.view.ActionMode;
     26 import android.view.Menu;
     27 import android.view.MenuItem;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.BaseAdapter;
     31 import android.widget.Checkable;
     32 import android.widget.FrameLayout;
     33 import android.widget.GridView;
     34 import android.widget.ImageView;
     35 
     36 import java.util.List;
     37 
     38 /**
     39  * This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on GridView.
     40  */
     41 public class Grid3 extends Activity {
     42 
     43     GridView mGrid;
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48 
     49         loadApps();
     50 
     51         setContentView(R.layout.grid_1);
     52         mGrid = (GridView) findViewById(R.id.myGrid);
     53         mGrid.setAdapter(new AppsAdapter());
     54         mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
     55         mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener());
     56     }
     57 
     58     private List<ResolveInfo> mApps;
     59 
     60     private void loadApps() {
     61         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
     62         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
     63 
     64         mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
     65     }
     66 
     67     public class AppsAdapter extends BaseAdapter {
     68         public AppsAdapter() {
     69         }
     70 
     71         public View getView(int position, View convertView, ViewGroup parent) {
     72             CheckableLayout l;
     73             ImageView i;
     74 
     75             if (convertView == null) {
     76                 i = new ImageView(Grid3.this);
     77                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);
     78                 i.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
     79                 l = new CheckableLayout(Grid3.this);
     80                 l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,
     81                         GridView.LayoutParams.WRAP_CONTENT));
     82                 l.addView(i);
     83             } else {
     84                 l = (CheckableLayout) convertView;
     85                 i = (ImageView) l.getChildAt(0);
     86             }
     87 
     88             ResolveInfo info = mApps.get(position);
     89             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
     90 
     91             return l;
     92         }
     93 
     94 
     95         public final int getCount() {
     96             return mApps.size();
     97         }
     98 
     99         public final Object getItem(int position) {
    100             return mApps.get(position);
    101         }
    102 
    103         public final long getItemId(int position) {
    104             return position;
    105         }
    106     }
    107 
    108     public class CheckableLayout extends FrameLayout implements Checkable {
    109         private boolean mChecked;
    110 
    111         public CheckableLayout(Context context) {
    112             super(context);
    113         }
    114 
    115         public void setChecked(boolean checked) {
    116             mChecked = checked;
    117             setBackgroundDrawable(checked ?
    118                     getResources().getDrawable(R.drawable.blue)
    119                     : null);
    120         }
    121 
    122         public boolean isChecked() {
    123             return mChecked;
    124         }
    125 
    126         public void toggle() {
    127             setChecked(!mChecked);
    128         }
    129 
    130     }
    131 
    132     public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
    133         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    134             mode.setTitle("Select Items");
    135             mode.setSubtitle("One item selected");
    136             return true;
    137         }
    138 
    139         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    140             return true;
    141         }
    142 
    143         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    144             return true;
    145         }
    146 
    147         public void onDestroyActionMode(ActionMode mode) {
    148         }
    149 
    150         public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
    151                 boolean checked) {
    152             int selectCount = mGrid.getCheckedItemCount();
    153             switch (selectCount) {
    154             case 1:
    155                 mode.setSubtitle("One item selected");
    156                 break;
    157             default:
    158                 mode.setSubtitle("" + selectCount + " items selected");
    159                 break;
    160             }
    161         }
    162 
    163     }
    164 }
    165