标签:root com 技术 mode item mat oncreate ref match
首先我们先看一下效果图
-------------------------------->>>>>>>-----------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
效果图有,代码的原理:
1.布局文件xml TabLayout 和 ViewPage 两个控件
2.写四个Frament 用 support.v4.app.Frament
3.写一个适配器 用于ViewPage 现实滑动 继承 FragmentPagerAdapter 实现三个方法 《1》getPageTitle(int position) 《2》getItem(int position) 《3》getCount()
4.在activity 里联合前面三步骤,ok
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面上传代码 xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/lay_root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="song.com.cn.tabactivityviewpage.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tab_tl" android:layout_width="match_parent" android:layout_height="38dp" android:background="@color/mall_theme_bg" app:tabIndicatorColor="@color/colorAccent" app:tabIndicatorHeight="2dp" app:tabMode="fixed" app:tabSelectedTextColor="@color/colorAccent" app:tabTextColor="@color/colorPrimary" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
这里只上传一个 frament
package song.com.cn.tabactivityviewpage; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @author lixiangsong * @date 创建时间 : 2017/8/4 * @Description: */ public class OneFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_one, container, false); Log.d("TwoFragment", "-------------OneFragment------"); return view; } }
下面是 Adapter
public class PagerAdapter extends FragmentPagerAdapter { private PagerAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return tab[position]; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }
源码下载路径 http://download.csdn.net/detail/qq_33495943/9921605
还有一个文章,也是tab切换 放在底部切换
链接 http://blog.csdn.net/qq_33495943/article/details/73228369
tab头部滑动切换,TabLayout和ViewPager实现
标签:root com 技术 mode item mat oncreate ref match
原文地址:http://www.cnblogs.com/xiaoliu500/p/7364555.html