<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_FindFragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBlue"
app:tabIndicatorColor="@color/white"
app:tabSelectedTextColor="@color/gray"
app:tabTextColor="@color/white"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp_FindFragment_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
app:tabIndicatorColor="@color/white" // 下方滚动的下划线颜色 app:tabSelectedTextColor="@color/gray" // tab被选中后,文字的颜色 app:tabTextColor="@color/white" // tab默认的文字颜色
package com.example.cg.myzhihu.Adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by cg on 2015/9/26.
*/
public class Find_tab_Adapter extends FragmentPagerAdapter {
private List<Fragment> list_fragment; //fragment列表
private List<String> list_Title; //tab名的列表
public Find_tab_Adapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_Title) {
super(fm);
this.list_fragment = list_fragment;
this.list_Title = list_Title;
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_Title.size();
}
//此方法用来显示tab上的名字
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position % list_Title.size());
}
}
package com.example.cg.myzhihu;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cg.myzhihu.Adapters.Find_tab_Adapter;
import java.util.ArrayList;
import java.util.List;
/**
* 发现页面
*/
public class FindFragment extends Fragment {
private TabLayout tab_FindFragment_title; //定义TabLayout
private ViewPager vp_FindFragment_pager; //定义viewPager
private FragmentPagerAdapter fAdapter; //定义adapter
private List<Fragment> list_fragment; //定义要装fragment的列表
private List<String> list_title; //tab名称列表
private Find_hotRecommendFragment hotRecommendFragment; //热门推荐fragment
private Find_hotCollectionFragment hotCollectionFragment; //热门收藏fragment
private Find_hotMonthFragment hotMonthFragment; //本月热榜fragment
private Find_hotToday hotToday; //今日热榜fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find, container, false);
initControls(view);
return view;
}
/**
* 初始化各控件
* @param view
*/
private void initControls(View view) {
tab_FindFragment_title = (TabLayout)view.findViewById(R.id.tab_FindFragment_title);
vp_FindFragment_pager = (ViewPager)view.findViewById(R.id.vp_FindFragment_pager);
//初始化各fragment
hotRecommendFragment = new Find_hotRecommendFragment();
hotCollectionFragment = new Find_hotCollectionFragment();
hotMonthFragment = new Find_hotMonthFragment();
hotToday = new Find_hotToday();
//将fragment装进列表中
list_fragment = new ArrayList<>();
list_fragment.add(hotRecommendFragment);
list_fragment.add(hotCollectionFragment);
list_fragment.add(hotMonthFragment);
list_fragment.add(hotToday);
//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
list_title = new ArrayList<>();
list_title.add("热门推荐");
list_title.add("热门收藏");
list_title.add("本月热榜");
list_title.add("今日热榜");
//设置TabLayout的模式
tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加tab名称
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(2)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(3)));
fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager(),list_fragment,list_title);
//viewpager加载adapter
vp_FindFragment_pager.setAdapter(fAdapter);
//tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
//TabLayout加载viewpager
tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
//tab_FindFragment_title.set
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有