先上效果圖:java
一、 編寫 xml佈局文件android
<?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>
二、編寫各個fragment 佈局文件app
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>
三、編寫fragment加載類ide
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 }
三、在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 }
三、調用字體
<com.example.dell.newscenter.myview.FragmentLayout android:layout_width="match_parent" android:layout_height="match_parent"> </com.example.dell.newscenter.myview.FragmentLayout>
一、編寫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 }
二、調用this
MyFragmentLayout m1 = new MyFragmentLayout(MainActivity.this); m1.setTitles(new String[]{"直播","推薦","追番"}); m1.initMainBottomMainPageFragment();