project name : imageGridView
target : 2.1
package com.myandroid.gridView;
activity Name : ImageGridViewActivity
put image in drawable name like - sample_i , sample_ii, sample_iii , sample_iv,
ImageGridViewActivity.java file are
public class ImageGridViewActivity extends Activity {
public static int pos,len;
ImageAdapter ia;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ia=new ImageAdapter(ImageGridViewActivity.this);
len=ia.getCount();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(ImageGridViewActivity.this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent intNext=new Intent(ImageGridViewActivity.this,imageShow.class);
startActivity(intNext);
pos=position;
Toast.makeText(ImageGridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
create anather class ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_i,
R.drawable.sample_ii,
R.drawable.sample_iii,
R.drawable.sample_iv,
};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
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(mThumbIds[position]);
return imageView;
}
}
target : 2.1
package com.myandroid.gridView;
activity Name : ImageGridViewActivity
put image in drawable name like - sample_i , sample_ii, sample_iii , sample_iv,
ImageGridViewActivity.java file are
public class ImageGridViewActivity extends Activity {
public static int pos,len;
ImageAdapter ia;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ia=new ImageAdapter(ImageGridViewActivity.this);
len=ia.getCount();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(ImageGridViewActivity.this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent intNext=new Intent(ImageGridViewActivity.this,imageShow.class);
startActivity(intNext);
pos=position;
Toast.makeText(ImageGridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
create anather class ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_i,
R.drawable.sample_ii,
R.drawable.sample_iii,
R.drawable.sample_iv,
};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
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(mThumbIds[position]);
return imageView;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////// main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="30dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
/////////////////////////////////////////////////////////////////////////////////////
your application is redy ...........
No comments:
Post a Comment