自定义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 布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6         <TextView
 7             android:gravity="center"
 8             android:id="@+id/f_Text"
 9             android:layout_width="match_parent"
10             android:layout_height="match_parent"
11             android:text="this is fragment"/>
12 
13 </LinearLayout>

3、编写fragment加载类

 1 public class FragmentUtil extends Fragment{
 2 private  int source;
 3 
 4 public void setSource(int source) {
 5         this.source = source;
 6     }
 7     @Nullable
 8     @Override
 9     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
10         return inflater.inflate(R.layout.f,container,false);
11     }
12 }

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

  1 package com.example.dell.newscenter.myview;
  2 
  3 
  4 
  5 import android.content.Context;
  6 import android.graphics.Color;
  7 import android.support.annotation.Nullable;
  8 import android.support.design.widget.TabLayout;
  9 import android.support.v4.app.Fragment;
 10 import android.support.v4.app.FragmentManager;
 11 import android.support.v4.app.FragmentPagerAdapter;
 12 import android.support.v4.view.ViewPager;
 13 import android.support.v7.app.AppCompatActivity;
 14 import android.util.AttributeSet;
 15 import android.util.Log;
 16 import android.view.LayoutInflater;
 17 import android.view.ViewGroup;
 18 import android.widget.LinearLayout;
 19 
 20 import com.example.dell.newscenter.R;
 21 import com.example.dell.newscenter.utils.FragmentUtil;
 22 
 23 import java.util.ArrayList;
 24 
 25 import static android.support.constraint.Constraints.TAG;
 26 
 27 public class FragmentLayout extends LinearLayout {
 28     private AppCompatActivity context;
 29     private TabLayout tabLayout  = null;//  上部放置 tablayout
 30     private ViewPager viewPager = null;// 下部放置  viewPager
 31     private Fragment[] fragments = {new Fragment(), new Fragment(), new Fragment()};
 32     private String titles[] = {"直播", "推荐", "追番"};
 33     private ArrayList<TabLayout.Tab> tabs = new ArrayList<>();
 34     private MyFragmentAdapter myFragmentAdapter ;
 35 
 36     public FragmentLayout(Context context) {
 37         super(context);
 38     }
 39 
 40     public FragmentLayout(Context context, @Nullable AttributeSet attrs) {
 41         super(context, attrs);
 42         this.context = (AppCompatActivity) context;
 43         LayoutInflater.from(context).inflate(R.layout.fragmentlayout, this);
 44         tabLayout = findViewById(R.id.fragment_tablayout);
 45         viewPager = findViewById(R.id.fragment_viewpager);
 46         setParam();
 47     }
 48 
 49 public void setParam() {
 50         //替换一个查看效果
 51         fragments[0] = new FragmentUtil();
 52         Log.d(TAG, "context上下文: "+context);
 53         //  使用适配器将ViewPager与Fragment绑定在一起
 54         myFragmentAdapter = new MyFragmentAdapter(context.getSupportFragmentManager());
 55         viewPager.setAdapter(myFragmentAdapter);
 56         //将TabLayout 与viewPager绑定在一起
 57         tabLayout.setupWithViewPager(viewPager);
 58 //        // 指定tab  的位置
 59 //        int count = tabLayout.getTabCount();
 60 //        Log.d(TAG, "count: " + count);
 61     }
 62 
 63     public void setFragments(Fragment[] fragments) {
 64         this.fragments = fragments;
 65     }
 66 
 67     public void setTitles(String[] titles) {
 68         this.titles = titles;
 69     }
 70 
 71     public void setTabs(ArrayList<TabLayout.Tab> tabs) {
 72         this.tabs = tabs;
 73     }
 74 
 75     public Fragment[] getFragments() {
 76         return fragments;
 77     }
 78 
 79     public String[] getTitles() {
 80         return titles;
 81     }
 82 
 83     public ArrayList<TabLayout.Tab> getTabs() {
 84         return tabs;
 85     }
 86 
 87 
 88 
 89     /**
 90      * 适配器
 91      */
 92     public class MyFragmentAdapter extends FragmentPagerAdapter {
 93         public MyFragmentAdapter(FragmentManager fm) {
 94             super(fm);
 95         }
 96         @Override
 97         public Fragment getItem(int position) {
 98             return fragments[position];
 99         }
100         @Override
101         public int getCount() {
102             return titles.length;
103         }
104         @Override
105         public CharSequence getPageTitle(int position) {
106             return titles[position];
107         }
108     }
109 }

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  绑定适配器

 1 package com.example.dell.newscenter.myfragment;
 2 
 3 
 4 import android.content.Context;
 5 import android.support.design.widget.TabLayout;
 6 import android.support.v4.app.Fragment;
 7 import android.support.v4.app.FragmentManager;
 8 import android.support.v4.app.FragmentPagerAdapter;
 9 import android.support.v4.view.ViewPager;
10 import android.support.v7.app.AppCompatActivity;
11 import android.util.Log;
12 import android.widget.LinearLayout;
13 
14 import com.example.dell.newscenter.R;
15 
16 import java.util.ArrayList;
17 
18 import static android.support.constraint.Constraints.TAG;
19 
20 public class MyFragmentLayout extends LinearLayout{
21 private TabLayout tabLayout = null;
22 private AppCompatActivity context = null;
23 private Fragment[] fragments = {new Fragment(),new Fragment(),new Fragment()};
24 private String titles[] = {"直播","推荐","追番"};
25 private ArrayList<TabLayout.Tab> tabs  = new ArrayList<>();
26 private ViewPager mainCenterMainpageViewpager = null;
27 private  MyFragmentAdapter myFragmentAdapter;
28 
29     public MyFragmentLayout(Context context) {
30         super(context);
31         this.context = (AppCompatActivity) context;
32     }
33 
34     public void initMainBottomMainPageFragment(){
35         Log.d(TAG, "初始化Fragment: " +context);
36         //  使用适配器将ViewPager与Fragment绑定在一起
37         mainCenterMainpageViewpager =context.findViewById(R.id.fragment_viewpager);
38         myFragmentAdapter = new MyFragmentAdapter(context.getSupportFragmentManager());
39         mainCenterMainpageViewpager.setAdapter(myFragmentAdapter);
40 
41       //将TabLayout 与viewPager绑定在一起
42         tabLayout = context.findViewById(R.id.fragment_tablayout);
43         tabLayout.setupWithViewPager(mainCenterMainpageViewpager);
44         // 指定tab  的位置
45         tabLayout  = context.findViewById(R.id.fragment_tablayout);
46         int count = tabLayout.getTabCount();
47         Log.d(TAG, "count: "+count);
48 
49     }
50     public void setTitles(String[] titles) {
51         this.titles = titles;
52     }
53     /**
54      *
55      *
56      *  适配器
57      */
58     public class MyFragmentAdapter extends FragmentPagerAdapter {
59 
60         public MyFragmentAdapter(FragmentManager fm) {
61             super(fm);
62         }
63         @Override
64         public Fragment getItem(int position) { return fragments[position]; }
65         @Override
66         public int getCount() { return titles.length; }
67         @Override
68         public CharSequence getPageTitle(int position) { return titles[position]; }
69     }
70 
71 }

2、调用

MyFragmentLayout m1 = new MyFragmentLayout(MainActivity.this);
m1.setTitles(new String[]{"直播","推荐","追番"});
m1.initMainBottomMainPageFragment();
原文地址:https://www.cnblogs.com/the-wang/p/9004311.html