主界面=====================================================
public class MainActivity extends ActionBarActivity {
private ViewPager mVp;
private TextView tv1, tv2, tv3;
private View v1, v2, v3;
// 定义装fragment的集合
private List<Fragment> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件id
mVp = (ViewPager) findViewById(R.id.vp);
v1 = findViewById(R.id.view1);
v2 = findViewById(R.id.view2);
v3 = findViewById(R.id.view3);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
// 初始化集合
list = new ArrayList<Fragment>();
// 向集合中加入fragment
list.add(new FragmentA());
list.add(new FragmentB());
list.add(new FragmentC());
mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list != null ? list.size() : 0;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
});
mVp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
if (arg0 == 0) {
v1.setVisibility(View.INVISIBLE);
v2.setVisibility(View.GONE);
v3.setVisibility(View.GONE);
}
if (arg0 == 1) {
v1.setVisibility(View.GONE);
v2.setVisibility(View.INVISIBLE);
v3.setVisibility(View.GONE);
}
if (arg0 == 2) {
v1.setVisibility(View.GONE);
v2.setVisibility(View.GONE);
v3.setVisibility(View.INVISIBLE);
}
}
@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
}
});
// 设置点击选项,mvp切换游标切换
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mVp.setCurrentItem(0);
v1.setVisibility(View.VISIBLE);
v2.setVisibility(View.GONE);
v3.setVisibility(View.GONE);
}
});
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mVp.setCurrentItem(1);
v2.setVisibility(View.VISIBLE);
v1.setVisibility(View.GONE);
v3.setVisibility(View.GONE);
}
});
tv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mVp.setCurrentItem(2);
v3.setVisibility(View.VISIBLE);
v2.setVisibility(View.GONE);
v1.setVisibility(View.GONE);
}
});
}
}
主界面布局============================================================
<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="com.bawie.test.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="46dp"
android:gravity="center"
android:text="本社介绍" />
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/red"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="46dp"
android:gravity="center"
android:text="履行职责" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/red"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="46dp"
android:gravity="center"
android:text="自我建设" />
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/red"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
color颜色背景可自定义