============================Activity_main布局============
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hf.pull.MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
/>
<RelativeLayout
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="2"
>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/dongtan"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:button="@null"
android:layout_weight="1"
android:gravity="center"
android:background="@drawable/select_color"
android:checked="true"
android:text="动弹"
/>
<RadioButton
android:id="@+id/boke"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:button="@null"
android:background="@drawable/select_color"
android:layout_weight="1"
android:gravity="center"
android:text="博客"
/>
<RadioButton
android:id="@+id/tuijian"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:button="@null"
android:background="@drawable/select_color"
android:layout_weight="1"
android:gravity="center"
android:text="推荐"
/>
</RadioGroup>
</RelativeLayout>
</LinearLayout>
======================Mainactivity==========================
package com.hf.pull;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.hf.fragment.BoKeDongTanFragment;
import com.hf.fragment.DongTanFragment;
import com.hf.fragment.TuiJianFragment;
public class MainActivity extends FragmentActivity {
private ViewPager vp;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找控件
findviewid();
//设置viewpager滑动适配
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new DongTanFragment();
case 1:
return new BoKeDongTanFragment();
default:
break;
}
return new TuiJianFragment();
}
});
//设置vp的滑动监听,底部跟着变动
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
//滑动
RadioButton rb = (RadioButton) rg.getChildAt(arg0);
rb.setChecked(true);
rb.setTextColor(Color.GREEN);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
//添加按钮点击监听
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i = 0; i < rg.getChildCount(); i++) {
if (rg.getChildAt(i).getId() == checkedId) {
RadioButton rb = (RadioButton) rg.getChildAt(i);
rb.setChecked(true);
rb.setTextColor(Color.GREEN);
vp.setCurrentItem(i);
}else {
RadioButton rb = (RadioButton) rg.getChildAt(i);
rb.setTextColor(Color.BLACK);
}
}
}
});
}
/**
* 查找控件
*/
private void findviewid() {
vp = (ViewPager) findViewById(R.id.vp);
rg = (RadioGroup) findViewById(R.id.rg);
//进入默认显示第一个
RadioButton rb = (RadioButton) rg.getChildAt(0);
rb.setChecked(true);
rb.setTextColor(Color.GREEN);
}
}
===============上面所new的DongtanFragment、BoKeFragment、TuiJianFragment自己在src中创建3个fragment类就可以