본문 바로가기

Android

어댑터뷰 - 그리드

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

   <GridView

             android:id="@+id/grid"            

             android:layout_width="fill_parent"

             android:layout_height="fill_parent"

             android:padding="10dp"

             android:verticalSpacing="10dp"

             android:horizontalSpacing="10dp"

             android:numColumns="auto_fit"

             android:columnWidth="60dp"

             android:stretchMode="columnWidth"

             android:gravity="center"

/>

 

 

</LinearLayout>

 

 

ImageAdapter.java

 

package com.example.gridviewex01;

 

import android.content.Context;

 

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.ImageView;

 

public class ImageAdapter extends BaseAdapter {

    

     private Context mContext;

     int[] picture = {R.drawable.pride,R.drawable.dog,R.drawable.cloud};

    

     public ImageAdapter(Context c){

           mContext = c;

     }

     @Override 

     public int getCount() {

           // TODO Auto-generated method stub

           return 100;

     }

 

     @Override

     public Object getItem(int position) {

           // TODO Auto-generated method stub

           return picture[position%3];

     }

 

     @Override

     public long getItemId(int position) {

           // TODO Auto-generated method stub

           return position;

     }

 

     @Override

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

           // TODO Auto-generated method stub

           ImageView imageView;

           if(convertView==null){

                imageView=new ImageView(mContext);

                imageView.setLayoutParams(new GridView.LayoutParams(100,100));

                imageView.setAdjustViewBounds(false);

                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

                imageView.setPadding(8, 8, 8, 8);

           } else {

                imageView=(ImageView)convertView;

           }

           imageView.setImageResource(picture[position%3]);

          

           return imageView;

     }

 

}

 

 

 

MainActivity.java

 

package com.example.gridviewex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           GridView grid = (GridView)findViewById(R.id.grid);

           ImageAdapter adapter = new ImageAdapter(this);

           grid.setAdapter(adapter);

          

           grid.setOnItemClickListener(new OnItemClickListener() {

               

 

                @Override

            public void onItemClick(AdapterView<?> parent, View view, int position,

                           long id) {

                     // TODO Auto-generated method stub

            Toast.makeText(MainActivity.this, position+ 

"번째그림선택", Toast.LENGTH_SHORT).show();

                }

             });

           }

               

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}




 

 

'Android' 카테고리의 다른 글

프로그레스 바 (ProgressBar) 실습  (0) 2013.04.02
어댑터뷰 - 갤러리  (0) 2013.04.01
어댑터 뷰 - 스피너  (0) 2013.04.01
항목 뷰 - 오버스크롤(OverScroll)  (0) 2013.04.01
항목 뷰 - ListActivity  (0) 2013.04.01