首先假设第一个Fragment是单独的剩下的全是相同布局的Fragment所以为了高端大气上档次我们复用它
创建两个Fragment第一不管它,主要是第二个需要复用的Fragment
package com.example.pandatv.View.live; import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.View; import com.example.pandatv.Base.BaseFragment; import com.example.pandatv.R; import com.example.pandatv.View.IHomeView; import com.example.pandatv.View.live.Adapter.MainAdapter; import com.example.pandatv.View.live.Bean.BiaoTiBean; import com.example.pandatv.View.live.Fragment.LiveFragment; import com.example.pandatv.View.live.Fragment.ReuseFragment; import com.example.pandatv.config.Urls; import com.example.pandatv.presenter.HomePresenter; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class PandaLiveFragment extends BaseFragment implements IHomeView{ public TabLayout tab_pandalive; public ViewPager vp_pandalive; @Override protected int getLayoutRes() { return R.layout.fragment_eye; } @Override protected void initData() { //网络请求 new HomePresenter(this).getUrl(Urls.PANDALIVETAB); } @Override protected void initView(View view) { tab_pandalive = view.findViewById(R.id.tab_pandalive); vp_pandalive = view.findViewById(R.id.vp_pandalive); } @Override public void getstringData(String data) { List<Fragment> fragments = new ArrayList<>(); List<String> slist = new ArrayList<>(); Gson gson = new Gson(); BiaoTiBean biaoTiBean = gson.fromJson(data, BiaoTiBean.class); List<BiaoTiBean.TablistBean> tablist = biaoTiBean.getTablist(); //直播 fragments.add(new LiveFragment()); //添加标题 for (int i = 0; i < tablist.size(); i++) { String title = tablist.get(i).getTitle(); slist.add(title); } //添加复用Fragment,注意长度。上面添加了一个单独的,这里Fragment比标题多一个 for (int i = 0; i < tablist.size()-1; i++) { ReuseFragment reuseFragment = new ReuseFragment(); fragments.add(reuseFragment); Bundle bundle = new Bundle(); bundle.putString("title",tablist.get(i+1).getTitle()); reuseFragment.setArguments(bundle); //在Fragment中必须用getChildFragmentManager() MainAdapter adapter = new MainAdapter(getChildFragmentManager(),fragments,slist); //设置适配器 vp_pandalive.setAdapter(adapter); //设置tabLayout tab_pandalive.setupWithViewPager(vp_pandalive); //设置文字的颜色 tab_pandalive.setTabTextColors(Color.BLACK,Color.BLUE); //设置下划线的颜色 tab_pandalive.setSelectedTabIndicatorColor(Color.BLUE); //设置是否滑动还是平铺 tab_pandalive.setTabMode(TabLayout.MODE_SCROLLABLE); } } }
复用的Fragment中
package com.example.pandatv.View.live.Fragment; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ListView; import com.example.pandatv.Base.BaseFragment; import com.example.pandatv.R; import com.example.pandatv.View.IHomeView; import com.example.pandatv.View.live.Adapter.ReuseAdapter; import com.example.pandatv.View.live.Bean.JingCaiBean; import com.example.pandatv.presenter.HomePresenter; import com.google.gson.Gson; import java.util.List; public class ReuseFragment extends BaseFragment implements IHomeView{ private String title; private ListView lv_reuse; @Override protected int getLayoutRes() { return R.layout.fragment_reuse; } @Override protected void initData() { //获取Activity不同的传值 Bundle bundle = getArguments(); if (bundle != null) { title = bundle.get("title").toString(); Log.d("MyFragment", title); } switch (title){ case"精彩一刻": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100167216881&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "当熊不让": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100332640004&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "超萌滚滚秀": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100272959126&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "熊猫档案": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100340574858&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "熊猫TOP榜": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100284428835&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "熊猫那些事儿": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100237714751&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "特别节目": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100167308855&n=7&serviceId=panda&o=desc&of=time&p=1"); break; case "原创新闻": new HomePresenter(this).getUrl("http://api.cntv.cn/video/videolistById?vsid=VSET100219009515&n=7&serviceId=panda&o=desc&of=time&p=1"); break; } } protected void initView(View inflate) { lv_reuse = (ListView) inflate.findViewById(R.id.lv_reuse); } public static ReuseFragment newInstance(String name) { Bundle args = new Bundle(); args.putString("name", name); ReuseFragment fragment = new ReuseFragment(); fragment.setArguments(args); return fragment; } @Override public void getstringData(String data) { Gson gson = new Gson(); JingCaiBean jingCaiBean = gson.fromJson(data, JingCaiBean.class); List<JingCaiBean.VideoBean> video = jingCaiBean.getVideo(); ReuseAdapter adapter = new ReuseAdapter(getActivity(),video); lv_reuse.setAdapter(adapter); } }