PagerTabStrip简单使用方式2

效果图示例:

 

 

PagerTabStrip简单使用方式2_第1张图片

 

 

PagerTabStrip简单使用方式2_第2张图片

 

 

 


1、res/layout下只有一个activity_main.xml布局

 


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"
    tools:context="${relativePackage}.${activityClass}" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pagerTabStrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.view.ViewPager>

</RelativeLayout>

 

 

=============================

 

 

2、源文件有2个一个MainActivity.java一个PagerFragment.java

 


PagerFragment.java类


代码

 

public class PagerFragment extends Fragment {

 private int tabIndex;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  tabIndex = getArguments().getInt("tabIndex");
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater,
   @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  TextView text = new TextView(getActivity());
  switch(tabIndex){
  case 0:
   text.setText("记录");
   break;
  case 1:
   text.setText("联系人");
   break;
  case 2:
   text.setText("收藏夹");
   break;
  case 3:
   text.setText("群组");
   break;
  }
  return text;
 }
}

 

 

 

=====================

 


3、MainActivity类


代码

 

public class MainActivity extends FragmentActivity {

 private ViewPager viewPager;
 private PagerTabStrip pagerTabStrip;
 private List<Fragment> list;
 
 private String[] titels = {"记录","联系人","收藏夹","群组"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.viewPager = (ViewPager) this.findViewById(R.id.viewPager);
  this.pagerTabStrip = (PagerTabStrip) this.findViewById(R.id.pagerTabStrip);
  
  //设置PagerTabStrip样式
  pagerTabStrip.setTextColor(Color.WHITE);//字体颜色
  pagerTabStrip.setBackgroundColor(Color.GRAY);//背景颜色
  pagerTabStrip.setDrawFullUnderline(true);//下划线
  pagerTabStrip.setTabIndicatorColor(Color.CYAN);//下划线颜色
  pagerTabStrip.setTextSpacing(30);//每个tab间隔
  
  list = new ArrayList<Fragment>();
  for(int i = 0;i<titels.length;i++){
   PagerFragment fragment = new PagerFragment();
   Bundle bundle = new Bundle();
   bundle.putInt("tabIndex", i);
   fragment.setArguments(bundle);
   list.add(fragment);
  }
  viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),list));
 }
 
 class MyFragmentPagerAdapter extends FragmentPagerAdapter{

  private List<Fragment> list;
  public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
   super(fm);
   // TODO Auto-generated constructor stub
   this.list = list;
  }

  @Override
  public Fragment getItem(int position) {
   // TODO Auto-generated method stub
   return this.list.get(position);
  }

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return this.list.size();
  }
  @Override//设置tab标题 并且和ViewPager关联
  public CharSequence getPageTitle(int position) {
   // TODO Auto-generated method stub
   return titels[position];
  }
  
 }
}


 

你可能感兴趣的:(PagerTabStrip)