自定义fragmentlayout
一、抽取视图文件,实例化需要在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的更多相关文章
- 关于Unity3D自定义编辑器的学习
被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做). 刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...
- 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表
1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...
- JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- ASP.NET Aries 入门开发教程5:自定义列表页工具栏区
前言: 抓紧时间,继续写教程,因为发现用户期待的内容,都在业务处理那一块. 不得不继续勤劳了. 这节主要介绍工具栏区的玩法. 工具栏的默认介绍: 工具栏默认包括5个按钮,根据不同的权限决定显示: 添加 ...
- UWP中实现自定义标题栏
UWP中实现自定义标题栏 0x00 起因 在UWP开发中,有时候我们希望实现自定义标题栏,例如在标题栏中加入搜索框.按钮之类的控件.搜了下资料居然在一个日文网站找到了一篇介绍这个主题的文章: http ...
- JavaScript 自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- 【WCF】自定义错误处理(IErrorHandler接口的用法)
当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...
- 自定义Inspector检视面板
Unity中的Inspector面板可以显示的属性包括以下两类:(1)C#以及Unity提供的基础类型:(2)自定义类型,并使用[System.Serializable]关键字序列化,比如: [Sys ...
随机推荐
- python笔记1:python基础
1.Python模块的标准文件模板: #!/usr/bin/env python #第1行注释可以让这个 .py 文件直接在Unix/Linux/Mac上运行 # -*- coding: utf-8 ...
- CentOS6.7安装部署LNMP(nginx1.8.0+php5.6.10+mysql5.6.12)
IP-10.0.0.8 1.安装nginx mkdir -p /server/tools cd /server/tools yum install -y pcre pcre-devel openssl ...
- Codeforces Gym 100650C The Game of Efil 模拟+阅读题
原题链接:http://codeforces.com/gym/100650/attachments/download/3269/20052006-acmicpc-east-central-north- ...
- highcharts中放aqi及6要素,再加上气象5要素的图
var chart = Highcharts.chart('container', { chart: { zoomType: 'xy' }, title: { text: '东京月平均天气数据' }, ...
- 转 Tesseract-OCR 字符识别---样本训练
转自:http://blog.csdn.net/feihu521a/article/details/8433077 Tesseract是一个开源的OCR(Optical Character Recog ...
- Spring IOC知识java反射
[1] Java反射知识-->Spring IoC :http://www.iteye.com/topic/1123081 [2] Java动态代理-->Spring AOP :http: ...
- Window10下Apache2.4的安装和运行
以前用Python运行的Web框架都是要运行在Linux下,加上WSGI服务器,比如Gunicorn+Flask,后来了解到了Apache,看看能不能基于Apache这个Web服务器下给python提 ...
- DevExpress.XtraGrid 【转】
http://www.cnblogs.com/zeroone/p/4574539.html DevExpress.XtraGrid控件使用 该控件类是一个表格控件,但是其具有很多方便而使用的功能,例如 ...
- 解决Sophos UTM 9防火墙上的“根分区填满”问题
Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall 收到“Sophos UTM 9”防火墻的“根分區填满”问题的 ...
- 怎样隐藏Windows7 系统保留分区
安装Windows7操作系统时须要预留出100MB左右的系统保留盘分区.在Windows7激活是必须给它分配盘符,否则无法将其成功激活,但是激活后该盘符永久地显示了出来,怎样将其隐藏掉呢? 1.隐藏前 ...