因为代码太多了,所以只能分两次发上来:
各位看官请见谅啊
/** * 根据频道完成频道分类布局 */
private void initTab() {
tabContainer.removeAllViews();
int count=defaultChannels.size();
LayoutInflater inflater=LayoutInflater.from(this);
for(int i=0;i<count;i++){
RadioButton columnRadio=(RadioButton)inflater.inflate(R.layout.top_category_item_radio, null);
columnRadio.setId(i);
columnRadio.setText(defaultChannels.get(i).getName());
RadioGroup.LayoutParams params=new LayoutParams(channelItemWidth,LayoutParams.WRAP_CONTENT);
params.leftMargin=5;
params.rightMargin=5;
columnRadio.setPadding(5, 5, 5, 5);
if(defaultChannels.get(i).getChecked()==1){
columnRadio.setChecked(true);
//columnRadio.setTextColor(color.white);
} tabContainer.addView(columnRadio,i,params);
} tabContainer.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
viewPager.setCurrentItem(checkedId);
}
});
}
private void initViewPager() {
ArrayList<Fragment> fragments=new ArrayList<Fragment>();
for(int i=0;i<defaultChannels.size();i++){
Fragment f=new NewsFragment();
fragments.add(f);
}
NewsPagerAdapter fragmentAdapter=new NewsPagerAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(fragmentAdapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
selectTabColumn(arg0);
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void selectTabColumn(final int position) {
RadioButton rb=(RadioButton) tabContainer.getChildAt(position);
rb.setChecked(true);
ChannelItem currentChannel=defaultChannels.get(position);
// 移动ScrollView,使选中项始终出现在当前屏幕中
int rWidth=rb.getMeasuredWidth();
int left=rb.getLeft();
int len=left+rWidth/2-ViewDisplayTool.getWindowWidth(this)/2;
topCategoryScoll.smoothScrollTo(len, 0);
}
}