您的位置:首页 > 其它

TabLayout

2016-09-08 20:15 162 查看
mainActivity.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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--TabLayout support.design包下  使用TabLayout设置的属性时 类似与使用自定义属性
tabIndicatorColor 设置指示器的颜色
tabIndicatorHeight 设置指示器的高度
tabMode 设置tabLayout的布局模式  fixed 固定不可滚动 scrollable可滚动
tabSelectedTextColor 设置导航中选中文本的颜色-->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:scrollbars="none"
app:tabIndicatorColor="#00aa00"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@android:color/holo_red_light"
>

</android.support.design.widget.TabLayout>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.view.ViewPager>
</LinearLayout>
主函数
package com.yztc.tablayoutdemo;import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {private TabLayout mTabLayout;private String[] titles={"热点","健康","财经","教育","商业","股票","电影","游戏"};private ViewPager vp;private List<Fragment> fragments;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTabLayout= (TabLayout) findViewById(R.id.tabLayout);vp= (ViewPager) findViewById(R.id.vp);//根据当前获取的标题添加到tab中for(int i=0;i<titles.length;i++){//            TabLayout.Tab  tab=mTabLayout.newTab();//            tab.setText(titles[i]);//            mTabLayout.addTab(tab);mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));}initData();// 添加viewpager的监听 参数传递tabLayout中提供的监听事件  viewpager中的page页与tab对应vp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));//设置tabLayout的监听事件  滑动改变tabLayout中的tab时viewpager跟随变化mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {@Overridepublic void onTabSelected(TabLayout.Tab tab) {vp.setCurrentItem(tab.getPosition());}@Overridepublic void onTabUnselected(TabLayout.Tab tab) {}@Overridepublic void onTabReselected(TabLayout.Tab tab) {}});}//初始化适配viewpager的数据public void initData(){fragments=new ArrayList<>();PagerFragment f1=new PagerFragment();Bundle bundle=new Bundle();bundle.putInt("index",1);f1.setArguments(bundle);PagerFragment f2=new PagerFragment();bundle=new Bundle();bundle.putInt("index",2);f2.setArguments(bundle);PagerFragment f3=new PagerFragment();bundle=new Bundle();bundle.putInt("index",3);f3.setArguments(bundle);fragments.add(f1);fragments.add(f2);fragments.add(f3);adapter=new MyAdapter(getSupportFragmentManager(),fragments);vp.setAdapter(adapter);}}适配器
package com.yztc.tablayoutdemo;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/*** Created by sqq on 16/9/8.*/public class MyAdapter extends FragmentPagerAdapter{private List<Fragment> list;public MyAdapter(FragmentManager fm,List<Fragment> list) {super(fm);this.list=list;}@Overridepublic Fragment getItem(int position) {return list.get(position);}@Overridepublic int getCount() {return list.size();}}
fragment
package com.yztc.tablayoutdemo;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/*** Created by sqq on 16/9/8.*/public class PagerFragment extends Fragment{@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view=inflater.inflate(R.layout.page_fragment,null);TextView tv= (TextView) view.findViewById(R.id.tv);Bundle bundle=getArguments();if(bundle!=null){int index=bundle.getInt("index");switch (index){case 1:tv.setText("热点");break;case 2:tv.setText("健康");break;case 3:tv.setText("财经");break;}}return view;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TabLayout 导航栏