TabLayout+ViewPager 实现页签滑动的效果


在2015年的google大会上,google发布了新的Android Support Design,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成TabPageIndicator的效果,最好的是它可以兼容到2.2以上版本,包括2.2。下面我就举一个简单的例子来使用它。

       只要在build.gradle中加入compile 'com.android.support:design:22.2.0'即可。


 fragment_find.xml



 这里面没有什么特别的,就是添加了一个TabLayout和Viewpager作为上下的布局。其中


 Find_tab_Adapter.Java  它是viewpager的Adapter,因为这里面我每个栏目下,都会有一些列表,所以采用list<View>的方式,在里面切换layout不太适合,所以我采用了List<Fragment>来直接加载多个fragment


FindFragment.java这个的说法,全在标注里面了



  1. package com.example.cg.myzhihu;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.support.design.widget.TabLayout;  
  6. import android.support.v4.app.Fragment;  
  7. import android.support.v4.app.FragmentPagerAdapter;  
  8. import android.support.v4.view.ViewPager;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12.   
  13. import com.example.cg.myzhihu.Adapters.Find_tab_Adapter;  
  14.   
  15. import java.util.ArrayList;  
  16. import java.util.List;  
  17.   
  18.   
  19. /** 
  20.  * 发现页面 
  21.  */  
  22. public class FindFragment extends Fragment {  
  23.   
  24.     private TabLayout tab_FindFragment_title;                            //定义TabLayout  
  25.     private ViewPager vp_FindFragment_pager;                             //定义viewPager  
  26.     private FragmentPagerAdapter fAdapter;                               //定义adapter  
  27.   
  28.     private List<Fragment> list_fragment;                                //定义要装fragment的列表  
  29.     private List<String> list_title;                                     //tab名称列表  
  30.   
  31.     private Find_hotRecommendFragment hotRecommendFragment;              //热门推荐fragment  
  32.     private Find_hotCollectionFragment hotCollectionFragment;            //热门收藏fragment  
  33.     private Find_hotMonthFragment hotMonthFragment;                      //本月热榜fragment  
  34.     private Find_hotToday hotToday;                                      //今日热榜fragment  
  35.   
  36.     @Override  
  37.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  38.                              Bundle savedInstanceState) {  
  39.         View view = inflater.inflate(R.layout.fragment_find, container, false);  
  40.   
  41.         initControls(view);  
  42.   
  43.         return view;  
  44.     }  
  45.   
  46.     /** 
  47.      * 初始化各控件 
  48.      * @param view 
  49.      */  
  50.     private void initControls(View view) {  
  51.   
  52.         tab_FindFragment_title = (TabLayout)view.findViewById(R.id.tab_FindFragment_title);  
  53.         vp_FindFragment_pager = (ViewPager)view.findViewById(R.id.vp_FindFragment_pager);  
  54.   
  55.         //初始化各fragment  
  56.         hotRecommendFragment = new Find_hotRecommendFragment();  
  57.         hotCollectionFragment = new Find_hotCollectionFragment();  
  58.         hotMonthFragment = new Find_hotMonthFragment();  
  59.         hotToday = new Find_hotToday();  
  60.   
  61.         //将fragment装进列表中  
  62.         list_fragment = new ArrayList<>();  
  63.         list_fragment.add(hotRecommendFragment);  
  64.         list_fragment.add(hotCollectionFragment);  
  65.         list_fragment.add(hotMonthFragment);  
  66.         list_fragment.add(hotToday);  
  67.   
  68.         //将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用  
  69.         list_title = new ArrayList<>();  
  70.         list_title.add("热门推荐");  
  71.         list_title.add("热门收藏");  
  72.         list_title.add("本月热榜");  
  73.         list_title.add("今日热榜");  
  74.   
  75.         //设置TabLayout的模式  
  76.         tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);  
  77.         //为TabLayout添加tab名称  
  78.         tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));  
  79.         tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));  
  80.         tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(2)));  
  81.         tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(3)));  
  82.   
  83.         fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager(),list_fragment,list_title);  
  84.   
  85.         //viewpager加载adapter  
  86.         vp_FindFragment_pager.setAdapter(fAdapter);  
  87.         //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);  
  88.         //TabLayout加载viewpager  
  89.         tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);  
  90.         //tab_FindFragment_title.set  
  91.     }  
  92.   
  93.   
  94. }  
  95.   
  96. 效果图,不太会做成动态的。   








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值