如何在网格视图中动态添加网格项目?目前,我有一个包含我的图像的适配器。我想从URL获取图像,并将它们动态添加到我的网格视图中。
发布于 2011-05-05 16:20:53
为网格视图创建自定义适配器。并将该自定义适配器设置为网格视图。下面是网格项目的xml代码。
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<imageview android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</imageview>
</linearlayout>下面是用于主布局的xml。
<gridview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
</gridview>下面是从BaseAdapter扩展而来的自定义适配器类
public class ImageAdapter extends BaseAdapter
{
Context context;
public ImageAdapter(Context context)
{
context = context;
}
@Override
public int getCount()
{
//return numbers of element u want on the grid
return 9;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if ( convertView == null )
{
//here we inflat the layout
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
//here add the image
ImageView iv = (ImageView)v.findViewById(R.id.grid_item_image);
iv.setImageResource(R.drawable.icon);
}
return v;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}希望这能对你有所帮助。
https://stackoverflow.com/questions/5894043
复制相似问题