ViewPager以图像的arrayList的第一个索引开始

时间:2014-06-07 06:27:08

标签: android gridview arraylist android-viewpager ontouchlistener

我已经制作了一个视图寻呼机滑动演示,我有一个gridView,我已经显示了来自ImageURL的arrayList的图像,现在我想在其点击的下一个活动中显示该图像,然后它在滑动上滑动,我已经为它实现了ViewPager,并且已经将适配器设置为viewPager,滑动功能正常工作,但是当我点击GridView中的任何图像时,它始终显示来自arrayList的第一个图像,但它应该显示已被点击的图像,所以总是来自arrayList的第一个位置图像是diaplsyed,那么我应该怎么做才能从我点击的图像启动ViewPager,请帮助并保存,谢谢你有愉快的一天。我的代码如下:

GridActivity.java

GridView gd_product;
ArrayList<String> resultArray;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_subcategory);
        initialize();
        sub_cat = getIntent().getStringExtra("sub_cat");
        cat_name = getIntent().getStringExtra("cat_name");
        menu.setSelectedTab(1);
        titlesubCat.setText(cat_name);

        System.out.println(":::::::::Inside catalog:::::::::");
        new GetSubCatalogList().execute();
        gd_product.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                big_image = subCatList.get(position).get(Const.TAG_BIG_IMG);
                i = new Intent(SubCatagoryActivity.this, DetailsActivity.class);
                i.putStringArrayListExtra("array", resultArray);
                i.putExtra(Const.TAG_BIG_IMG, big_image);
                i.putExtra("cat_name", cat_name);
                i.putExtra("pos", position);

                startActivity(i);

            }
        });

此处 big_Image 是单击的imageUrl,而 pos 是该Imageurl在 ResultArray(ImageURL的ArrayList)

BigImage.java

ArrayList<String> resultArray;
    private DisplayImageOptions options;
    public static ImageLoader imageLoader;
resultArray = getIntent().getStringArrayListExtra("array");
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setVisibility(View.VISIBLE);

        adapter = new ImagePagerAdapter();

        pos = getCategoryPos(big_img);
        System.out.println("::::::::::::::INDEX OF::::::::::::" + pos);
        viewPager.setCurrentItem(pos);
viewPager.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if (isOnClick) {
                        if (event.getX() > viewPager.getWidth() / 2) {
                            // go to next

                            viewPager.setCurrentItem(pos, true);

                            pos++;
                        } else {
                            viewPager.setCurrentItem(pos, true);
                            pos--;
                            // go to previous
                        }
                        return true;
                    }
                }
                return false;
            }
        }); 

private class ImagePagerAdapter extends PagerAdapter {

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = DetailsActivity.this;
            ImageView imageView;
            imageView = new ImageView(context);
            try {

                int padding = context.getResources().getDimensionPixelSize(
                        R.dimen.padding_medium);
                imageView.setPadding(padding, padding, padding, padding);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                imageLoader.displayImage(resultArray.get(position), imageView);

                ((ViewPager) container).addView(imageView, 0);
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }
int getCategoryPos(String category) {
        return resultArray.indexOf(category);
    }

1 个答案:

答案 0 :(得分:0)

您在哪里为GrodView设置适配器? 你看过here

吗?