如何动态添加图像到GridView?

时间:2011-11-08 16:38:55

标签: java android android-image android-gridview

我有一个基于项目数组创建的GridView。当网格滚动到底部时,我需要添加更多图像,但我不知道该怎么做。

现在我明白了以下几点:

  1. 我有一个解析器,它解析数组并将ImageIds提供给将返回ImageViews
  2. 的类
  3. 不知何故,我必须更改此数组并让适配器知道它,所以我的问题是,如何获得对此适配器的引用?
  4. 这是我的代码:

    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();  
            }
        });
    
    }
    }
    

    现在有一些冗余代码,但这是我的测试项目。我所知道的是,适配器的更新必须在firstVisibleItem + visibleItemCount == totalItemCount时(即当GridView到达底部时)。

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

    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

    <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中

    <?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>
    

    我该如何处理?

2 个答案:

答案 0 :(得分:4)

您可以通过.getAdapter()获取对支持GridView的适配器的引用。当用户向下滚动时,获取此适配器,添加新图像(我认为您应该使用ArrayList而不是简单数组,以便您可以在ImageAdapter类中轻松追加更多数据),并在现在修改的适配器上调用.notifyDatasetChanged() ,所以它会加载新数据。

答案 1 :(得分:0)

您应该可以在gridview上调用getAdapter。或者 - 它可能不是最佳实践 - 但您可以使适配器保持静态,这样您就可以从Grid活动中进行访问。然后,您可以动态地将图像添加到适配器并重置您的布局。

试一试。这就是我要做的。如果您找到更好的方式让我知道!