我要如何刷卡全屏选择的网格图像我要、网格、全屏、图像

2023-09-07 22:11:53 作者:北溟有鱼

我创建了一个网格视图图像应用程序,我想显示图像刷卡所选的图像。

其实我实现了图像轻扫在我的应用程序的,但的问题是图像刷卡开始从第一图像不是从选定的图像。

例如:

如果我选择第3图像图像轻扫应该开始从第3图像不会从第一形象。

我的code:

MainActivity.java

 包com.td.gridview;

进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.ViewGroup.LayoutParams;
进口android.view.ViewTreeObserver;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;
进口android.widget.TextView;
进口android.widget.AdapterView.OnItemClickListener;

公共类MainActivity延伸活动{

    私人的GridView photoGrid;
    私人诠释mPhotoSize,mPhotoSpacing;
    私人ImageAdapter imageAdapter;

    //一些项目添加到网格
    私有静态最后的String [] CONTENT =新的String []
            {
            PIC1
            PIC2
            PIC3
            pic4
            pic5
            pic6
            pic7
            PIC8
            pic9
            };
    静态最终诠释[]图标=新INT []
            {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
            R.drawable.pic9
            };

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        //获取照片的大小和间距
        mPhotoSize = getResources()getDimensionPixelSize(R.dimen.photo_size)。
        mPhotoSpacing = getResources()getDimensionPixelSize(R.dimen.photo_spacing)。

        //初始化图像适配器
        imageAdapter =新ImageAdapter();

        photoGrid =(GridView控件)findViewById(R.id.albumGrid);

        //开始发送图像全屏

        / **
         *在单人的GridView项Click事件
         * * /
        photoGrid.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,视图V,
                    INT位置,长的id){

                //发送图片ID为FullScreenActivity
                意图I =新的意图(getApplicationContext(),SwipeActivity.class);
                //通过数组索引
                i.putExtra(ID,位置);
                startActivity(ⅰ);
            }
        });
        //端发送图像全屏

        //设置图像适配器到GridView
        photoGrid.setAdapter(imageAdapter);

        //取得网格的视图树观察者和动态设置高度和NUMCOLS
        photoGrid.getViewTreeObserver()。addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
            @覆盖
            公共无效onGlobalLayout(){
                如果(imageAdapter.getNumColumns()== 0){
                    最终诠释为numColumns =(INT)Math.floor(photoGrid.getWidth()/(mPhotoSize + mPhotoSpacing));
                    如果(numColumns列大于0){
                        最终诠释columnWidth中=(photoGrid.getWidth()/为numColumns) -  mPhotoSpacing;
                        imageAdapter.setNumColumns(为numColumns);
                        imageAdapter.setItemHeight(columnWidth时);

                    }
                }
            }
        });
    }

    // ///////// ImageAdapter类/////////////////
    公共类ImageAdapter扩展了BaseAdapter {
        私人LayoutInflater mInflater;
        私人诠释mItemHeight = 0;
        私人诠释mNumColumns = 0;
        私人RelativeLayout.LayoutParams mImageViewLayoutParams;

        公共ImageAdapter(){
            mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        公众诠释getCount将(){
            返回CONTENT.length;
        }

        //设置NUMCOLS
        公共无效setNumColumns(INT为numColumns){
            mNumColumns =为numColumns;
        }

        公众诠释getNumColumns(){
            返回mNumColumns;
        }

        //设置照片项目高度
        公共无效setItemHeight(INT高度){
            如果(高== mItemHeight){
                返回;
            }
            mItemHeight =高度;
            mImageViewLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,mItemHeight);
            notifyDataSetChanged();
        }

        公共对象的getItem(INT位置){
            返回的位置;
        }

        众长getItemId(INT位置){
            返回的位置;
        }

        公共查看getView(最终诠释的立场,观点来看,一个ViewGroup父){

            如果(查看== NULL)
                鉴于= mInflater.inflate(R.layout.photo_item,NULL);

            ImageView的盖=(ImageView的)view.findViewById(R.id.cover);
            TextView的标题=(TextView中)view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            //检查我们的高度计算列的宽度相匹配
            如果(cover.getLayoutParams()。高度!= mItemHeight){
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(图标[位置%ICONS.length]);
            title.setText(含量[位置%CONTENT.length]);

            返回查看;
        }
    }

}
 
AI背景网格问题,下面这个图片后面的网格怎么做的

SwipeActivity.java

 包com.td.gridview;

进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.view.PagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ImageView;
进口com.td.gridview.MainActivity;



公共类SwipeActivity扩展活动
{

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.swipe_view);

    INT []图标= MainActivity.ICONS;

    //获取图像位置

    意图N = getIntent();
    INT位置= n.getIntExtra(ID,位置);

    ViewPager viewPager =(ViewPager)findViewById(R.id.view_pager);
    ImagePagerAdapter适配器=新ImagePagerAdapter(SwipeActivity.this,MainActivity.ICONS [位置]);
    viewPager.setAdapter(适配器);
    viewPager.setCurrentItem(位置);
  }

  私有类ImagePagerAdapter扩展PagerAdapter
  {
      INT []图标= MainActivity.ICONS;

    @覆盖
    公众诠释getCount将()
    {
      返回icons.length;
    }

    @覆盖
    公共布尔isViewFromObject(查看视图,Object对象)
    {
      返回查看==((ImageView的)对象);
    }

    @覆盖
    公共对象instantiateItem(ViewGroup中的容器,INT位置)
    {
      上下文的背景下= SwipeActivity.this;
      ImageView的ImageView的=新ImageView的(上下文);
// INT填充= context.getResources()。getDimensionPixelSize(
// R.dimen.padding_large);
// imageView.setPadding(填充,填充,填充,填充);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(图标[位置]);
      ((ViewPager)容器).addView(ImageView的,0);
      返回ImageView的;
    }

    @覆盖
    公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象)
    {
      ((ViewPager)容器).removeView((ImageView的)对象);
    }
  }
}
 

解决方案

后过多的废话/错误,我终于得到了答案,这是非常简单的...;)

  //获取意图数据
    意图I = getIntent();

    //选择的图片ID
    。INT位置= i.getExtras()调用getInt(ID);

ViewPager viewPager =(ViewPager)findViewById(R.id.view_pager);
    ImagePagerAdapter适配器=新ImagePagerAdapter();
    viewPager.setAdapter(适配器);
    viewPager.setCurrentItem(位置);
 

这是简单的答案,而不是这个code:

  ImagePagerAdapter适配器=新ImagePagerAdapter(mContext,图标[位置]);
 

这个工作。

  viewPager.setCurrentItem(位置);
 

现在下面是我的洞的应用程序code:

MainActivity.java

 包com.td.gridview;

进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.ViewGroup.LayoutParams;
进口android.view.ViewTreeObserver;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;
进口android.widget.TextView;
进口android.widget.AdapterView.OnItemClickListener;

公共类MainActivity延伸活动{


    私人的GridView photoGrid;
    私人诠释mPhotoSize,mPhotoSpacing;
    私人ImageAdapter imageAdapter;

    //一些项目添加到网格
    私有静态最后的String [] CONTENT =新的String []
            {
            阿肯,贾斯汀·比伯,好,大肖恩,
            布兰妮斯皮尔斯,希拉里,迈克尔布雷,阿肯,贾斯汀·比伯,好,大肖恩,
            布兰妮斯皮尔斯,希拉里,迈克尔布雷,布兰妮斯皮尔斯,希拉里,迈克尔布雷,阿肯
            贾斯汀·比伯,好,大肖恩,布兰妮斯皮尔斯,希拉里,迈克尔布雷,阿肯
            贾斯汀·比伯,好,大肖恩,布兰妮斯皮尔斯,希拉里,迈克尔布雷,阿肯
            贾斯汀·比伯,好,大肖恩,布兰妮斯皮尔斯,希拉里,迈克尔布雷,布兰妮斯皮尔斯
            希拉里,迈克尔布雷,阿肯,贾斯汀·比伯,好,大肖恩,布兰妮斯皮尔斯,希拉里,
            迈克尔布雷
            };
    静态最终诠释[]图标=新INT []
            {
            R.drawable.cover_akon,R.drawable.cover_justin,
            R.drawable.cover_alright,R.drawable.cover_big_sean,R.drawable.cover_britney,R.drawable.cover_hilary,
            R.drawable.cover_mb,R.drawable.cover_akon,R.drawable.cover_justin,R.drawable.cover_alright,
            R.drawable.cover_big_sean,R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,
            R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,R.drawable.cover_akon,
            R.drawable.cover_justin,R.drawable.cover_alright,R.drawable.cover_big_sean,R.drawable.cover_britney,
            R.drawable.cover_hilary,R.drawable.cover_mb,R.drawable.cover_akon,R.drawable.cover_justin,
            R.drawable.cover_alright,R.drawable.cover_big_sean,R.drawable.cover_britney,R.drawable.cover_hilary,
            R.drawable.cover_mb,R.drawable.cover_akon,R.drawable.cover_justin,R.drawable.cover_alright,
            R.drawable.cover_big_sean,R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,
            R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,R.drawable.cover_akon,
            R.drawable.cover_justin,R.drawable.cover_alright,R.drawable.cover_big_sean,R.drawable.cover_britney,
            R.drawable.cover_hilary,R.drawable.cover_mb
            };

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);



        //获取照片的大小和间距
        mPhotoSize = getResources()getDimensionPixelSize(R.dimen.photo_size)。
        mPhotoSpacing = getResources()getDimensionPixelSize(R.dimen.photo_spacing)。

        //初始化图像适配器
        imageAdapter =新ImageAdapter();

        photoGrid =(GridView控件)findViewById(R.id.albumGrid);

        //开始发送图像全屏

        / **
         *在单人的GridView项Click事件
         * * /
        photoGrid.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,视图V,
                    INT位置,长的id){

                //发送图片ID为FullScreenActivity
                意图I =新的意图(getApplicationContext(),SwipeActivity.class);
                //通过数组索引
                i.putExtra(ID,位置);
                startActivity(ⅰ);
            }
        });
        //端发送图像全屏

        //设置图像适配器到GridView
        photoGrid.setAdapter(imageAdapter);

        //取得网格的视图树观察者和动态设置高度和NUMCOLS
        photoGrid.getViewTreeObserver()。addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
            @覆盖
            公共无效onGlobalLayout(){
                如果(imageAdapter.getNumColumns()== 0){
                    最终诠释为numColumns =(INT)Math.floor(photoGrid.getWidth()/(mPhotoSize + mPhotoSpacing));
                    如果(numColumns列大于0){
                        最终诠释columnWidth中=(photoGrid.getWidth()/为numColumns) -  mPhotoSpacing;
                        imageAdapter.setNumColumns(为numColumns);
                        imageAdapter.setItemHeight(columnWidth时);

                    }
                }
            }
        });
    }

    // ///////// ImageAdapter类/////////////////
    公共类ImageAdapter扩展了BaseAdapter {
        私人LayoutInflater mInflater;
        私人诠释mItemHeight = 0;
        私人诠释mNumColumns = 0;
        私人RelativeLayout.LayoutParams mImageViewLayoutParams;

        公共ImageAdapter(){
            mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        公众诠释getCount将(){
            返回CONTENT.length;
        }

        //设置NUMCOLS
        公共无效setNumColumns(INT为numColumns){
            mNumColumns =为numColumns;
        }

        公众诠释getNumColumns(){
            返回mNumColumns;
        }

        //设置照片项目高度
        公共无效setItemHeight(INT高度){
            如果(高== mItemHeight){
                返回;
            }
            mItemHeight =高度;
            mImageViewLayoutParams =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,mItemHeight);
            notifyDataSetChanged();
        }

        公共对象的getItem(INT位置){
            返回的位置;
        }

        众长getItemId(INT位置){
            返回的位置;
        }

        公共查看getView(最终诠释的立场,观点来看,一个ViewGroup父){

            如果(查看== NULL)
                鉴于= mInflater.inflate(R.layout.photo_item,NULL);

            ImageView的盖=(ImageView的)view.findViewById(R.id.cover);
            TextView的标题=(TextView中)view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            //检查我们的高度计算列的宽度相匹配
            如果(cover.getLayoutParams()。高度!= mItemHeight){
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(图标[位置%ICONS.length]);
            title.setText(含量[位置%CONTENT.length]);

            返回查看;
        }
    }

}
 

SwipeActivity.java

 包com.td.gridview;

    进口android.app.Activity;
    进口android.content.Context;
    进口android.content.Intent;
    进口android.os.Bundle;
    进口android.support.v4.view.PagerAdapter;
    进口android.support.v4.view.ViewPager;
    进口android.view.View;
    进口android.view.ViewGroup;
    进口android.widget.ImageView;



    公共类SwipeActivity扩展活动
    {

      @覆盖
      公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.swipe_view);

     //获取意图数据
        意图I = getIntent();

        //选择的图片ID
        。INT位置= i.getExtras()调用getInt(ID);

    ViewPager viewPager =(ViewPager)findViewById(R.id.view_pager);
    ImagePagerAdapter适配器=新ImagePagerAdapter();
    viewPager.setAdapter(适配器);
    viewPager.setCurrentItem(位置);
  }

  私有类ImagePagerAdapter扩展PagerAdapter
  {
      INT []图标= MainActivity.ICONS;

    @覆盖
    公众诠释getCount将()
    {
      返回icons.length;
    }

    @覆盖
    公共布尔isViewFromObject(查看视图,Object对象)
    {
      返回查看==((ImageView的)对象);
    }

    @覆盖
    公共对象instantiateItem(ViewGroup中的容器,INT位置)
    {
      上下文的背景下= SwipeActivity.this;
      ImageView的ImageView的=新ImageView的(上下文);
// INT填充= context.getResources()。getDimensionPixelSize(
// R.dimen.padding_large);
// imageView.setPadding(填充,填充,填充,填充);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(图标[位置]);
      ((ViewPager)容器).addView(ImageView的,0);
      返回ImageView的;
    }

    @覆盖
    公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象)
    {
      ((ViewPager)容器).removeView((ImageView的)对象);
    }
  }
}
 

activity_main.xml

 < GridView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / albumGrid
    风格=@风格/ PhotoGrid
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@彩色/黑白
    机器人:columnWidth中=@扪/ photo_size
    机器人:horizo​​ntalSpacing =@扪/ photo_spacing
    机器人:为numColumns =auto_fit
    机器人:填充=4DP
    机器人:滚动条=无
    机器人:stretchMode =columnWidth中
    机器人:verticalSpacing =@扪/ photo_spacing/>
 

photo_item.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / album_item
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>

< ImageView的
    机器人:ID =@ + ID /覆盖
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBottom =@ + ID /覆盖
    机器人:后台=#7000
    机器人:填充=6DP>

    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ellipsize =结束
        机器人:单线=真
        机器人:文字颜色=@色/白
        机器人:TEXTSIZE =12SP
        机器人:TEXTSTYLE =黑体/>
< / LinearLayout中>
< / RelativeLayout的>
 

swipe_view.xml

 < android.support.v4.view.ViewPager的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / view_pager
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/>
 

i created a grid-view image app, and i want to show selected image in image swipe.

actually i implemented image swipe in my app but the problem is image swipe starts from 1st image not from selected image.

example:

if i choose 3rd image image swipe should starts from 3rd image not from 1st image.

my code:

MainActivity.java

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    private GridView photoGrid;
    private int mPhotoSize, mPhotoSpacing;
    private ImageAdapter imageAdapter;

    // Some items to add to the GRID
    private static final String[] CONTENT = new String[] 
            { 
            "pic1",
            "pic2",
            "pic3",
            "pic4",
            "pic5",
            "pic6",
            "pic7",
            "pic8",
            "pic9"
            };
    static final int[] ICONS = new int[] 
            { 
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
            R.drawable.pic9
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the photo size and spacing
        mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
        mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

        // initialize image adapter
        imageAdapter = new ImageAdapter();

        photoGrid = (GridView) findViewById(R.id.albumGrid);

        //start sent image to full screen             

        /**
         * On Click event for Single Gridview Item
         * */
        photoGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
        //end sent image to full screen

        // set image adapter to the GridView
        photoGrid.setAdapter(imageAdapter);

        // get the view tree observer of the grid and set the height and numcols dynamically
        photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });
    }

    // ///////// ImageAdapter class /////////////////
    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

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

        // set numcols
        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        // set photo item height
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            notifyDataSetChanged();
        }

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

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

        public View getView(final int position, View view, ViewGroup parent) {

            if (view == null)
                view = mInflater.inflate(R.layout.photo_item, null);

            ImageView cover = (ImageView) view.findViewById(R.id.cover);
            TextView title = (TextView) view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (cover.getLayoutParams().height != mItemHeight) {
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(ICONS[position % ICONS.length]);
            title.setText(CONTENT[position % CONTENT.length]);

            return view;
        }
    }

}

SwipeActivity.java

    package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.td.gridview.MainActivity;



public class SwipeActivity extends Activity 
{   

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_view);

    int[] icons = MainActivity.ICONS;    

    //get image position

    Intent n = getIntent();
    int position= n.getIntExtra("id", position);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);    
    ImagePagerAdapter adapter = new ImagePagerAdapter(SwipeActivity.this , MainActivity.ICONS[position]);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);
  }

  private class ImagePagerAdapter extends PagerAdapter 
  {
      int[] icons = MainActivity.ICONS;    

    @Override
    public int getCount() 
    {
      return icons.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) 
    {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
    {
      Context context = SwipeActivity.this;
      ImageView imageView = new ImageView(context);
//      int padding = context.getResources().getDimensionPixelSize(
//          R.dimen.padding_large);
//      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(icons[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) 
    {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }
}

解决方案

after too much bullshit / errors i finally got the answer and it's very simply...;)

// get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);

this is the simple answer instead of this code:

ImagePagerAdapter adapter = new ImagePagerAdapter(mContext , icons[position]);

this worked.

viewPager.setCurrentItem(position);

now below is my hole app code:

MainActivity.java

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {


    private GridView photoGrid;
    private int mPhotoSize, mPhotoSpacing;
    private ImageAdapter imageAdapter;

    // Some items to add to the GRID
    private static final String[] CONTENT = new String[] 
            { 
            "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
            "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
            "Micheal Buble" 
            };
    static final int[] ICONS = new int[] 
            { 
            R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb 
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        // get the photo size and spacing
        mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
        mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

        // initialize image adapter
        imageAdapter = new ImageAdapter();

        photoGrid = (GridView) findViewById(R.id.albumGrid);

        //start sent image to full screen             

        /**
         * On Click event for Single Gridview Item
         * */
        photoGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
        //end sent image to full screen

        // set image adapter to the GridView
        photoGrid.setAdapter(imageAdapter);

        // get the view tree observer of the grid and set the height and numcols dynamically
        photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });
    }

    // ///////// ImageAdapter class /////////////////
    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

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

        // set numcols
        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        // set photo item height
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            notifyDataSetChanged();
        }

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

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

        public View getView(final int position, View view, ViewGroup parent) {

            if (view == null)
                view = mInflater.inflate(R.layout.photo_item, null);

            ImageView cover = (ImageView) view.findViewById(R.id.cover);
            TextView title = (TextView) view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (cover.getLayoutParams().height != mItemHeight) {
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(ICONS[position % ICONS.length]);
            title.setText(CONTENT[position % CONTENT.length]);

            return view;
        }
    }

}

SwipeActivity.java

    package com.td.gridview;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;



    public class SwipeActivity extends Activity 
    {   

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.swipe_view);

     // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);
  }

  private class ImagePagerAdapter extends PagerAdapter 
  {
      int[] icons = MainActivity.ICONS;    

    @Override
    public int getCount() 
    {
      return icons.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) 
    {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
    {
      Context context = SwipeActivity.this;
      ImageView imageView = new ImageView(context);
//      int padding = context.getResources().getDimensionPixelSize(
//          R.dimen.padding_large);
//      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(icons[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) 
    {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }
}

activity_main.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/albumGrid"
    style="@style/PhotoGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:columnWidth="@dimen/photo_size"
    android:horizontalSpacing="@dimen/photo_spacing"
    android:numColumns="auto_fit"
    android:padding="4dp"
    android:scrollbars="none"
    android:stretchMode="columnWidth"
    android:verticalSpacing="@dimen/photo_spacing" />

photo_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/cover"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cover"
    android:background="#70000000"
    android:padding="6dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>

swipe_view.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />