public class DataBWT4 extends Activity {
    private GridView gv;
    static final int IMGS[] = {R.drawable.a01,
        R.drawable.a02,
        R.drawable.a03,
        R.drawable.a04,
        R.drawable.a05,
        R.drawable.a06,
        R.drawable.a07,
        R.drawable.a08,
        R.drawable.a09,
        R.drawable.a10,
        R.drawable.a11,
        R.drawable.a12
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gv = (GridView)findViewById(R.id.GridView01);
        gv.setAdapter(new UserAdapter(this));
        
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Toast.makeText(DataBWT4.this, "선택 인덱스 : " + position, 0).show();
}
       
});
        
    }
}

class UserAdapter extends BaseAdapter{ // Controller
private Context c;
private ImageView iv;
UserAdapter(){
}
UserAdapter(DataBWT4 c){
this.c = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return DataBWT4.IMGS.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataBWT4.IMGS[position];
}

@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
if(convertView == null){
iv = new ImageView(c);
iv.setLayoutParams(new GridView.LayoutParams(120,150));
//iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setScaleType(ImageView.ScaleType.FIT_XY); //크기 고정 옵션
iv.setPadding(5, 5, 15, 5);
}else{
iv = (ImageView)convertView;
}
iv.setImageResource(DataBWT4.IMGS[position]);
return iv;
}
}

'스마트폰 > 안드로이드' 카테고리의 다른 글

이미지뷰(ImageView) 공부.  (0) 2011.05.13
웹 소스 찍기  (0) 2011.05.13
데이터 바인딩3 공부  (0) 2011.05.13
데이터 바인딩2 공부  (0) 2011.05.13
데이터 바인딩1 공부  (0) 2011.05.13

+ Recent posts