我怎样才能动态图像添加到一个GridView? [英] How can I dynamically add images to a GridView?

查看:95
本文介绍了我怎样才能动态图像添加到一个GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的GridView 是基于项目的数组上创建的。我需要添加更多的图像作为电网滚动至底部,但我不知道该怎么做。

I have a GridView that is created based on an array of items. I need to add more images as the grid is scrolled to the bottom, but I'm not sure how to do that.

现在我明白以下内容:

  1. 在我有解析阵列,并提供ImageIds到将返回类的适配器 ImageViews
  2. 不知怎的,我必须要改变这个数组,让适配器知道这件事,所以我的问题是,如何获得一个引用此适配器?

这是我的code:

package com.wm.grid;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.Toast;

public class GridActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Integer[] mThumbIds12 = {
                R.drawable.sample_2, R.drawable.sample_3,R.drawable.s1,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7
    };
    final Integer[] mThumbIds1 = { 
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.s1,
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.s1,
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.s1,
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.s1,
    };
    final GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this,mThumbIds12));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(GridActivity.this, "" + position, Toast.LENGTH_SHORT).show();

        }
    });
    gridview.setOnScrollListener(new OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_IDLE:
                //List is idle
                break;
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                //Toast.makeText(GridActivity.this, "X", Toast.LENGTH_SHORT).show();
                break;
            case OnScrollListener.SCROLL_STATE_FLING:
                //Toast.makeText(GridActivity.this, "Z", Toast.LENGTH_SHORT).show();
                break;
            }   
        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if(firstVisibleItem + visibleItemCount == totalItemCount){
                Toast.makeText(GridActivity.this, "BOTTOM", Toast.LENGTH_SHORT).show();

            }
            //Toast.makeText(GridActivity.this, "TOP", Toast.LENGTH_SHORT).show();  
        }
    });

}
}

现在有一些多余的code,但是这是我的测试项目。我所知道的是,适配器的更新已经发生时, firstVisibleItem + visibleItemCount == totalItemCount (即当的GridView 已经到达底部)。

Now there is some redundant code, but this is my testing project. All I know is that the updating of the adapter has to happen when firstVisibleItem + visibleItemCount == totalItemCount (i.e. when the GridView has reached the bottom).

这是我的 ImageAdapter (基于Android开发站点)

This is my ImageAdapter (based on the Android dev site)

package com.wm.grid;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.wm.grid.R;

public class ImageAdapter extends BaseAdapter {
   private Context mContext;
   private Integer[] mThumbIds;
    public ImageAdapter(Context c,Integer[] m) {
        mContext = c;
        mThumbIds = m;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    public Integer[] AddItems(Integer[] a){
        final Integer[] aThumbIds;
        final int i;
        aThumbIds = a;
        Integer[] a2 = new Integer[mThumbIds.length + aThumbIds.length];
        System.arraycopy(mThumbIds, 0, a2, 0, mThumbIds.length);
        System.arraycopy(aThumbIds, 0, a2, mThumbIds.length, aThumbIds.length);
        return mThumbIds;
    }
}

image.xml

image.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/grid_item_image"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:src="@drawable/s1"
      >
</ImageView>

main.xml中

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
>
<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1">

<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="40pt"
android:text="row three"
android:textSize="15pt" />

</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<GridView  
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="150dp"
android:numColumns="auto_fit"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>

我应该如何看待这一点?

How should I approach this?

推荐答案

您可以参考适配器通过.getAdapter支持在GridView()。当用户向下滚动,得到这个适配器,添加新的图像(我认为你应该使用简单数组的一个ArrayList,而不是让您可以轻松地在ImageAdapter类追加更多的数据),而现在修改的适配器上调用.notifyDatasetChanged() ,所以它会在加载新的数据。

You can get reference to the adapter backing the GridView by .getAdapter(). When the user scrolled down, get this adapter, add the new images (I think you should use an ArrayList instead of simple arrays so you can easily append more data in the ImageAdapter class), and call .notifyDatasetChanged() on the now modified adapter, so it'll load in the new data.

这篇关于我怎样才能动态图像添加到一个GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆