6

我使用以下示例设置了一个 simpleonpagechangelistener:

Android SimpleOnPageChangeListener - 确定滑动方向

但是,当我执行代码然后滑动 viewpager 时,我希望调用以下行:

public void onPageSelected(int position) {

然而,它似乎从来没有这样做——这导致了一个非功能性的 viewPager。

有什么建议么?

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


        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);

        }
    }
}

XML:

 <android.support.v4.view.ViewPager
  android:id="@+id/view_pager"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent" />
4

5 回答 5

29

我使用的是 ViewPagerIndicator,结果发现当你这样做时,你应该在指示器上而不是在 viewpager 上设置 OnPageChangeListener。(如记录

这样做:

    mIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            doYourThing();
        }
    });

代替:

    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            doYourThing();
        }
    });

如果您使用的是SlidingTabLayout ,同样的事情也适用

于 2014-05-06T14:50:17.663 回答
1

采用

 viewPager.setOnPageChangeListener(mPageChangeListener);

消除

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

}

 setOnPageChangeListener(mPageChangeListener);
于 2013-12-13T15:07:59.987 回答
0

您是否尝试使用 ViewPager.setOnPageChangeListener 而不是您的 SimpleChangeListener?就试一试吧:

mPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int pos) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int pos) {

        }
    });
于 2013-12-13T15:08:38.633 回答
0

我现在遇到了同样的问题 - 我还将一个变量设置为等于:

 mPageChangeListener =  new ViewPager.OnPageChangeListener() {<methods>}

然后通过以下方式将其分配给我的查看器:

 mPager.setOnPageChangeListener(mPageChangeListener)

您的代码不起作用的原因是您在 onCreate 方法中创建了局部变量。有两种方法可以解决此问题:

  1. 您可以在设置器中为 onPageChangeListener 分配代码,以便:

mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {<methods>})

  1. 您可以为您的“mPageChangeListener”创建一个成员变量,以便在调用 onCreate 之前对其进行初始化。因此,将您的 mPageChangeListener 代码复制到您的 onCreate 方法上方并在那里进行初始化。
于 2015-04-30T20:17:07.943 回答
0
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            public void onPageSelected(int currentPage) {
                // work here after changing page
            }

        });
于 2015-10-11T20:41:23.537 回答