RadioGroup的setOnCheckedChangeLis

2016-11-08  本文已影响544人  苍蝇的梦

2016-11-8 遇到的一点小问题

收入界面需要用到RadioGroup来切换Viewpager的Item显示,Viewpager滑动时也需要切换RadioGroup的选中;效果如图:

收入详情

内容不多,懒得用Fragment和FragmentPagerAdapter,直接用的View和PagerAdapter。

PagerAdapter mAdapter =newPagerAdapter(){

@Override

public intgetCount() {

returnradioId.length;

}

@Override

public booleanisViewFromObject(View paramView, Object paramObject) {

returnparamView == paramObject;

}

@Override

public voiddestroyItem(ViewGroup container,intposition, Object object) {container.removeView((View) object);

}

@Override

publicObject instantiateItem(ViewGroup container,intposition) {

View view = LayoutInflater.from(context).inflate(R.layout.list_income,null);

TextView textName = (TextView) view.findViewById(R.id.name);

textName.setText(pageName[position]+":¥560.00");

container.addView(view);

returnview;

}

};

然后在mPager.addOnPageChangeListener的onPageSelected(intposition)方法里来设置RadioGroup中的RadioButton的选中切换

之前用的是mRadio.check(radioId[position]);方法,

发现RadioGroup的setOnCheckedChangeListener里的onCheckedChanged(RadioGroup group,intcheckedId)会被调用三次所以只好改成

((RadioButton)mRadio.getChildAt(position)).setChecked(true);

这样ViewPager滑动结束时就只调用一次onCheckedChanged;

在RadioGroupon的CheckedChanged设置mPager.setCurrentItem(index);会多次调用,因此只好加了个变量 booleanis PageChange。

好了,这边贴一下代码,以后再使用就直接拿。

private static final int[]radioId= {R.id.radio0,R.id.radio1,R.id.radio2,R.id.radio3};//radioButton的id

private booleanisPageChange;//防止onPageSelected与onCheckedChanged冲突

finalRadioGroup mRadio = (RadioGroup) findViewById(R.id.radio);

finalViewPager mPager = (ViewPager) findViewById(R.id.pager);

mRadio.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {

@Override

public voidonCheckedChanged(RadioGroup group,intcheckedId) {

if(isPageChange){

isPageChange=false;

return;

}

isPageChange=true;

intindex = Arrays.binarySearch(radioId, checkedId);

mPager.setCurrentItem(index);

}

});

mPager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {

@Override

public voidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels) {}

@Override

public voidonPageSelected(intposition) {

if(isPageChange){

isPageChange=false;

return;

}

isPageChange=true;

((RadioButton)mRadio.getChildAt(position)).setChecked(true);

}

@Override

public voidonPageScrollStateChanged(intstate) {}

});

PagerAdapter mAdapter =newPagerAdapter(){

@Override

public intgetCount() {

returnradioId.length;

}

@Override

public booleanisViewFromObject(View paramView, Object paramObject) {

returnparamView == paramObject;

}

@Override

public voiddestroyItem(ViewGroup container,intposition, Object object) {container.removeView((View) object);

}

@Override

publicObject instantiateItem(ViewGroup container,intposition) {

View view = LayoutInflater.from(context).inflate(R.layout.list_income,null);

TextView textName = (TextView) view.findViewById(R.id.name);

textName.setText(pageName[position]+":¥560.00");

container.addView(view);

returnview;

}

};

mPager.setOffscreenPageLimit(3);

mPager.setAdapter(mAdapter);

之前都是发的51CTO博客,换这边试试

二分法搜索的问题:http://qq445493481.blog.51cto.com/9545543/1791789

上一篇下一篇

猜你喜欢

热点阅读