一.首先是布局文件:
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
<RadioGroup
android:id="@+id/tab_radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio1"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:checked="true"
android:drawableTop="@drawable/ic_launcher"
android:text="页面1" />
<RadioButton
android:id="@+id/radio2"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面2" />
<RadioButton
android:id="@+id/radio3"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面3" />
<RadioButton
android:id="@+id/radio4"
style="@style/tab_button_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/ic_launcher"
android:text="页面4" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
子页面布局文件(数量根据需求来定);
tab1
<?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"
android:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面一"
android:textSize="72dp"/>
tab2
<?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"
android:gravity="center"
android:background="@color/ablue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面二"
android:textSize="72dp"/>
</LinearLayout>
二、代码文件:
public class MainActivity extends Activity {
// 页卡内容
private ViewPager mPager;
// Tab页面列表
private List<View> listViews;
private Intent tab1Intent;
private Intent tab2Intent;
private Intent tab3Intent;
private Intent tab4Intent;
LocalActivityManager groupActivity;
RadioButton radio1, radio2, radio3, radio4;
RadioButton[] radios;
RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
groupActivity = new LocalActivityManager(this, true);
groupActivity.dispatchCreate(savedInstanceState);
mPager = (ViewPager) findViewById(R.id.vPager);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radiogroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
RadioButton[] radioButtons = { radio1, radio2, radio3, radio4 };
radios = radioButtons;
tab1Intent = new Intent(this, Tab1Activity.class);
tab2Intent = new Intent(this, Tab2Activity.class);
tab3Intent = new Intent(this, Tab3Activity.class);
tab4Intent = new Intent(this, Tab4Activity.class);
listViews = new ArrayList<View>();
listViews.add(getView("1", tab1Intent));
listViews.add(getView("2", tab2Intent));
listViews.add(getView("3", tab3Intent));
listViews.add(getView("4", tab4Intent));
//获取子页面上的控件
//View v1=listViews.get(0);
//v1.findViewById(R.id.**);
MyPagerAdapter mpAdapter = new MyPagerAdapter(listViews);
mPager.setAdapter(mpAdapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
radio1.setChecked(true);
radiochange(radios);
break;
case 1:
radio2.setChecked(true);
radiochange(radios);
break;
case 2:
radio3.setChecked(true);
radiochange(radios);
break;
case 3:
radio4.setChecked(true);
radiochange(radios);
break;
}
}
@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
}
});
radiochange(radios);
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch (arg1) {
case R.id.radio1:
radiochange(radios);
mPager.setCurrentItem(0);
break;
case R.id.radio2:
radiochange(radios);
mPager.setCurrentItem(1);
break;
case R.id.radio3:
radiochange(radios);
mPager.setCurrentItem(2);
break;
case R.id.radio4:
radiochange(radios);
mPager.setCurrentItem(3);
break;
}
}
});
}
private View getView(String id, Intent intent) {
return groupActivity.startActivity(id, intent).getDecorView();
}
public void radiochange(RadioButton[] radioButtons) {
for (RadioButton radioButton : radioButtons) {
if (radioButton.isChecked()) {
radioButton
.setTextColor(getResources().getColor(R.color.ablue));
} else {
radioButton.setTextColor(Color.BLACK);
}
}
}
}
//子窗体代码(根据需求数量而定)
public class Tab1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
}
}