我想为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());
答案 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)
{
}
});