布局
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <com.astuetz.PagerSlidingTabStrip 7 android:id="@+id/mTabStrip" 8 android:layout_width="match_parent" 9 android:layout_height="50dp" 10 android:layout_alignParentTop="true" > 11 12 </com.astuetz.PagerSlidingTabStrip> 13 14 <android.support.v4.view.ViewPager 15 android:id="@+id/mViewPager" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:layout_below="@+id/mTabStrip" > 19 </android.support.v4.view.ViewPager> 20 21 </RelativeLayout>
实现代码
1 import java.util.ArrayList; 2 import java.util.List; 3 4 import com.astuetz.PagerSlidingTabStrip; 5 import com.wangzhen.adapter.MyPagerAdapter; 6 7 import android.os.Bundle; 8 import android.support.v4.app.Fragment; 9 import android.support.v4.view.ViewPager; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.view.ViewGroup; 13 14 public class FragmentConversation extends Fragment { 15 16 private PagerSlidingTabStrip mTabStrip; 17 private ViewPager mViewPager; 18 19 private MyPagerAdapter mAdapter; 20 21 public View onCreateView(LayoutInflater inflater, ViewGroup container, 22 Bundle savedInstanceState) { 23 View view = inflater.inflate(R.layout.fragment_conversation, container, 24 false); 25 mTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.mTabStrip); 26 mViewPager = (ViewPager) view.findViewById(R.id.mViewPager); 27 return view; 28 }; 29 30 @Override 31 public void onActivityCreated(Bundle savedInstanceState) { 32 super.onActivityCreated(savedInstanceState); 33 34 List<Fragment> list = new ArrayList<Fragment>(); 35 list.add(new FragmentContact()); 36 list.add(new FragmentPlugin()); 37 list.add(new FragmentContact()); 38 list.add(new FragmentContact()); 39 list.add(new FragmentPlugin()); 40 list.add(new FragmentContact()); 41 list.add(new FragmentContact()); 42 list.add(new FragmentPlugin()); 43 list.add(new FragmentContact()); 44 45 mAdapter = new MyPagerAdapter(getFragmentManager(), list); 46 mViewPager.setAdapter(mAdapter); 47 48 mTabStrip.setViewPager(mViewPager); 49 } 50 }
1 import java.util.ArrayList; 2 import java.util.List; 3 4 import android.support.v4.app.Fragment; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentPagerAdapter; 7 8 public class MyPagerAdapter extends FragmentPagerAdapter { 9 10 private List<String> mTitles; 11 private List<Fragment> mDatas; 12 13 public MyPagerAdapter(FragmentManager fm, List<Fragment> data) { 14 super(fm); 15 mDatas = data; 16 mTitles = new ArrayList<String>(); 17 for (int i = 0; i < mDatas.size(); i++) { 18 mTitles.add("Tab " + (i + 1)); 19 } 20 } 21 22 @Override 23 public CharSequence getPageTitle(int position) { 24 return mTitles.get(position); 25 } 26 27 @Override 28 public Fragment getItem(int position) { 29 return mDatas.get(position); 30 } 31 32 @Override 33 public int getCount() { 34 return mDatas.size(); 35 } 36 37 }
时间: 2024-10-05 21:41:13