哪一行代码告诉Android在GalleryView中显示所有图像?

时间:2013-11-06 12:40:52

标签: android image-gallery galleryview

以下代码是在Android中使用GalleryView的典型方式。但是,我不明白哪行代码实际显示所有图像1-8。

据我所知,所有图片1-8都存储在 数组ImageIds。但是,getView{...}之后的@Override内的以下代码仅显示一个特定图片(ImageIds[position]),其中position是您选择的图片。

i.setImageResource(ImageIds[position]); 

因此,哪一行代码告诉Android显示所有ImageIds图像?

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:orientation="vertical">


     <Gallery
         android:id="@+id/gallery1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" />

      <ImageView
         android:id="@+id/imageView1"
         android:layout_marginTop="100dp"
         android:layout_width="250dp"
         android:layout_gravity="center_horizontal"
         android:layout_height="250dp"
         android:src="@drawable/image1" />
</LinearLayout>

MainActivity

public class MainActivity extends Activity 
{

      ImageView selectedImage;  
      private Integer[] ImageIds = {
                 R.drawable.image1,
                 R.drawable.image2,
                 R.drawable.image3,
                 R.drawable.image4,
                 R.drawable.image5,
                 R.drawable.image6,
                 R.drawable.image7,
                 R.drawable.image8
         };
     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

              Gallery gallery = (Gallery) findViewById(R.id.gallery1);
         selectedImage=(ImageView)findViewById(R.id.imageView1);
         gallery.setSpacing(1);
         gallery.setAdapter(new GalleryImageAdapter(this));

          // clicklistener for Gallery
         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(MainActivity.this, "Your selected position = " + position, Toast.LENGTH_SHORT).show();
                // show the selected Image
                selectedImage.setImageResource(ImageIds[position]);
             }
         });
     }

 }

GalleryImageAdapter 

 public class GalleryImageAdapter extends BaseAdapter 
 {
     private Context mContext;

     private Integer[] ImageIds = {
             R.drawable.image1,
             R.drawable.image2,
             R.drawable.image3,
             R.drawable.image4,
             R.drawable.image5,
             R.drawable.image6,
             R.drawable.image7,
             R.drawable.image8
     };

     public GalleryImageAdapter(Context context) 
     {
         mContext = context;
     }

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

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

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


     // Override this method according to your need
     public View getView(int position, View view, ViewGroup viewGroup) 
     {
         // TODO Auto-generated method stub
         ImageView i = new ImageView(mContext);

         i.setImageResource(ImageIds[position]);
         i.setLayoutParams(new Gallery.LayoutParams(200, 200));

         i.setScaleType(ImageView.ScaleType.FIT_XY);

         return i;
     }
 }

4 个答案:

答案 0 :(得分:1)

setAdapter的{​​{1}}方法调用您在其中提供的适配器的所有回调方法作为参数。

即,

Gallery

调用您的gallery.setAdapter(new GalleryImageAdapter(this)); ,在GalleryImageAdapter内部调用getView()您在getCount方法中提供的次数。

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

会多次调用getView()方法ImageIds.length

答案 1 :(得分:0)

方法

public View getView(int position, View view, ViewGroup viewGroup) 
适配器中的

将图像阵列中的图像设置为适配器。当你打电话

ImageView i = new ImageView(mContext);
i.setImageResource(ImageIds[position]);

gallery.setAdapter(new GalleryImageAdapter(this));

将适配器设置为您的图库。

答案 2 :(得分:0)

当android需要显示特定页面时,它将调用getView方法来获取该页面的UI。当您浏览所有页面时,将调用getView以获取相应的位置,即1,2,3 ......

答案 3 :(得分:0)

如果您在BaseAdapter未实现的方法中进行了标记,则会有getCount方法。当我们在for循环中执行代码时,同样的功能也在这里。

getCount将返回数组的长度,它将从数组的第一个元素执行到您在getCount方法中提供的长度。

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