刚开始做项目,有一个页面上面是三个标签, 下面三个页面可以左右滑动,刚开始挺抓狂的,怎么让它们标签与页面关联起来呢?我想到了TabHost和ViewPager结合,使用TabHost切换Activity,使用ViewPager切换界面,废话少说,直接看代码吧。
先看看布局文件:
<?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" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/GoldEnrod" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:background="@drawable/albums_icon_bg"
android:ems="10"
android:hint="请输入歌名:" >
<requestFocus />
</EditText>
</RelativeLayout>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" >
</FrameLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager_tab"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</TabHost>
</LinearLayout>
项目中页面文件:
import java.util.ArrayList;
import java.util.List;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
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.Log;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.example.thjyyy.R;
public class Tab_changpin_Activity extends TabActivity
{
private ViewPager mViewPager;
private TabHost mTabHost;
// Tab页面列表
private List<View> listViews;
// 当前页卡编号
private int currIndex = 0;
// 页卡内容
private LocalActivityManager manager = null;
// 动画图片偏移量
private int offset = 0;
// 动画图片宽度
private int bmpW;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_tab_changpin);
View layout = getLayoutInflater().inflate(
R.layout.activity_tab_changpin, null);
mTabHost = getTabHost();
mViewPager = (ViewPager) findViewById(R.id.pager_tab);
View view1 = getLayoutInflater().inflate(R.layout.tabmusic_item, null);
TextView tv_tabitem1 = (TextView) view1
.findViewById(R.id.tv_tabmusic_item);
tv_tabitem1.setText("单曲");
View view2 = getLayoutInflater().inflate(R.layout.tabmusic_item, null);
TextView tv_tabitem2 = (TextView) view2
.findViewById(R.id.tv_tabmusic_item);
tv_tabitem2.setText("歌手");
View view3 = getLayoutInflater().inflate(R.layout.tabmusic_item, null);
TextView tv_tabitem3 = (TextView) view3
.findViewById(R.id.tv_tabmusic_item);
tv_tabitem1.setText("专辑");
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(view1)
.setContent(new Intent(this, SigleSongActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(view2)
.setContent(new Intent(this, TestActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(view3)
.setContent(new Intent(this,PersonListActivity.class)));
mTabHost.setCurrentTab(0);
manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);
//设置tab的点击事件
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
if("tab1".equals(tabId))
{
mViewPager.setCurrentItem(0);
}
if("tab2".equals(tabId))
{
mViewPager.setCurrentItem(1);
}
if("tab3".equals(tabId))
{
mViewPager.setCurrentItem(2);
}
}
});
InitViewPager();
}
private void InitViewPager()
{
/**
* 初始化ViewPager
*/
listViews = new ArrayList<View>();
MyPagerAdapter mpAdapter = new MyPagerAdapter(listViews);
Intent intent = new Intent(this, SigleSongActivity.class);
listViews.add(getView("tab1", intent));
Intent intent2 = new Intent(this, TestActivity.class);
listViews.add(getView("tab2", intent2));
Intent intent3 = new Intent(this, PersonListActivity.class);
listViews.add(getView("tab3", intent3));
mViewPager.setAdapter(mpAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
}
/**
* ViewPager适配器
*/
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) {
}
}
}