布局
<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" > <com.astuetz.PagerSlidingTabStrip
android:id="@+id/mTabStrip"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true" > </com.astuetz.PagerSlidingTabStrip> <android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/mTabStrip" >
</android.support.v4.view.ViewPager> </RelativeLayout>
实现代码
import java.util.ArrayList;
import java.util.List; import com.astuetz.PagerSlidingTabStrip;
import com.wangzhen.adapter.MyPagerAdapter; 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; public class FragmentConversation extends Fragment { private PagerSlidingTabStrip mTabStrip;
private ViewPager mViewPager; private MyPagerAdapter mAdapter; public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conversation, container,
false);
mTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.mTabStrip);
mViewPager = (ViewPager) view.findViewById(R.id.mViewPager);
return view;
}; @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); List<Fragment> list = new ArrayList<Fragment>();
list.add(new FragmentContact());
list.add(new FragmentPlugin());
list.add(new FragmentContact());
list.add(new FragmentContact());
list.add(new FragmentPlugin());
list.add(new FragmentContact());
list.add(new FragmentContact());
list.add(new FragmentPlugin());
list.add(new FragmentContact()); mAdapter = new MyPagerAdapter(getFragmentManager(), list);
mViewPager.setAdapter(mAdapter); mTabStrip.setViewPager(mViewPager);
}
}
import java.util.ArrayList;
import java.util.List; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class MyPagerAdapter extends FragmentPagerAdapter { private List<String> mTitles;
private List<Fragment> mDatas; public MyPagerAdapter(FragmentManager fm, List<Fragment> data) {
super(fm);
mDatas = data;
mTitles = new ArrayList<String>();
for (int i = 0; i < mDatas.size(); i++) {
mTitles.add("Tab " + (i + 1));
}
} @Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
} @Override
public Fragment getItem(int position) {
return mDatas.get(position);
} @Override
public int getCount() {
return mDatas.size();
} }