一、抽取视图文件,实例化需要在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. est6 -- Object.is()、Object.assign()、Object.defineProperty()、Symbol、Proxy

    Object.is()用来比较两个值是否严格相等.它与严格比较运算符(===)的行为基本一致,不同之处只有两个:一是+0不等于-0,二是NaN等于自身. + === - //true NaN === ...

  2. springboot使用Mybatis(xml和注解)全解析

    ​  刚毕业的第一份工作是 java 开发,项目中需要用到 mybatis,特此记录学习过程,这只是一个简单 demo,mybatis 用法很多不可能全部写出来,有更复杂的需求建议查看 mybatis ...

  3. 使用一个数组存储一个英文句子"java is an object oriented programing language"

    class fun { public static void main(String[] args) { String str="java is an object oriented pro ...

  4. 提高在Xcode上的工作效率

    对于在Xcode上提高工作效率,内功在这不提,对于外力,我将它分为三类: 工具.快捷键和小技巧.主要获得的路径是通过平时积累和看 WWDC12 上的 Session 402:Working Effic ...

  5. android listview 添加数据

    <span style="white-space:pre"> </span>listView = (ListView) findViewById(R.id. ...

  6. 【spring data jpa】启动报错:nested exception is java.util.NoSuchElementException

    spring boot项目中 使用spring data jpa 启动报错: org.springframework.beans.factory.UnsatisfiedDependencyExcept ...

  7. APP公布到应用市场(苹果APP STORE+安卓各大应用市场)

    注意事项 1.应用要签名,为了以后可以顺利更新应用.要保持每次的签名一致,所以要妥善保管好签名数据. 2.进行公布測试,最好有个检查表,每次公布的时候进行核查. 苹果APP STORE 一.证书的导出 ...

  8. nyoj43 24 Point game(DFS)

    题目43 题目信息 pid=43" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 讨论 ...

  9. wamp中apache2.4.9配置httpd.conf允许外部访问

        安装最新的wamp后发现通过外部网络无法访问本机的apache.在网上查询了相关问题,所有的答案基本都是说在httpd.conf文件中加入语句Allow from all.但是这些对应的是ap ...

  10. ubuntu 14.04 LTS 安装webbentch压力測试工具

    近期在做 压力測试工具,除了apache的ab測试工具外,发现webbentch工具也不错,这里简介下这两个工具. 一.webbentch安装: wget http://blog.s135.com/s ...