0

我在实现 ImagePagerAdapter 时遇到了一些麻烦 - 由于以下两个警告,我相信我没有正确实现它:

The constructor Home.ImagePagerAdapter(Activity, int[], String[]) is never used locally 

&

The value of the field Home.ImagePagerAdapter.stringArray is not used

我不确定我到底做错了什么(这是我第一次使用 PagerAdapter 构建任何东西)。

JAVA

   public class Home extends YouTubeBaseActivity implements

    VideoClickListener {
        ...
        private int mCurrentTabPosition = NO_CURRENT_POSITION;
        private static final int NO_CURRENT_POSITION = -1;
        private ViewPager mPager;
        ...
        int imageArray[];
        String[] stringArray;

        private OnPageChangeListener mPageChangeListener;
        ImagePagerAdapter adapter = new ImagePagerAdapter();

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

            final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

            viewPager.setAdapter(adapter);
    ...

            setOnPageChangeListener(mPageChangeListener);


            mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {


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

                        System.out.print(position);
                        // Moving to the right
                        String playlist = "TheMozARTGROUP‎";
                        new GetYouTubeUserVideosTask(responseHandler, playlist)
                                .execute();

                        View vg = findViewById(R.layout.home);
                        vg.invalidate();

                    } else if (position < oldPos) {
                        // Moving to the Left
                        System.out.print(position);
                        String playlist = "TheMozARTGROUP‎";
                        new GetYouTubeUserVideosTask(responseHandler, playlist)
                                .execute();

                        View vg = findViewById(R.layout.home);
                        vg.invalidate();

                    }
                    viewPager.setOnPageChangeListener(mPageChangeListener);
                }

                private void onTabChanged(PagerAdapter adapter,
                        int mCurrentTabPosition, int position) {
                    // TODO Auto-generated method stub

                }
            };

        }

        private void setOnPageChangeListener(
                OnPageChangeListener mPageChangeListener2) {
            // TODO Auto-generated method stub

        }

        ...



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

            // this is your constructor
            public ImagePagerAdapter() {
                super();

            }

            private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                    R.drawable.classical_up_btn, R.drawable.country_up_btn,
                    R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                    R.drawable.island_up_btn, R.drawable.latin_up_btn,
                    R.drawable.pop_up_btn, R.drawable.samba_up_btn };

            private String[] stringArray = new String[] { "vevo",
                    "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
                    "EminemVEVO‎" };

            @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;
            }

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

            protected void onTabChanged(final PagerAdapter adapter,
                    final int oldPosition, final int newPosition) {
                // Calc if swipe was left to right, or right to left
                if (oldPosition > newPosition) {
                    // left to right
                } else {
                    // right to left


                }
                final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

            }
        }
    }

非常感谢任何建议。

4

1 回答 1

0

对于第一个错误,删除默认构造函数

           // this is your constructor
        public ImagePagerAdapter() {
            super();

        }

对于第二个错误,在您的构造函数中,您正在分配

 stringArray = stringArra;

然后你稍后在下面重新分配它:

            private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
                "EminemVEVO‎" };

要么不重新分配它并使用现有数组,要么从构造函数中删除“stringarra”。

于 2013-12-12T23:30:33.833 回答