도래울

안드로이드 GridView 예제 본문

개발/Android

안드로이드 GridView 예제

도래울 2016. 2. 5. 11:43

 

xml파일

<?xml version="1.0" encoding="utf-8"?>


<GridView android:id="@+id/gridview" android:layout_width="fill_parent"
 android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
 android:numColumns="auto_fit" android:verticalSpacing="10dp"
 android:columnWidth="90dp" android:stretchMode="columnWidth"  android:gravity="center"/>


Java 파일


package my.andr;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class UIEvent extends Activity{
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  GridView gridView = (GridView) findViewById(R.id.gridview);
  gridView.setAdapter((ListAdapter) new ImageAdapter(this));
 }
 public class ImageAdapter extends BaseAdapter {
  private Context mContext;
  private Integer[] mThumblds= {
    R.drawable.androidmarker,R.drawable.b,R.drawable.black,R.drawable.black1,R.drawable.icon,R.drawable.j,R.drawable.j,R.drawable.m,R.drawable.suni};
    public ImageAdapter(Context c) {
     mContext = c;
    }
    public int getCount() {
     return mThumblds.length;
    }
    public Object getItem(int position) {
     return null;
    }
    public long getItemId(int position) {
     return 0;
    }
    public View getView(int position, View convertView,
      ViewGroup parent) {
     ImageView imageView;
     if (convertView == null) {
      imageView = new ImageView(mContext);
      imageView.setLayoutParams(new GridView.LayoutParams(85,85));
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageView.setPadding(8, 8, 8, 8);
     }else {
      imageView = (ImageView) convertView;
     }
     imageView.setImageResource(mThumblds[position]);
     return imageView;
    }
  }
 }

 

 

'개발 > Android' 카테고리의 다른 글

안드로이드 Resources에서 Color Value 사용하기  (0) 2016.02.05
안드로이드 커스텀뷰 예제  (0) 2016.02.05
안드로이드 탭 예제  (0) 2016.02.05
Android DatePicker  (0) 2016.02.05
[Android] Time Picker  (0) 2016.02.05
Comments