一、抽取视图文件,实例化需要在xml文件中

先上效果图:

  

1、  编写 xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!--
//导航栏背景颜色
android:background="#ffff00"
//指示器颜色
app:tabIndicatorColor="#66ff33"
//指示器高度
app:tabIndicatorHeight="20p"
//普通状态下文字的颜色
app:tabTextColor="@color/colorPrimary"
//选中时文字的颜色
app:tabSelectedTextColor="#CC33FF"
//是否可滑动:fixed:固定;scrollable:可滑动
app:tabMode="fixed"
//设置选项卡的背景:此处要写一个selector)
app:tabBackground="@drawable/selected"
//设置字体大小:此处要写一个style) app:tabTextAppearance="@style/MyTabLayoutTextAppearance" -->
<!--android.support.design.widget.TabLayout 可以制作动画效果的tablayout --> <android.support.design.widget.TabLayout
android:id="@+id/fragment_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="10dp"
app:tabTextColor="@color/colorAccent"
app:tabSelectedTextColor="@android:color/white"
app:tabMode="scrollable"
app:tabBackground="@drawable/main_center_mainpage_tablayout_tabbackground_selector" /> <android.support.v4.view.ViewPager
android:id="@+id/fragment_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> </android.support.v4.view.ViewPager>
</LinearLayout>

2、编写各个fragment 布局文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:gravity="center"
android:id="@+id/f_Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is fragment"/> </LinearLayout>

3、编写fragment加载类

 public class FragmentUtil extends Fragment{
private int source; public void setSource(int source) {
this.source = source;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.f,container,false);
}
}

3、在java文件中加载布局并编写适配器

 package com.example.dell.newscenter.myview;

 import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout; import com.example.dell.newscenter.R;
import com.example.dell.newscenter.utils.FragmentUtil; import java.util.ArrayList; import static android.support.constraint.Constraints.TAG; public class FragmentLayout extends LinearLayout {
private AppCompatActivity context;
private TabLayout tabLayout = null;// 上部放置 tablayout
private ViewPager viewPager = null;// 下部放置 viewPager
private Fragment[] fragments = {new Fragment(), new Fragment(), new Fragment()};
private String titles[] = {"直播", "推荐", "追番"};
private ArrayList<TabLayout.Tab> tabs = new ArrayList<>();
private MyFragmentAdapter myFragmentAdapter ; public FragmentLayout(Context context) {
super(context);
} public FragmentLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = (AppCompatActivity) context;
LayoutInflater.from(context).inflate(R.layout.fragmentlayout, this);
tabLayout = findViewById(R.id.fragment_tablayout);
viewPager = findViewById(R.id.fragment_viewpager);
setParam();
} public void setParam() {
//替换一个查看效果
fragments[0] = new FragmentUtil();
Log.d(TAG, "context上下文: "+context);
// 使用适配器将ViewPager与Fragment绑定在一起
myFragmentAdapter = new MyFragmentAdapter(context.getSupportFragmentManager());
viewPager.setAdapter(myFragmentAdapter);
//将TabLayout 与viewPager绑定在一起
tabLayout.setupWithViewPager(viewPager);
// // 指定tab 的位置
// int count = tabLayout.getTabCount();
// Log.d(TAG, "count: " + count);
} public void setFragments(Fragment[] fragments) {
this.fragments = fragments;
} public void setTitles(String[] titles) {
this.titles = titles;
} public void setTabs(ArrayList<TabLayout.Tab> tabs) {
this.tabs = tabs;
} public Fragment[] getFragments() {
return fragments;
} public String[] getTitles() {
return titles;
} public ArrayList<TabLayout.Tab> getTabs() {
return tabs;
} /**
* 适配器
*/
public class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments[position];
}
@Override
public int getCount() {
return titles.length;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
}

3、调用

  <com.example.dell.newscenter.myview.FragmentLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> </com.example.dell.newscenter.myview.FragmentLayout>

二、java代码  用new 实例化

1、编写layout  绑定适配器

 package com.example.dell.newscenter.myfragment;

 import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.LinearLayout; import com.example.dell.newscenter.R; import java.util.ArrayList; import static android.support.constraint.Constraints.TAG; public class MyFragmentLayout extends LinearLayout{
private TabLayout tabLayout = null;
private AppCompatActivity context = null;
private Fragment[] fragments = {new Fragment(),new Fragment(),new Fragment()};
private String titles[] = {"直播","推荐","追番"};
private ArrayList<TabLayout.Tab> tabs = new ArrayList<>();
private ViewPager mainCenterMainpageViewpager = null;
private MyFragmentAdapter myFragmentAdapter; public MyFragmentLayout(Context context) {
super(context);
this.context = (AppCompatActivity) context;
} public void initMainBottomMainPageFragment(){
Log.d(TAG, "初始化Fragment: " +context);
// 使用适配器将ViewPager与Fragment绑定在一起
mainCenterMainpageViewpager =context.findViewById(R.id.fragment_viewpager);
myFragmentAdapter = new MyFragmentAdapter(context.getSupportFragmentManager());
mainCenterMainpageViewpager.setAdapter(myFragmentAdapter); //将TabLayout 与viewPager绑定在一起
tabLayout = context.findViewById(R.id.fragment_tablayout);
tabLayout.setupWithViewPager(mainCenterMainpageViewpager);
// 指定tab 的位置
tabLayout = context.findViewById(R.id.fragment_tablayout);
int count = tabLayout.getTabCount();
Log.d(TAG, "count: "+count); }
public void setTitles(String[] titles) {
this.titles = titles;
}
/**
*
*
* 适配器
*/
public class MyFragmentAdapter extends FragmentPagerAdapter { public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) { return fragments[position]; }
@Override
public int getCount() { return titles.length; }
@Override
public CharSequence getPageTitle(int position) { return titles[position]; }
} }

2、调用

MyFragmentLayout m1 = new MyFragmentLayout(MainActivity.this);
m1.setTitles(new String[]{"直播","推荐","追番"});
m1.initMainBottomMainPageFragment();

自定义fragmentlayout的更多相关文章

  1. 关于Unity3D自定义编辑器的学习

    被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做).  刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...

  2. 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表

    1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...

  3. JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome

    今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...

  4. ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单

    前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...

  5. ASP.NET Aries 入门开发教程5:自定义列表页工具栏区

    前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...

  6. UWP中实现自定义标题栏

    UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...

  7. JavaScript 自定义对象

    在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...

  8. 【WCF】自定义错误处理(IErrorHandler接口的用法)

    当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...

  9. 自定义Inspector检视面板

    Unity中的Inspector面板可以显示的属性包括以下两类:(1)C#以及Unity提供的基础类型:(2)自定义类型,并使用[System.Serializable]关键字序列化,比如: [Sys ...

随机推荐

  1. CodeForces - 393E Yet Another Number Sequence

    Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  2. Jenkins使用Pipeline插件实现多个Job之间的串并联(教程收集)(待实践)

    在原始不使用插件时,在Jenkins我们要实现多个Job之间的连续集成时,我们一般的做法就是在每个Job上关联另一个Job,但是,这样做有个弊端,只能是连续的,如果要实现串或并联这样的,估计比较难:但 ...

  3. eclipse项目java版本更改

    然后.右键点击项目->properties->Java Compiler->....如图 ​ 最后,右键点击项目->properties->Project  Facets ...

  4. fastscript增加三方控件

    fastscript增加三方控件 A.关于如何使用第三方控件,增加方法.属性.事件)举例如下: 如:有一控件为edtbutton:TedtButton,我们需要在动态脚本中使用该控件.我们采用如下方法 ...

  5. xamarin.android pullToRefresharp.Android下拉刷新样式、侧滑删除功能

    如果你正则使用xamarin.From开发项目,那么listview一定是你比不可少的控件.但是由于xamarin的listview在安卓上的功能有限,所以经常需要使用Renderers来改写平台实现 ...

  6. python 工具mouse_find 鼠标定位

    import os,time import pyautogui as pag try: while True: print ("Press Ctrl-C to end") x,y ...

  7. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  8. Git以及github的使用方法(二)创建仓库,git add添加到“暂储区”,git commit添加到“本地仓库”

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...

  9. Linux mm相关的问题

    [S]为什么High MEM是从896M開始的? As the running kernel needs these functions, a region of at least VMALLOC_R ...

  10. 每天进步一点点——mysql——Percona XtraBackup(innobackupex)

    一.  简单介绍 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对InnoDB和XtraDB存储引擎的数据库非堵塞地备份(对于MyISAM的备份相同须要加表锁).Xt ...