如何在网格视图中的图像之间滑动?

时间:2014-03-17 11:04:42

标签: android gridview android-fragments android-viewpager custom-adapter

我有什么是片段之间的许多选项卡..每个片段都有网格视图,每次我们在网格视图中按下图像时,它应该在新活动中全屏显示..我想添加滑动功能这个全屏图像,所以用户可以在网格视图中的其他图像之间左右滑动...但我一直看到全屏活动上的错误和日志给出了点指针EXCEPTION !!!请帮忙..

首先,这里是其中一个片段类的代码:

public class KitchenBlinds extends Fragment {
    public Integer[] mThumbIds = {
            R.drawable.kit_1, R.drawable.kit_2,
            R.drawable.kit_3, R.drawable.kit_4,
            R.drawable.kit_5, R.drawable.kit_6,
            R.drawable.kit_7, R.drawable.kit_8,
            R.drawable.kit_9, R.drawable.kit_10,
            R.drawable.kit_11, R.drawable.kit_12,
            R.drawable.kit_13, R.drawable.kit_14,
            R.drawable.kit_15
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);

    GridView gridview = (GridView)view.findViewById(R.id.grid_view);
      try{
        // Instance of ImageAdapter Class
            gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds)); 
      } catch (OutOfMemoryError E) { 
        E.printStackTrace();
    }


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

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getActivity(), FullImageActivity2.class);
                // passing array index
                 i.putExtra("id", mThumbIds[position]);

                    Log.d("ID", "" + mThumbIds[position]);
                     startActivity(i);
            }   
        });


    return view;    
}


}

然后这是网格视图类的图像适配器的代码:

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;


        // Keep all Images in array
        public Integer[] mThumbIds = {};


        public ImageAdapter(Context c,Integer[] mThumbIds2){
            mContext = c;
            this.mThumbIds=mThumbIds2;
        }



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

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {         
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setBackgroundResource(R.layout.edit_border);
            imageView.setPadding(3, 3, 3, 3);

            return imageView;
        }
    }

以下是Image Pager Adapter的代码:

public class ImagePagerAdapter  extends PagerAdapter{
    private List<ImageView> images;

    public ImagePagerAdapter(List<ImageView> images) {
        this.images = images;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = images.get(position);
        container.addView(imageView);
        return imageView;
    }

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

    @Override
    public int getCount() {
        return images.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object o) {
        // TODO Auto-generated method stub
          return view == o;
    }
}

最后这里是每次我选择图片时崩溃的全屏活动的代码:

 public class FullImageActivity2 extends Activity {


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_full_image_activity2);
     // Loop through the ids to create a list of full screen image views
        ImageAdapter imageAdapter = new ImageAdapter(this);
        List<ImageView> images = new ArrayList<ImageView>();

        for (int i = 0; i < imageAdapter.getCount(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageAdapter.mThumbIds[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            images.add(imageView);
        }

        // Finally create the adapter
        ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(imagePagerAdapter);

        // Set the ViewPager to point to the selected image from the previous activity
        // Selected image id
        int position = getIntent().getExtras().getInt("id");
        viewPager.setCurrentItem(position);
    }
    }

我一直看到这行错误:

  ImageAdapter imageAdapter = new ImageAdapter(this);

因为我在图像适配器中配置我的构造函数来获取两个工作正常的参数,但我不知道如何在这里传递它! 请帮忙 !!而且事先提前......

1 个答案:

答案 0 :(得分:1)

嗯..我通过在厨房百叶窗活动中发送数组来解决问题:

i.putExtra("array", mThumbIds);

然后我像这样收到它并将其添加到构造函数中:

Intent i = getIntent();
Object[] s = (Object[]) getIntent().getSerializableExtra("array");
Integer[] newArray = Arrays.copyOf(s, s.length, Integer[].class);

并将其添加到构造函数中:

ImageAdapter imageAdapter = new ImageAdapter(this, newArray);