您的位置:首页 > 其它

TabLayout加载多个Fragment

2017-12-18 19:35 288 查看


首先我先说一下这个布局就是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"
9c9e

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐