Android Workspace Widget

 

I was looking for an android widget similar to the launcher workspace widget ( the one used in almost all launchers ), but a bit more customizable and with the possibility to use an Adapter as content provider. So at the end I mixed portion of code from the android launcher Workspace widget, the Gallery widget and the AbsListView widget.

Nothing particular tricky, just a combination of the 3 widgets in order to have the same sliding effect of the workspace ( using custom number of columns and rows ), but at the same time with an ArrayAdapter for creating views, and a little glow effect at start at the end of scrolling for creating the gingerbread style overscroll effect.

This is the source code: workspace

It’s just a first version, so it’s not really optimized…

The adapter used for creating this example looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class WorkspaceAdapter extends ArrayAdapter<applicationinfo> {

   int screenId;
   PackageManager pm;
   private LayoutInflater mInflater;
   private int nCellsPerScreen = 4;

   public WorkspaceAdapter( Context context, int textViewResourceId, List</applicationinfo><applicationinfo> objects ) {
      super( context, textViewResourceId, objects );
      screenId = textViewResourceId;
      pm = context.getPackageManager();
      nCellsPerScreen = context.getResources().getInteger( R.integer.config_portraitCells ) * context.getResources().getInteger( R.integer.config_portraitRows );
      mInflater = (LayoutInflater) context.getSystemService( LAYOUT_INFLATER_SERVICE );
   }

   @Override
   public int getCount() {
      return (int) Math.ceil( (double) super.getCount() / nCellsPerScreen );
   }

   public int getRealCount() {
      return super.getCount();
   }

   @Override
   public View getView( int position, View convertView, ViewGroup parent ) {

      if ( convertView == null ) {
         convertView = mInflater.inflate( screenId, mWorkspace, false );
         ((CellLayout)convertView).setNumCols( nCellsPerScreen );
      }

      CellLayout cell = (CellLayout) convertView;

      int index = position * nCellsPerScreen;
      int realCount = getRealCount();

      for ( int i = 0; i < nCellsPerScreen; i++ ) {
         CellInfo cellInfo = cell.findVacantCell( 1, 1 );
         TextView text;

         if ( cellInfo == null ) {
            text = (TextView) cell.getChildAt( i );
         } else {
            text = (TextView) mInflater.inflate( R.layout.application_boxed, cell, false );
            CellLayout.LayoutParams lp = new CellLayout.LayoutParams( cellInfo.cellX, cellInfo.cellY, cellInfo.spanH,
                  cellInfo.spanV );
            cell.addView( text, i, lp );
         }

         if ( index + i < realCount ) {
            ApplicationInfo appInfo = getItem( index + i );
            CharSequence label = appInfo.loadLabel( pm );
            Drawable bm = appInfo.loadIcon( pm );

            text.setCompoundDrawablesWithIntrinsicBounds( null, bm, null, null ); // new
            text.setText( label );
            text.setClickable( true );
            text.setFocusable( true );
            text.setVisibility( View.VISIBLE );
         } else {
            text.setVisibility( View.INVISIBLE );
         }
      }
      return convertView;
   }
}

 

  • Fan

    This is really awesome, please keep providing details!