//MyPagerAdapter
public class MyPagerAdapter extends FragmentPagerAdapter{//MainActivity
// private String[] titles = {"周一","周二","周三","周四","周五","周六","周日"};
List<TitleMenu.DataBean> results;
public MyPagerAdapter(FragmentManager fm, List<TitleMenu.DataBean> results) {
super(fm);
this.results = results;
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragment();
// Bundle bundle = new Bundle();
// bundle.putString("text",titles[position]);
// myFragment.setArguments(bundle);
return myFragment;
}
@Override
public int getCount() {
return results != null ? results.size() : 0;
}
@Override
public CharSequence getPageTitle(int position) {
return results.get(position).getName();
}
}
public class MainActivity extends AppCompatActivity {
private TabLayout main_tab;
private ViewPager main_pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示当前页面布局
setContentView(R.layout.activity_main);
//获取资源ID
main_tab = (TabLayout) findViewById(R.id.main_tab);
main_pager = (ViewPager) findViewById(R.id.main_pager);
//适配数据
// main_pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
//将tablayout和viewpager关联起来
getData();
main_tab.setupWithViewPager(main_pager);
}
private void getData() {
RequestParams params = new RequestParams("http://lf.snssdk.com/neihan/service/tabs" +
"/?essence=1&iid=3216590132&device_id=32613520945&ac=wifi&channel=360&aid=" +
"7&app_name=joke_essay&version_code=612&version_name=6.1.2&dev" +
"ice_platform=android&ssmix=a&device_type=sansung&device_brand=" +
"xiaomi&os_api=28&os_version=6.10.1&uuid=326135942187625&openudid=" +
"3dg6s95rhg2a3dg5&manifest_version_code=61" +
"2&resolution=1450*2800&dpi=620&update_version_code=6120");
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
TitleMenu titleMenu = gson.fromJson(result, TitleMenu.class);
List<TitleMenu.DataBean> results = titleMenu.getData();
main_pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), results));
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}