gpt4 book ai didi

android - 如何通过 Viewpager 的 setCurrentItem 方法使我的箭头不可见?

转载 作者:行者123 更新时间:2023-11-29 20:29:32 25 4
gpt4 key购买 nike

在我的 MainActivity 的 Oncreate 中,我添加了这段代码:

mDateAreaSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {

// Hide right arrow if reach last position
if (arg0 == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}

// Hide left arrow if reach first position
else if (arg0 == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}

// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
}

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

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});

然后在按钮上单击我添加此代码:

mDateAreaSlider.setCurrentItem(0);

但问题是它可以滑动到我选择的页面,但不会让我的箭头不可见。

注意:这两个箭头是主布局中的ImageView,不是膨胀布局。所以它与适配器无关。

最佳答案

将您的原始代码替换为:

final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if (position == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}

// Hide left arrow if reach first position
else if (position == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}

// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
};
mDateAreaSlider.addOnPageChangeListener(onPageChangeListener);

在您的按钮点击时执行此代码

mDateAreaSlider.setCurrentItem(0);
mDateAreaSlider.post(new Runnable() {
@Override
public void run() {
onPageChangeListener.onPageSelected(0);
}
});

关于android - 如何通过 Viewpager 的 setCurrentItem 方法使我的箭头不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32194593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com