我希望在ViewPager的第一个位置时隐藏左箭头,并在ViewPager的最后位置时隐藏右箭头。然后,我还想用TextView显示当前页码(5/30)。 5是当前页码,30是总页码。谢谢你。
这是我的代码;
mPager.beginFakeDrag();
leftNav = (ImageButton) findViewById(R.id.left_nav);
rightNav = (ImageButton) findViewById(R.id.right_nav);
txt_count = (ImageButton) findViewById(R.id.txt);
int tab = mPager.getCurrentItem();
if (tab == 0){
leftNav.setVisibility(View.GONE); // hide all position :(
}
leftNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = mPager.getCurrentItem();
if (tab > 0) {
tab--;
mPager.setCurrentItem(tab);
} else if (tab == 0) {
mPager.setCurrentItem(tab);
}
}
});
rightNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = mPager.getCurrentItem();
tab++;
mPager.setCurrentItem(tab);
}
});
答案 0 :(得分:1)
您可以使用:
viewPager.getCurrentItem()
ViewPager.getAdapter().getCount() The getAdapter() method returns the object that supplies the pages for the ViewPager.
答案 1 :(得分:0)
要在ViewPager
中获取所有页数,请使用viewPager.getAdapter()
viewPager.getAdapter().getCount()
要查找ViewPager
使用ViewPager.getCuurentItem()
viewPager.getCurrentItem()
答案 2 :(得分:0)
添加onPageChangeListener,如下所示
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == (mPager.getAdapter().getCount() - 1)) {
rightNav.setVisibility(View.GONE);
} else {
rightNav.setVisibility(View.VISIBLE);
}
if (position == 0) {
leftNav.setVisibility(View.GONE);
} else {
leftNav.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
您点击左侧和右侧的聆听者右箭头按钮
leftNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPager.getAdapter() != null && mPager.getAdapter().getCount() != null && mPager.getCurrentItem() != 0) {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
});
rightNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPager.getAdapter() != null && mPager.getAdapter().getCount() != null && mPager.getCurrentItem() != (mPager.getAdapter().getCount() - 1)) {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
});
答案 3 :(得分:0)
使用OnPageChangeListener
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
if(position == 0)
{
// hide left nav
}
else if(position == mPager.getAdapter().getCount() - 1) // is last position
{
// hide right nav
}
else
{
// show all nav
}
}
}
<强>参考强>