布局---------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="maxinawei.bw.com.zhonghelianxi.Fragment1"
android:background="#09f"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myTab"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
android:layout_below="@+id/myTab"
></android.support.v4.view.ViewPager>
</RelativeLayout>
具体的操作-----------------------------------------
public class Fragment1 extends Fragment {
private ViewPager pager;
private TabLayout mytab;
private List<DateType> meuns;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container, false);
pager = v.findViewById(R.id.pager);
mytab = v.findViewById(R.id.myTab);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//初始化
initmeuns();
MyAdapter adapter=new MyAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
mytab.setupWithViewPager(pager);
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return meuns.get(position).getTitle();
}
@Override
public Fragment getItem(int position) {
Bundle bundle=new Bundle();
bundle.putString("type",meuns.get(position).getTitle());
ContentFrag frag=new ContentFrag();
frag.setArguments(bundle);
return frag;
}
@Override
public int getCount() {
return meuns.size();
}
}
private void initmeuns() {
meuns=new ArrayList<>();
meuns.add(new DateType("数据新闻","xbsjxw"));
meuns.add(new DateType("快讯","txs"));
meuns.add(new DateType("头条","toutiao"));
meuns.add(new DateType("精编公告","news/mobile/jbgg"));
meuns.add(new DateType("美股","news/mobile/mgxw"));
meuns.add(new DateType("港股","news/mobile/ggxw"));
meuns.add(new DateType("基金","news/mobile/jjxw"));
meuns.add(new DateType("理财","news/mobile/lcxw"));
}