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]; } } }
fragment复用==
最新推荐文章于 2018-09-07 19:30:09 发布