MainActivity的代码如下:
package com.saxon.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import com.saxon.viewpagerdemo.fragment.CategoryFragment;
import com.saxon.viewpagerdemo.fragment.FindFragment;
import com.saxon.viewpagerdemo.fragment.HomeFragment;
import com.saxon.viewpagerdemo.fragment.MineFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
private LinearLayout[] linearlayouts;
private int count;
private ViewPager vp_main;
private LinearLayout linearlayout_main;
private List<Fragment> list = new ArrayList<Fragment>();
private MyFragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initViewPager();
initLinearLayout();
}
private void initLinearLayout() {
count = linearlayout_main.getChildCount();
linearlayouts = new LinearLayout[count];
for (int i = 0; i < count; i++) {
LinearLayout linearLayout = (LinearLayout) linearlayout_main
.getChildAt(i);
linearlayouts[i] = linearLayout;
linearlayouts[i].setEnabled(true);
linearlayouts[i].setSelected(false);
linearlayouts[i].setTag(i);
linearlayouts[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
vp_main.setCurrentItem((Integer) arg0.getTag());
}
});
}
linearlayouts[0].setEnabled(false);
linearlayouts[0].setSelected(true);
}
private void initViewPager() {
list.add(new HomeFragment());
list.add(new CategoryFragment());
list.add(new FindFragment());
list.add(new MineFragment());
adapter = new MyFragmentAdapter(getSupportFragmentManager());
vp_main.setAdapter(adapter);
vp_main.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < count; i++) {
linearlayouts[i].setEnabled(true);
linearlayouts[i].setSelected(false);
}
linearlayouts[arg0].setEnabled(false);
linearlayouts[arg0].setSelected(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
}
});
}
private void initView() {
vp_main = (ViewPager) findViewById(R.id.vp_main);
linearlayout_main = (LinearLayout) findViewById(R.id.linearlayout_main);
}
class MyFragmentAdapter extends FragmentPagerAdapter{
private FragmentManager manager;
private FragmentTransaction fragmentTransaction;
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
this.manager = fm;
}
@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();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
super.instantiateItem(container, position);
Fragment fragment = list.get(position);
manager.beginTransaction().show(fragment).commit();
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.hide(list.get(position)).commit();
}
}
}
布局文件activity_main.xmlx的代码如下:
<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" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#bebebe"/>
<LinearLayout
android:id="@+id/linearlayout_main"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/ll_home"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/tab_home_selector"
/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/tab_text_selector"
android:text="首页"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_cate"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/tab_category_selector"
/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/tab_text_selector"
android:text="分类"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_find"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/tab_find_selector"
/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/tab_text_selector"
android:text="发现"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_mine"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/tab_mine_selector"
/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/tab_text_selector"
android:text="我"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
tab_text_selector.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#46BF1A" android:state_selected="true"/>
<item android:color="#ABABAB"/>
</selector>
tab_home_selector.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/menu_home_first_on" android:state_selected="true"></item>
<item android:drawable="@drawable/menu_home_first_off"></item>
</selector>
同理你可以分别写出 tab_categroy_selector.xml;tab_find_selector.xml;tab_mine_selector.xml;
Fragmemt的布局文件你可以随便写写就行了,希望对您有所帮助;
效果图如下: