整体布局思路如图:
1、首先创建一个Activity并且继承FragmentActivity,布局文件中留下一个Framelayout
初始化事务,添加Fragment,提交事务
MainActivity:
package com.zhansy.mytext;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
//FragmentTransaction对fragment进行添加,移除,替换,以及执行其他动作
private FragmentTransaction fragmentTransaction;
private Fragment myFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (myFragment == null) {
myFragment = new MyFragment();
fragmentTransaction.add(R.id.fl,myFragment);
}else {
fragmentTransaction.show(myFragment);
}
//最后要提交事务
fragmentTransaction.commit();
}
}
2、在Activity添加的Fragment中有两个子Fragment
package com.zhansy.mytext;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015/8/10.
*/
public class MyFragment extends Fragment implements ViewPager.OnPageChangeListener{
private FragmentTabPager pageadapter;
List<Fragment> fragments;
ViewPager viewPager;
TextView mone,mtwo;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.myfragment, container, false);
fragments = new ArrayList<Fragment>();
//添加两个碎片
fragments.add(new OneFragment());
fragments.add(new TwoFragment());
pageadapter = new FragmentTabPager(((MainActivity)getActivity()).getSupportFragmentManager());
pageadapter.setmFragments((ArrayList<Fragment>) fragments);
//这里花了好长时间,用getActivity()找不到它的ID的,必须用定义的view.findViewById
viewPager = (ViewPager) view.findViewById(R.id.view_page);
mone = (TextView) view.findViewById(R.id.mOne);
mtwo = (TextView) view.findViewById(R.id.mTwo);
viewPager.setAdapter(pageadapter);
viewPager.setOnPageChangeListener(this);
mone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(0);
}
});
mtwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(1);
}
});
return view;
}
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
}
分别为OneFragment和TwoFragment
package com.zhansy.mytext;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2015/8/10.
*/
public class OneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return LayoutInflater.from(getActivity()).inflate(R.layout.one, container, false);
}
}
package com.zhansy.mytext;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2015/8/10.
*/
public class TwoFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return LayoutInflater.from(getActivity()).inflate(R.layout.two, container, false);
}
}
最后肯定是一个适配器并且继承 FragmentStatePagerAdapter
package com.zhansy.mytext;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
/**
* Created by Administrator on 2015/8/10.
*/
public class FragmentTabPager extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mFragments =new ArrayList<Fragment>();
public ArrayList<Fragment> getmFragments() {
return mFragments;
}
public void setmFragments(ArrayList<Fragment> mFragments) {
this.mFragments = mFragments;
}
public FragmentTabPager(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
if(mFragments!=null&&mFragments.size()>0){
return mFragments.get(i);
}else{
return null;
}
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
布局文件随便一点都可以,如下:
activity_main.xml:
<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=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl" />
</RelativeLayout>
myfragment.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"
>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/my_one"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:tag="0">
<TextView
android:id="@+id/mOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="第一个"
android:textSize="14sp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/my_two"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:tag="1">
<TextView
android:id="@+id/mTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="第二个"
android:textSize="14sp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="2dp"
android:layout_marginTop="5dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_below="@id/ll">
<View
android:layout_width="fill_parent"
android:layout_height="1px"
/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rl" />
</RelativeLayout>
one.xml和two.xml任意布局都可以,就不上图了