Fragment簡單複用2

首先假設第一個Fragment是單獨的剩下的全是相同佈局的Fragment因此爲了高端大氣上檔次咱們複用它java

建立兩個Fragment第一無論它,主要是第二個須要複用的Fragmentandroid

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中api

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);

    }
}
相關文章
相關標籤/搜索