Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animationDuration="1500"
android:spacing="5px" />
</RelativeLayout>
ImageAdapter.java
package com.example.gridviewex02;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int[] mImageIds = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5,
R.drawable.image6
};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mImageIds[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
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
return imageView;
}
}
MainActivity.java
package com.example.gridviewex02;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gal = (Gallery) findViewById(R.id.gallery);
gal.setAdapter(new ImageAdapter(this));
gal.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position + "번째그림선택",
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@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) 실습2 (0) | 2013.04.02 |
---|---|
프로그레스 바 (ProgressBar) 실습 (0) | 2013.04.02 |
어댑터뷰 - 그리드 (0) | 2013.04.01 |
어댑터 뷰 - 스피너 (0) | 2013.04.01 |
항목 뷰 - 오버스크롤(OverScroll) (0) | 2013.04.01 |