以下這幾篇寫很仔細了
Android 使用 Fragment 建立 Tab 取代 TabActivity
給TabHost加上ViewPager滑動效果_Android
package com.example.user.tabexample;
import java.util.ArrayList;
import java.util.List;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
public class MainActivity extends TabActivity {
//頁卡內容
private ViewPager mPager;
// Tab頁面列表
private List<View> listViews;
// 當前頁卡編號
private LocalActivityManager manager = null;
private final Context context = MainActivity.this;
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator(
"正在聽").setContent(
new Intent(this, AppleFragment.class)));
mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator(
"本地聽").setContent(
new Intent(this, FacebookFragment.class)));
/* mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator(
"網絡聽").setContent(
new Intent(this, CActivity.class)));*/
mTabHost.setCurrentTab(0);
//tabhost改變同樣改變ViewPager的內容
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
mPager.setCurrentItem(Integer.parseInt(tabId));
}
});
manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);
InitViewPager();
}
private void InitViewPager() {
mPager = (ViewPager) findViewById(R.id.vPager);
listViews = new ArrayList<View>();
MyPagerAdapter mpAdapter = new MyPagerAdapter(listViews);
Intent intent = new Intent(context, AppleFragment.class);
listViews.add(getView("A", intent));
Intent intent2 = new Intent(context, FacebookFragment.class);
listViews.add(getView("B", intent2));
/*Intent intent3 = new Intent(context, CActivity.class);
listViews.add(getView("C", intent3));*/
mPager.setAdapter(mpAdapter);
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;
public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
mTabHost.setCurrentTab(0);
break;
case 1:
mTabHost.setCurrentTab(1);
break;
/* case 2:
mTabHost.setCurrentTab(2);
break;*/
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
private View getView(String id,Intent intent)
{
return manager.startActivity(id, intent).getDecorView();
}
}
====================================
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class AppleFragment extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple_fragment);
TextView txtResult = (TextView) findViewById(R.id.textView1);
txtResult.setText(value);
}
private String value = "";
}
========activity_main.xml=====
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<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:background="#e4eecd"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
</FrameLayout>
</LinearLayout>
</TabHost>
=======activity_apple_fragment.xml========
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.tabexample.AppleFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="239dp"
android:layout_marginTop="35dp" />
</RelativeLayout>
