【问题标题】:Dynamically adding grid items in grid view在网格视图中动态添加网格项
【发布时间】:2014-05-23 14:34:49
【问题描述】:

如何在网格视图中动态添加网格项?目前,我有一个包含我的图像的适配器。我想从一个 URL 获取我的图像并将它们动态添加到我的网格视图中。

【问题讨论】:

  • 有解决这个问题的办法吗?

标签: android gridview


【解决方案1】:

为网格视图创建自定义适配器。并将该自定义适配器设置为网格视图。 这是网格项的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;
  }
  }

希望对你有帮助。

【讨论】:

  • ...和?创建适配器并将其附加到 Gridview 并不能解决原始海报问题:我想从 URL 获取我的图像并动态将它们添加到我的网格查看.
  • @IndrekKõue 如果您仍然卡住,也许我可以帮助您做些不同的事情
猜你喜欢
  • 2019-12-02
  • 1970-01-01
  • 2014-07-15
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多