Android GridViewビューの例
6398 ワード
gridview.xmlファイル:
griditem.xml
ImageAdapter.JAvaファイルは次のとおりです.
GridViewActivity.JAvaファイルは次のとおりです.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" GridView "
android:textSize="20px"/>
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:textColor="#ffffff"/>
<GridView android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
griditem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView android:layout_height="wrap_content"
android:id="@+id/itemIamge"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/itemText">
</TextView>
</LinearLayout>
ImageAdapter.JAvaファイルは次のとおりです.
package com.example.baseexample;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
private class GridHolder{
ImageView appImage;
TextView appName;
}
private List<GridInfo> list;
private LayoutInflater mInflater;
private Context mContext;
public ImageAdapter(Context c){
super();
this.mContext = c;
}
public void setList(List<GridInfo> list) {
this.list = list;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int index) {
return list.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
GridHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.griditem, null);
holder = new GridHolder();
holder.appImage = (ImageView)convertView.findViewById(R.id.itemIamge);
holder.appName = (TextView)convertView.findViewById(R.id.itemText);
convertView.setTag(holder);
}else{
holder = (GridHolder) convertView.getTag();
}
GridInfo info = list.get(index);
if (info != null) {
holder.appName.setText(info.getName());
holder.appImage.setImageResource(info.getAppImage());
}
return convertView;
}
}
GridViewActivity.JAvaファイルは次のとおりです.
package com.example.baseexample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class GridViewActivity extends Activity {
private TextView tv;
private GridView gv;
private List<GridInfo> bookList;
private List<Map<String,Object>> tmpList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
gv = (GridView)findViewById(R.id.gridview);
int[] picIDs={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
int[] bookIDs={R.string.a,R.string.b,R.string.c,R.string.d,R.string.e,R.string.f};
//////////////////////////////////////////////////////////////////
// SimpleAdapter
int rowCnt = picIDs.length;
tmpList = new ArrayList<Map<String,Object>>();
for(int i=0;i<rowCnt;i++){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("picCol", picIDs[i]);
map.put("bookCol", this.getResources().getString(bookIDs[i]));
tmpList.add(map);
}
SimpleAdapter ada = new SimpleAdapter(this,tmpList,R.layout.griditem,new String[]{"picCol","bookCol"},new int[]{R.id.itemIamge,R.id.itemText});
gv.setAdapter(ada);
//////////////////////////////////////////////////////////////////////////
// Adapter, BaseAdapter
// bookList = new ArrayList<GridInfo>();
// for(int i=0;i<picIDs.length;i++){
// bookList.add(new GridInfo(this.getResources().getString(bookIDs[i]), picIDs[i]));
// }
// ImageAdapter adapter = new ImageAdapter(this);
//
// adapter.setList(bookList);
// gv.setAdapter(adapter);
////////////////////////////////////////////////////////////////////////////////
gv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
tv = (TextView)findViewById(R.id.tv);
LinearLayout l1=(LinearLayout)arg1;
TextView t1=(TextView)l1.getChildAt(1);
String str = " "+t1.getText().toString();
tv.setText(str);
}
});
}
}