在网格视图中动态添加网格项目 [英] Dynamically adding grid items in grid view
本文介绍了在网格视图中动态添加网格项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在网格视图中动态添加网格项目?目前,我有一个包含我的图像的适配器。我想从网址中获取图片并将其动态添加到网格视图中。
并为该网格视图设置该自定义适配器。这里是网格项目的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
{
上下文上下文;
public ImageAdapter(Context context)
{
context = context;
$ b @Override
public int getCount()
{
//返回网格中元素的数字
return 9 ;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
View v = convertView;
if(convertView == null)
{
//这里我们填充布局
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item,null);
//在这里添加图像
ImageView iv =(ImageView)v.findViewById(R.id.grid_item_image);
iv.setImageResource(R.drawable.icon);
}
return v;
}
@Override
public Object getItem(int arg0){
// TODO自动生成的方法存根
返回null;
}
@Override
public long getItemId(int arg0){
// TODO自动生成的方法存根
return 0;
希望这能帮助你。
How can I dynamically add grid items in grid view? Currently, I have an adapter containing my images. I want to get my images from an URL and dynamically add them to my grid view.
解决方案
Create custom adapter for grid view. And set that custom adapter for gird view. Here is the xml code for grid item.
<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>
and here is xml for main layout.
<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>
and here is custom adapter class which extends from 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;
}
}
Hope this can help u.
这篇关于在网格视图中动态添加网格项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文