Advertisement

ViewPager简单介绍(三) ViewPager+Fragment+TabLayout

阅读量:

前两篇文章中探讨了TabLayout和ViewPgaer+Fragment的结合使用。本文尝试将两者与...结合应用。如图所示:

如果对着这两者有什么不清楚的,可以看看这两篇,都是一些简单的使用。

TabLayout的使用

ViewPager+Fragment的简单使用

废话不多说,直接上代码。

要使用TabLayout,必须先在app的build.gradle中添加:

复制代码
    compile 'com.android.support:design:23.1.1'

在主布局中添加TabLayout和ViewPager

activity_main.xml

复制代码
 <?xml version="1.0" encoding="utf-8"?>

    
 <LinearLayout android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     xmlns:app="http://schemas.android.com/apk/res-auto"
    
     android:orientation="vertical"
    
     xmlns:android="http://schemas.android.com/apk/res/android">
    
     <android.support.design.widget.TabLayout
    
     android:id="@+id/tab"
    
     android:layout_width="fill_parent"
    
     android:layout_height="wrap_content"
    
     app:tabIndicatorColor="@color/colorPrimaryDark"
    
     app:tabSelectedTextColor="@color/colorPrimary"
    
     app:tabTextColor="@color/colorAccent" />
    
     <android.support.v4.view.ViewPager
    
     android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     android:id="@+id/vp">
    
     </android.support.v4.view.ViewPager>
    
 </LinearLayout>

创建两个Fragement类

fragment1.xml

复制代码
 <?xml version="1.0" encoding="utf-8"?>

    
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
     android:orientation="vertical" android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     android:background="@color/colorAccent">
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     android:text="fragment1"
    
     />
    
 </LinearLayout>

fragment2.xml

复制代码
 <?xml version="1.0" encoding="utf-8"?>

    
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
     android:orientation="vertical" android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     android:background="@color/colorPrimary">
    
     <TextView
    
     android:layout_width="match_parent"
    
     android:layout_height="match_parent"
    
     android:text="fragment2"
    
     />
    
  
    
 </LinearLayout>

Fragment1.class

复制代码
 public class Fragment1 extends Fragment{

    
     @Override
    
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    
                          Bundle savedInstanceState) {
    
     // TODO Auto-generated method stub
    
     View view= inflater.inflate(R.layout.fragment1, container, false);
    
     return view;
    
     }
    
 }

Fragment2.class

复制代码
 public class Fragment1 extends Fragment{

    
     @Override
    
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    
                          Bundle savedInstanceState) {
    
     // TODO Auto-generated method stub
    
     View view= inflater.inflate(R.layout.fragment2, container, false);
    
     return view;
    
     }
    
 }

创建ViewPager的适配器

FragAdapter.calss

复制代码
 public class FragAdapter extends FragmentPagerAdapter {

    
     private List<Fragment> fragmentList;
    
     private List<String> title;
    
     public FragAdapter(FragmentManager fm,List<Fragment> fragments,List<String> titles) {
    
     super(fm);
    
     fragmentList = fragments;
    
     title = titles;
    
     }
    
  
    
     @Override
    
     public Fragment getItem(int position) {
    
     return fragmentList.get(position);
    
     }
    
  
    
     @Override
    
     public int getCount() {
    
     return fragmentList.size();
    
     }
    
     @Override
    
     public CharSequence getPageTitle(int position) {
    
     return title.get(position);
    
     }
    
 }

其中getPageTitle方法是获取标题的。

MainActivity中

复制代码
 public class MainActivity extends AppCompatActivity {

    
     private TabLayout mTabLayout;
    
     //标题列表
    
     ArrayList<String> titleList= new ArrayList<String>();
    
  
    
     @Override
    
     protected void onCreate(Bundle savedInstanceState) {
    
     super.onCreate(savedInstanceState);
    
     setContentView(R.layout.activity_main);
    
     //构造适配器
    
     List<Fragment> fragments=new ArrayList<Fragment>();
    
     fragments.add(new Fragment1());
    
     fragments.add(new Fragment2());
    
     titleList.add("武侠");
    
     titleList.add("科幻");
    
     FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments,titleList);
    
  
    
     //设定适配器
    
     ViewPager vp = (ViewPager)findViewById(R.id.vp);
    
     vp.setAdapter(adapter);
    
     mTabLayout= (TabLayout) findViewById(R.id.tab);
    
     mTabLayout.setupWithViewPager(vp);
    
     }
    
 }

至此已经完成最简单的使用方法介绍。当然地,在TabLayout组件中有众多样式属性可供应用者使用和调整。此外,在TabLayout属性中并未提供直接设置下划线宽度的功能,请自行编写代码以实现这一需求

最后附上遇到的一个小问题:

ClassNotFoundException: 未识别到相关类:android.support.v7.internal.widget.TintManager

这段代码:$inderator.setupWithViewPager(vpnewInfo); 在为TabLayout设置ViewPager时出现了错误。

在引入design包的时候,版本要和appcompat版本一致。

复制代码
    'com.android.support:design:25.0.0'
    'com.android.support:appcompat-v7:25.0.0'

全部评论 (0)

还没有任何评论哟~