protected Context mContext;
public List<AppsItemInfo> list;
public GridviewAdapter(Context context,List<AppsItemInfo> list){
mContext = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
View view = convertView;
if (null == view) {
holder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(
R.layout.list_item, null);
holder.img = (ImageView)view.findViewById(R.id.IM_image);
holder.title = (TextView)view.findViewById(R.id.TV_title);
view.setTag(holder);
}else {
holder = (ViewHolder)view.getTag();
}
holder.img.setBackgroundDrawable(list.get(position).getIcon());
holder.title.setText(list.get(position).getLabel());
return view;
}
public final class ViewHolder{
public ImageView img;
public TextView title;
}
}
list_item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="100dp"
android:gravity="center"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
>
<ImageView
android:id="@+id/IM_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
/>
<TextView
style="@style/gridview"
android:id="@+id/TV_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/IM_image"
android:layout_marginTop="6dp"
android:singleLine="true"
android:layout_alignLeft="@+id/IM_image"
android:layout_alignRight="@+id/IM_image"
android:gravity="center"
android:textColor="@color/white"
/>
</RelativeLayout>
在activity中的用法:
GV_showAllApp = (GridView) findViewById(R.id.GV_showAllApp);
gridviewAdapter = new GridviewAdapter(this, getData());//getData为填充数据
GV_showAllApp.setAdapter(gridviewAdapter);