首先我先說一下這個佈局就是TabLayout加ViewPager然後我們TabLayout的第一個是 Fragment跟其餘7個Fragment
加載的數據不一樣,看代碼把,代碼有註釋。
public class FragmentHd extends Fragment{
public TabLayout hd_tl;
public ViewPager hd_vp;
List<String> listStr; //用於存放添加Fragment的結合
List<Fragment> listTv;//用於存放Fragment的集合
public TextView quanguo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragm_hd,null);
//找控件
hd_tl = view.findViewById(R.id.hd_tl);
hd_vp = view.findViewById(R.id.hd_vp);
quanguo = view.findViewById(R.id.quanguo);
//TabLayout的橫向佈局 添加數據
listStr = new ArrayList();
listStr.add("全部");
listStr.add("綜藝娛樂");
listStr.add("財經訪談");
listStr.add("文化旅遊");
listStr.add("時尚體育");
listStr.add("青少科技");
listStr.add("養生保健");
listStr.add("公益");
listTv = new ArrayList<>();
//第一個Fragment界面
listTv.add(new FragmentQuan());
listStr.get(0);
//剩下的7個Fragment
for (int i = 0; i < 7; i++) {
listTv.add(new Fragments(listStr.get(i)));
}
//拿到適配器
MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
hd_vp.setAdapter(adapter);
//ViewpAge的預加載解決方法
hd_vp.setOffscreenPageLimit(listTv.size());
//TabLayout和ViewPage進行聯動
hd_tl.setupWithViewPager(hd_vp);
return view;
}
//ViewPage的適配器
class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return listTv.get(position);
}
@Override
public int getCount() {
return listTv.size();
}
@Override
public CharSequence getPageTitle(int position) {
return listStr.get(position);
}
}
}
佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFB516"
>
<TextView
android:id="@+id/quanguo"
android:layout_width="50dp"
android:layout_height="match_parent"
android:text="全國∨"
android:textColor="#ffffff"
android:layout_marginLeft="10dp"
android:gravity="center"
/>
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="觀衆網"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="match_parent"
android:src="@mipmap/b8t"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="@+id/hd_tl"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/hd_vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>