결과화면

 

 

//GridViewDemo.java

 

package com.example.containerdemo;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDemo extends Activity {
 GridView mGrid;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  loadApps();
  setContentView(R.layout.gridviewdemo);
  mGrid = (GridView)findViewById(R.id.myGrid);
  mGrid.setAdapter(new AppsAdapter());  
 }
 
 private List<ResolveInfo> mApps;
 
 private void loadApps(){
  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  
  mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
 }
 
 public class AppsAdapter extends BaseAdapter{
  public AppsAdapter(){
   
  }
  
  public View getView(int position, View convertView, ViewGroup parent){
   ImageView i;
   
   if(convertView == null){
    i = new ImageView(GridViewDemo.this);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new GridView.LayoutParams(50,50));
   } else {
    i = (ImageView)convertView;
   }
   
   ResolveInfo info = mApps.get(position);
   i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
   return i;
  }
  
  public final int getCount(){
   return mApps.size();
  }
  
  public final Object getItem(int position){
   return mApps.get(position);   
  }
  
  public final long getItemId(int position){
   return position;   
  }
 }

}

 

 

//gridviewdemo.xml

 

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:id="@+id/myGrid"/>

 

'2020년도 이전 > [WebSig] Android' 카테고리의 다른 글

Sliding Demo - 슬라이딩드로어  (0) 2013.08.16
Flipper Demo - 뷰플리퍼  (0) 2013.08.16
ExpandableList ViewDemo  (0) 2013.08.14
ListView Demo5  (0) 2013.08.14
ListView Demo3 - 리스트 추가, 삭제  (0) 2013.08.14

+ Recent posts