• 结构
    21 ViewPager RadioGroup_ide

MainActivity.java

package com.qf.day21_viewpagerfragmentrg_demo4;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Layout;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;

public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;
    private RadioGroup rgMain;

    // 数据集合
    private List<Fragment> list = new ArrayList<Fragment>();
    private String[] titles = { "新闻", "娱乐", "军事", "体育" };
    private RadioButton[] rbs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化View
        initView();
        // 初始化ViewPager
        initViewPager();
        // 初始化导航书签
        initTab();

    }

    // 初始化导航书签
    public void initTab() {
        rbs = new RadioButton[titles.length];
        for (int i = 0; i < titles.length; i++) {
            // 动态创建RadioButton
            rbs[i] = new RadioButton(getApplicationContext());
            // rbs[i].setText(titles[i]);
            rbs[i].setGravity(Gravity.CENTER);
            BitmapDrawable a = null;
            rbs[i].setButtonDrawable(a);


            rbs[i].setBackgroundResource(R.drawable.selector_main);
            int screenWith = getResources().getDisplayMetrics().widthPixels;
            int eachWith = screenWith / titles.length;
            rbs[i].setWidth(eachWith);
            rgMain.addView(rbs[i]);
        }
        // 默认第0个元素是被选中的
        rbs[0].setChecked(true);
        rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for (int i = 0; i < titles.length; i++) {
                    if (rbs[i].getId() == checkedId) {
                        // Viewpager滑动到 点击的RadioButton上
                        viewPager.setCurrentItem(i);

                    }
                }
            }
        });

    }

    // 初始化ViewPager
    public void initViewPager() {

        // 获取数据源
        for (int i = 0; i < titles.length; i++) {
            MyFragment myFragment = MyFragment.getInstance(i + 1);
            list.add(myFragment);
        }

        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));

        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                rbs[arg0].setChecked(true);
            }

            @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

            }
        });
    }

    // 初始化View
    public void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rgMain = (RadioGroup) findViewById(R.id.rg_main);
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return list.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

    }

}

MyFragment.java

package com.qf.day21_viewpagerfragmentrg_demo4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;



public class MyFragment extends ListFragment {

    private  TextView tvShow;

    private int index =0;


    public static MyFragment getInstance(int index){
        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        myFragment.setArguments(args);
        return myFragment;

    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);

        Bundle bundle = getArguments();
        if(bundle!=null){
            index = bundle.getInt("index");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        tvShow = (TextView) v.findViewById(R.id.tv_show);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        switch (index) {
        case 1:
            tvShow.setText("您点击了书签1");

            break;
        case 2:
            tvShow.setText("您点击了书签2");
            break;
        case 3:
            tvShow.setText("您点击了书签3");
            break;
        case 4:
            tvShow.setText("您点击了书签4");
            break;
        default:
            break;
        }

        SimpleAdapter adapter = new SimpleAdapter(
                getActivity(), 
                loadNetWorkData(), 
                R.layout.item, 
                new String[]{"icon","title","content"}, 
                new int[]{R.id.iv_item,R.id.title_item,R.id.content_item});

        setListAdapter(adapter);

    }

    /**
     * 假设从网络获取数据
     * @return
     */
    private List<Map<String,Object>> loadNetWorkData(){

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for(int i=0;i<20;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("icon", R.drawable.ic_launcher);
            map.put("title", "郭XX大战曹XXX"+i+"tab"+index);
            map.put("content", "降龙十八掌赢"+i+"tab"+index);
            list.add(map);

        }

        return list;

    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
    }

}

selector_main.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@android:drawable/ic_menu_add"></item>
    <item android:state_checked="false" android:drawable="@android:drawable/ic_menu_day"></item>

</selector>

activity_main.xml

<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=".MainActivity" >

    <RadioGroup
        android:id="@+id/rg_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         >

    </RadioGroup>
    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f00"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v4.view.ViewPager>


</LinearLayout>

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#f00"
        android:text="AAA"
        />
    <ListView
        android:id="@android:id/list" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>


</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <ImageView 
        android:id="@+id/iv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    <TextView 
        android:id="@+id/title_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_item"
        android:text="name"
        />
    <TextView 
        android:id="@+id/content_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_item"
        android:text="aaa"
        android:layout_alignBottom="@id/iv_item"
        />

</RelativeLayout>