先看下布局文件
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" >
<TabHost
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>//这个地方一定要设置为gone
</TabHost>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
下边是每页布局文件 view_content.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" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"/>
</LinearLayout>
下边是应用的主要实现文件
package com.wind.imagescroll;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{
private View view_content;
private ViewPager mViewPager;
//private PagerTabStrip mPagerTabStrip;
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewPager = (ViewPager)this.findViewById(R.id.pager);
mTabHost = (TabHost)this.findViewById(R.id.tab_title);
//mPagerTabStrip = (PagerTabStrip)this.findViewById(R.id.pagertab);
//mPagerTabStrip.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
//mPagerTabStrip.setTabIndicatorColor(getResources().getColor(android.R.color.holo_blue_light));
initialize();
}
private void initialize() {
int[] drawable = new int[]{R.drawable.a1, R.drawable.a2, R.drawable.a3};
String[] titles = {"tab1", "tab2", "tab3"};
List<View> viewPages = new ArrayList<View>();
List<String> tab_titles = new ArrayList<String>();
mTabHost.setup();
for (int i = 0; i < drawable.length; i++) {
//这个地方是每次inflate解析布局文件生成一个新的页面视图
view_content = inflate(R.layout.view_content,null);
ImageView imageView = (ImageView)view_content.findViewById(R.id.image);
imageView.setImageResource(drawable[i]);
viewPages.add(view_content);
tab_titles.add(titles[i]);
TabSpec tabSpec = mTabHost.newTabSpec(titles[i]);
//把tabSpec内容设置为隐藏的tabcontent
tabSpec.setContent(android.R.id.tabcontent);
tabSpec.setIndicator(titles[i]);
mTabHost.addTab(tabSpec);
}
MyPageAdapter adapter = new MyPageAdapter(viewPages,tab_titles);
mViewPager.setAdapter(adapter);
mViewPager.setOnPageChangeListener(this);
mTabHost.setOnTabChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyPageAdapter extends PagerAdapter {
private List<View> mViewPages;
private List<String> mTabTitles;
public MyPageAdapter(List<View> viewPages, List<String> tab_titles) {
mViewPages = new ArrayList<View>(viewPages);
mTabTitles = new ArrayList<String>(tab_titles);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView(mViewPages.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager)container).addView(mViewPages.get(position),0);
return mViewPages.get(position);
}
@Override
public int getCount() {
return mViewPages.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return mTabTitles.get(position);
}
}
private View inflate(int viewId, ViewGroup viewGroup) {
return getLayoutInflater().inflate(viewId, viewGroup, false);
}
@Override
public void onTabChanged(String tabId) {
int currentIndex = -1;
if (mViewPager != null && mTabHost != null) {
currentIndex = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(currentIndex);
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int currentIndex = -1;
if (mViewPager != null && mTabHost != null) {
currentIndex = mViewPager.getCurrentItem();
mTabHost.setCurrentTab(currentIndex);
}
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
}
TabHost+ViewPager做能左右滑动的TabHost,布布扣,bubuko.com
TabHost+ViewPager做能左右滑动的TabHost
原文:http://blog.csdn.net/u012554768/article/details/20060499