Android GridView自定义Adapter
public class AdapterActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view); GridView gridView = (GridView) findViewById(R.id.gridView1); List<HashMap<String, GridViewItem>> hashMapList = new ArrayList<HashMap<String, GridViewItem>>(); //测试数据 for (int i = 0; i < 8; i++) { HashMap<String, GridViewItem> tempHashMap = new HashMap<String, GridViewItem>(); tempHashMap.put("item", getGridViewItem(1)); hashMapList.add(tempHashMap); } MyAdapter myAdapter = new MyAdapter(this, hashMapList); gridView.setAdapter(myAdapter); } public GridViewItem getGridViewItem(int page) { Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); GridViewItem tempItem = new GridViewItem(tempBitmap, "测试一下" + new Date().getSeconds()); return tempItem; } }
/** * GradView 项 * * @author Administrator * */ public class GridViewItem { public Bitmap bitmap;// 图片 public String title;// 题标 // 待扩展 public GridViewItem() { } public GridViewItem(Bitmap bitmap, String title) { super(); this.bitmap = bitmap; this.title = title; } }
/** * * @author Administrator * */ public class MyAdapter extends BaseAdapter { private List<HashMap<String, GridViewItem>> list; private GridViewItem tempGridViewItem; private LayoutInflater layoutInflater; public MyAdapter(Context context, List<HashMap<String, GridViewItem>> list) { this.list = list; layoutInflater = LayoutInflater.from(context); } /** * 数据总数 */ @Override public int getCount() { return list.size(); } /** * 获取当前数据 */ @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (layoutInflater != null) { view = layoutInflater .inflate(R.layout.activity_gridview_item, null); ImageView imageView = (ImageView) view .findViewById(R.id.imageView1); TextView textView = (TextView) view.findViewById(R.id.title); //获取自定义的类实例 tempGridViewItem = list.get(position).get("item"); imageView.setImageBitmap(tempGridViewItem.bitmap); textView.setText(tempGridViewItem.title); } return view; } }
两个布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.51" android:horizontalSpacing="2dip" android:numColumns="auto_fit" android:verticalSpacing="2dip" > </GridView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dip" android:background="#E8F2FE" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/title" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /> <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@android:drawable/stat_sys_download" /> </LinearLayout> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。