不要在ViewPager中使用PageListener

时间:2015-01-28 10:54:27

标签: android android-viewpager

我想为ViewPager设置PageListener,但我的代码不起作用。

这是我的代码

听课:

private class PageListener extends ViewPager.SimpleOnPageChangeListener {
    public void onPageSelected(int position) {
        currentPage = position;
        if (currentPage == 0) {
            ((DashboardActivity) getActivity()).hideArcMenu();
        } else if (currentPage == 1) {
            ((DashboardActivity) getActivity()).hideArcMenu();
        } else if (currentPage == 2) {
            if (DatabaseManager.getInstance(getActivity()).getAllHistoryCardsCountById(((DashboardActivity) getActivity()).getCarId()) <= 0) {
                ((DashboardActivity) getActivity()).showArcMenu();
            }
        }
    }
}

并设置为ViewPager:

vpPager.setOnPageChangeListener(new PageListener());

1 个答案:

答案 0 :(得分:2)

试试这段代码:

vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
     @Override
     public void onPageSelected(int position) 
     {
        currentPage = position;
        if (currentPage == 0) {
            ((DashboardActivity) getActivity()).hideArcMenu();
        } else if (currentPage == 1) {
            ((DashboardActivity) getActivity()).hideArcMenu();
        } else if (currentPage == 2) {
            if (DatabaseManager.getInstance(getActivity()).getAllHistoryCardsCountById(((DashboardActivity) getActivity()).getCarId()) <= 0) {
                ((DashboardActivity) getActivity()).showArcMenu();
            }
        }     
     }
     @Override
     public void onPageScrollStateChanged(int state)
     {
     }
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
     {
     }
});