实现效果:根据数组内容动态绘制gridview,同时每个item都有长按、单击事件,长按事件里又嵌套新的单击事件。
xml代码:
app_grid_view_item.xml,用于配置gridview中一个item 的布局:
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:layout_width="fill_parent">
android:layout_centerHorizontal="true"
android:id="@+id/itemImage" >
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/itemText">
android:layout_centerHorizontal="true"
android:id="@+id/deleteView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="@drawable/delete">
activity_main.xml布局文件,用于配置gridview的布局:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Activity代码:
private int countOfApps;
private List
private List
private GridView mGridview;
MySimpleAdapter myadapter = new MySimpleAdapter(MainActivity.this);
mGridview.setAdapter(myadapter);
mGridview.setOnItemClickListener(this);
public final class AppViewHolder {
public ImageView img;
public ImageView delete;
public TextView title;
}
public class MySimpleAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context = null;
public MySimpleAdapter(Context contextpara) {
this.context = contextpara;
mInflater = LayoutInflater.from(context);
}
//决定gridview中有多少个item
@Override
public int getCount() {
// TODO Auto-generated method stub
return countOfApps + 1;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//注意:getView每过一段时间被调用一次,其作用是刷新gridview 中的单个item,至于gridview中有多少个item,则是由getCount()函数决定的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub