2. 使用FragemntTabHost+Fragment实现功能界面切换;
3. 使用HttpClient网络框架进行网络访问;
图5.1主界面图
主要实现代码如下所示:
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private NavigationView navigationView;
private CoordinatorLayout coordinatorLayout;
private FragmentTabHost ftIndicatorTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initContentView();
initToolBar();
initDrawerLayout();
initNavigationView();
setFragmentTabHost();
}
private void initNavigationView() {
navigationView = (NavigationView) findViewById(R.id.nv_main_navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawerLayout.closeDrawers();
navigationView.setCheckedItem(item.getItemId());
return true;
}
});
}
private void initDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.dl_main_drawerLayout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, 0, 0);
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
}
private void initToolBar() {
toolbar = (Toolbar) findViewById(R.id.tb_main_toolBar);
toolbar.setTitle("热点新闻");
toolbar.setTitleTextColor(UIUtils.getColor(R.color.black_gray));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void initContentView() {
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.dl_main_coordinatorLayout);
}
private void setFragmentTabHost() {
//1.初始化FragmentTabHost
ftIndicatorTabHost = (FragmentTabHost) findViewById(R.id.ft_indicator_fragment_host);
//2.关联tabHost和FrameLayout
ftIndicatorTabHost.setup(UIUtils.getContext(), getSupportFragmentManager(), R.id.fl_main);
//3.创建Tab
TabHost.TabSpec tab1 = ftIndicatorTabHost.newTabSpec("热点新闻");
tab1.setIndicator(getIndicatorView("热点新闻", R.drawable.tab_news));
TabHost.TabSpec tab2 = ftIndicatorTabHost.newTabSpec("服务");
tab2.setIndicator(getIndicatorView("服务", R.drawable.tab_service));
TabHost.TabSpec tab3 = ftIndicatorTabHost.newTabSpec("求职");
tab3.setIndicator(getIndicatorView("求职", R.drawable.tab_work));
//4.添加tab
ftIndicatorTabHost.addTab(tab1, NewsFragment.class, getBundle("热点新闻"));
ftIndicatorTabHost.addTab(tab2, ServiceFragment.class, getBundle("服务")); 基于Android的大学生生活小助手设计与开发(6):http://www.751com.cn/jisuanji/lunwen_1423.html