【发布时间】:2014-05-23 14:34:49
【问题描述】:
如何在网格视图中动态添加网格项?目前,我有一个包含我的图像的适配器。我想从一个 URL 获取我的图像并将它们动态添加到我的网格视图中。
【问题讨论】:
-
有解决这个问题的办法吗?
如何在网格视图中动态添加网格项?目前,我有一个包含我的图像的适配器。我想从一个 URL 获取我的图像并将它们动态添加到我的网格视图中。
【问题讨论】:
为网格视图创建自定义适配器。并将该自定义适配器设置为网格视图。 这是网格项的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;
}
}
希望对你有帮助。
【讨论】: