我正在尝试使用RadioGroup
来处理我的ViewPager
屏幕切换。但是,ViewPager
只是不会滚动到指定的位置。我很困惑,我是那个没做对的人。以下是我的代码
寻呼机听众
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
switch (position) {
case 0:
switcherGroup.check(R.id.view1);
break;
case 1:
switcherGroup.check(R.id.view2);
break;
case 2:
switcherGroup.check(R.id.view3);
break;
case 3:
switcherGroup.check(R.id.view4);
break;
case 4:
switcherGroup.check(R.id.view5);
break;
default:
break;
}
}
@Override
public void onPageSelected(int position) {
//
}
@Override
public void onPageScrollStateChanged(int state) {
//
}
});
以上工作正常。但是,当我添加RadioGroup CheckListener时,那就是问题所在。
RagioGroup 当if
一直使屏幕随意切换时,我添加了pager.setCurrentItem(value, true)
次检查。就像它根本没有遵循指示一样。
private final RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.loanView:
if (pager.getCurrentItem() != 0) {
pager.setCurrentItem(0, true);
}
break;
case R.id.loanProfileView:
if (pager.getCurrentItem() != 1) {
pager.setCurrentItem(1, true);
}
break;
case R.id.loanEmployeeView:
if (pager.getCurrentItem() != 2) {
pager.setCurrentItem(2, true);
}
break;
case R.id.loanHouseView:
if (pager.getCurrentItem() != 3) {
pager.setCurrentItem(3, true);
}
break;
case R.id.loanIncomeView:
if (pager.getCurrentItem() != 4) {
pager.setCurrentItem(4, true);
}
break;
default:
break;
}
}
};
我的寻呼机适配器
公共类PagersAdapter扩展了FragmentPagerAdapter {
public PagersAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Frag1();
switch (position) {
case 0:
fragment = new Frag1();
break;
case 1:
fragment = new Frag2();
break;
case 2:
fragment = new Frag3();
break;
case 3:
fragment = new Frag4();
break;
case 4:
fragment = new Frag5();
}
return fragment;
}
@Override
public int getCount() {
return 5;
}
任何帮助将不胜感激。感谢
答案 0 :(得分:2)
你有两个错误
onPageScrolled()
ViewPager.OnPageChangeListener
你应该使用
onPageSelected()
而不是RadioGroup.OnCheckedChangeListener
侦听器,反之亦然。将ViewPager.OnPageChangeListener初始化为变量并改为使用onPageSelected()
。
private final ViewPager.SimpleOnPageChangeListener simpleOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Logic
}
};
并取消设置每个侦听器上的另一个侦听器,并在函数末尾将它们分配回来,以便不会循环触发侦听器并获取stackoverflows。
private final RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
pager.removeOnPageChangeListener(simpleOnPageChangeListener);
// Logic
pager.addOnPageChangeListener(simpleOnPageChangeListener);
}
};
private final ViewPager.SimpleOnPageChangeListener simpleOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
radioGroup.setOnCheckedChangeListener(null);
// Logic
radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
}
};