public static final String ARGUMENTS_NAME = null;
private ViewPager vp;
private RadioGroup rg;
private ImageView img;
private int indicatorWidth;
public static String[] tabTitle = { "资讯", "热点", "博客", "推荐" }; // 标题
private List<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
rg = (RadioGroup) findViewById(R.id.rg);
img = (ImageView) findViewById(R.id.img);
list = new ArrayList<Fragment>();
list.add(new Fragments());
list.add(new RDFragments());
list.add(new BKFragments());
list.add(new TJFragments());
TabFragmentPagerAdapter mAdapter = new TabFragmentPagerAdapter(
getSupportFragmentManager());
vp.setAdapter(mAdapter);
DisplayMetrics dm = new DisplayMetrics();
// 将当前窗口的一些信息放在DisplayMetrics类中,
getWindowManager().getDefaultDisplay().getMetrics(dm);
// radiobutton的宽度
indicatorWidth = dm.widthPixels / 4;
LayoutParams params = img.getLayoutParams();
params.width = indicatorWidth;
img.setLayoutParams(params);
setlisenten();
}
@SuppressWarnings("deprecation")
private void setlisenten() {
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (rg != null && rg.getChildCount() > position) {
((RadioButton) rg.getChildAt(position)).performClick();
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
private int currentIndicatorLeft;
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.zixun:
setimg(0);
break;
case R.id.redian:
setimg(1);
break;
case R.id.boke:
setimg(2);
break;
case R.id.tuijian:
setimg(3);
break;
default:
break;
}
}
private void setimg(int checkedId) {
TranslateAnimation animation = new TranslateAnimation(
currentIndicatorLeft, ((RadioButton) rg
.getChildAt(checkedId)).getLeft(), 0f, 0f);
animation.setDuration(100);
animation.setFillAfter(true);
// 执行位移动画
img.startAnimation(animation);
vp.setCurrentItem(checkedId); // ViewPager 跟随一起 切换
// 记录当前 下标的距最左侧的 距离
currentIndicatorLeft = ((RadioButton) rg.getChildAt(checkedId))
.getLeft();
}
});
}
class TabFragmentPagerAdapter extends FragmentPagerAdapter {
public TabFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
}
布局
<RelativeLayout
android:id="@+id/rl"
android:background="#F2F2F2"
android:layout_width="match_parent"
android:layout_height="40dip" >
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="38dip"
android:orientation="horizontal" >
<RadioButton
android:layout_weight="1"
android:id="@+id/zixun"
android:button="@null"
android:gravity="center"
android:checked="true"
android:textColor="@drawable/rb_blue_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="资讯"/>
<RadioButton
android:layout_weight="1"
android:id="@+id/redian"
android:button="@null"
android:textColor="@drawable/rb_blue_bg"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热点"/>
<RadioButton
android:layout_weight="1"
android:id="@+id/boke"
android:button="@null"
android:gravity="center"
android:textColor="@drawable/rb_blue_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="博客"/>
<RadioButton
android:textColor="@drawable/rb_blue_bg"
android:layout_weight="1"
android:id="@+id/tuijian"
android:button="@null"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="推荐"/>
</RadioGroup>
<ImageView
android:id="@+id/img"
android:layout_width="1dip"
android:layout_height="5dip"
android:layout_alignParentBottom="true"
android:background="#0f0"
android:scaleType="matrix"
/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
//布局2
<com.bwie.xlistview.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f00"
android:dividerHeight="1dp"
android:id="@+id/lv"></com.bwie.xlistview.XListView>
//颜色Drawable
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#0f0" />
<item android:state_checked="false" android:color="#000000"/>
</selector>