public class MainActivity extends FragmentActivity {
Listfragments=new ArrayList<>();
private TabLayout tablayout;
private ViewPager viewPager;
Listtab=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
initId();
//添加数据
initData();
tablayout.addTab(tablayout.newTab().setText(tab.get(0)));
tablayout.addTab(tablayout.newTab().setText(tab.get(1)));
tablayout.addTab(tablayout.newTab().setText(tab.get(2)));
tablayout.addTab(tablayout.newTab().setText(tab.get(3)));
MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//设置和ViewPater关联
tablayout.setupWithViewPager(viewPager);
tablayout.setTabsFromPagerAdapter(adapter);
}
private void initData() {
fragments.add(new FragmentA());
fragments.add(new FragmentB());
fragments.add(new FragmentC());
fragments.add(new FragmentD());
tab.add("最新日报");
tab.add("专栏");
tab.add("热门");
tab.add("主题日报");
}
private void initId() {
tablayout = (TabLayout) findViewById(R.id.tab);
viewPager = (ViewPager) findViewById(R.id.viewpager);
}
private class MyAdapter extends FragmentPagerAdapter{
@Override
public CharSequence getPageTitle(int position) {
return tab.get(position);
}
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
//---------------------------------------------------------
public class GrideActivity extends AppCompatActivity { Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){
case 0:
String obj = (String) msg.obj;
Gson gson=new Gson();
TiaoZhuanData tiaoZhuanData = gson.fromJson(obj, TiaoZhuanData.class); Liststories = tiaoZhuanData.getStories();
MyTiaoZhuanAdapter adapter=new MyTiaoZhuanAdapter(GrideActivity.this,stories); recyclerview.setAdapter(adapter);
break;
} } };
private RecyclerView recyclerview;
private void adapterGrid(Liststories) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gride);
recyclerview = (RecyclerView) findViewById(R.id.recycler_grid);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(GrideActivity.this);
recyclerview.setLayoutManager(linearLayoutManager);
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
Toast.makeText(this, ""+id, Toast.LENGTH_SHORT).show();
String path="http://news-at.zhihu.com/api/4/theme/"+id;
initData(path);
}
private void initData(String path) {
OkHttp3Utils.doGet(path, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Log.i("mmm",json);
Message message=new Message();
message.what=0;
message.obj=json;
handler.sendMessage(message);
}
});
}