基本stringArray / ViewPager实现问题

时间:2013-12-18 14:42:29

标签: java android arrays android-viewpager

我创建了一个stringArray,我想用viewPager:

    private String[] stringArray = new String[] { "value1",
            "value2", "value3", "value4",
    "value5" };

我找到了这篇SO文章:

Different text for each image in image viewpager

我尝试模仿它的格式 - 但是我从日食中得到警告:

未使用字段Home.ImagePagerAdapter.stringArray的值

因此,我觉得好像没有正确链接我的stringArray和viewPager。

ViewPager:

mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                String list1 = "value1";
                Log.d("PK", "**** onPageSelected");
                new GetDataTask(responseHandler, list1)
                .execute();

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                Log.d("PK", "onPageScrolled");
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int pos) {

                Log.d("PK", "onPageScrollStateChanged");

            }
        });


mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

    @Override
            public void onPageSelected(int position) {
                onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
                position = mCurrentTabPosition;
                int oldPos = mPager.getCurrentItem();

                Log.d("PK", "Page selected");

                if (position > oldPos) {

                    System.out.print(position);
                    // Moving to the right
                    String list = "value1";


                } else if (position < oldPos) {
                    // Moving to the Left
                    System.out.print(position);
                    String list = "value1";


                }
                mPager.setOnPageChangeListener(mPageChangeListener);
            }

            private void onTabChanged(PagerAdapter adapter,
                    int mCurrentTabPosition, int position) {

                Log.d("PK", "Tab changed");
                // TODO Auto-generated method stub

            }

...


private class ImagePagerAdapter extends PagerAdapter implements
    ViewPager.OnPageChangeListener {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        public ImagePagerAdapter() {
            super();

        }

        private int[] mImages = new int[] { R.drawable.btn1,
                R.drawable.btn2, R.btn3,
                R.btn4, R.btn5 };

        private String[] stringArray = new String[] { "value1",
                "value2", "value3", "value4",
        "value5" };

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageScrollStateChanged");
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageScrolled");
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageSelected");
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要在视图寻呼机上使用OnPageListener

您的字符串数组必须是一个类字段,并且需要与ViewPager(mImages.lenght)的页数一样长。否则你将面临IndexOutOfBoundsException

mPager.setOnPageChangeListener(new OnPageChangeListener() {

                    @Override
                    public void onPageSelected(int pos) {

                            String playlist1 = stringArray[pos];
                            new GetYouTubeUserVideosTask(responseHandler, playlist1)
                            .execute();

                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int pos) {

                    }
            });