第一篇GridViewコントロール
3045 ワード
くだらないことは言わないで、テーマに直行します~!
図のように構成されています.
mainコード:
Control_GridViewActivity.javaコード:
図のように構成されています.
mainコード:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gv1" android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
></GridView>
</LinearLayout>
Control_GridViewActivity.javaコード:
public class Control_GridViewActivity extends Activity {
GridView gv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView) this.findViewById(R.id.gv1);
gv.setAdapter(new myadpter(Control_GridViewActivity.this));
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ImageView iv = (ImageView) arg1;
String name = iv.getResources().getResourceName(mygroup[arg2]);
Toast.makeText(Control_GridViewActivity.this,
String.valueOf(arg2) + "name:" + name,
Toast.LENGTH_LONG).show();
}
});
}
class myadpter extends BaseAdapter {
Context ct;
public myadpter(Context ct) {
this.ct = ct;
}
public int getCount() {
// TODO Auto-generated method stub
return 20;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return null;
ImageView iv = new ImageView(ct);
iv.setLayoutParams(new GridView.LayoutParams(85, 85));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(),
mygroup[position]));
return iv;
}
}
int[] mygroup = { R.raw.jwc1, R.raw.jwc2, R.raw.jwc3, R.raw.jwc4,
R.raw.jwc5, R.raw.jwc1, R.raw.jwc2, R.raw.jwc3, R.raw.jwc4,
R.raw.jwc5, R.raw.jwc1, R.raw.jwc2, R.raw.jwc3, R.raw.jwc4,
R.raw.jwc5, R.raw.jwc1, R.raw.jwc2, R.raw.jwc3, R.raw.jwc4,
R.raw.jwc5 };
}