fragment复用==

public class HomePage extends Fragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list;
    private String[] str = new String[]{"推荐", "体育", "科技", "教育", "娱乐", "财经", "股票", "旅游", "美女"};
    private Button button;
    private List<ChannelBean> channelBeanList;
    private List<String> newslist;
    private SharedPreferences sp;
    private String json;
    private AppCompatActivity context;
    private List<String> urllist;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = (AppCompatActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.activity_home_page, null);
        tabLayout = (TabLayout) view.findViewById(R.id.tab);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        sp = context.getSharedPreferences("set", context.MODE_PRIVATE);
        json = sp.getString("use", null);
        initView();
        button = (Button) view.findViewById(R.id.jia);
        newslist = new ArrayList<>();
        newslist.add("推荐");newslist.add("热点");newslist.add("北京");
        newslist.add("股票");
        newslist.add("社会");newslist.add("图片");newslist.add("娱乐");
        newslist.add("问答");newslist.add("科技");newslist.add("汽车");
        newslist.add("财经");newslist.add("军事");newslist.add("体育");
        newslist.add("段子");newslist.add("美女");newslist.add("国际");
        newslist.add("健康");newslist.add("小说");newslist.add("时尚");
        newslist.add("旅游");newslist.add("数码");newslist.add("教育");
        newslist.add("房产");newslist.add("历史");newslist.add("宠物");
        newslist.add("美食");newslist.add("游戏");newslist.add("视频");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (json == null) {
                    channelBeanList = new ArrayList<ChannelBean>();
                    for (int i = 0; i < newslist.size(); i++) {
                        ChannelBean bean = null;
                        if (i < 10) {
                            bean = new ChannelBean(newslist.get(i), true);
                        } else {
                            bean = new ChannelBean(newslist.get(i), false);
                        }
                        channelBeanList.add(bean);
                    }
                    ChannelActivity.startChannelActivity(context, channelBeanList);
                } else {
                    ChannelActivity.startChannelActivity(context, json);
                }
            }
        });
        urllist = new ArrayList<>();
        for (String title : str) {
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=war&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=sport&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=tech&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=edu&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=ent&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=money&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=gupiao&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=travel&limit=10&page=");
            urllist.add("http://wangyi.butterfly.mopaasapp.com/news/api?type=lady&limit=10&page=");
        }
        return view;
    }


    private void initView() {
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        MyAdapter adapter = new MyAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Recommend rec = new Recommend();
            Bundle bundle = new Bundle();
            bundle.putString("urlconnect", urllist.get(position));
            rec.setArguments(bundle);
            return rec;
        }

        @Override
        public int getCount() {
            return str.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return str[position];
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值