Fragment简单复用2

首先假设第一个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);

    }
}
223916_bL9y_2663968.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值