80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.mrwang.xbjzw;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.baidu.mapapi.SDKInitializer;
import com.example.mrwang.xbjzw.Fragment.InformationFragment;
import com.example.mrwang.xbjzw.Fragment.JobFragment;
import com.example.mrwang.xbjzw.Fragment.PageFragment;
import com.example.mrwang.xbjzw.Fragment.PersonFragment;
import com.example.mrwang.xbjzw.Fragment.SearchFragment;
//应用主界面 里面分了5个Fragment
public class MainActivity extends BaseActivity implements View.OnClickListener {
private RelativeLayout layout_page, layout_information, layout_job,layout_search,layout_person;
private FrameLayout layout_fragment;
private TextView tv_page, tv_information, tv_job,tv_search,tv_person;
private ImageView iv_page, iv_information, iv_job,iv_search,iv_person;
private FragmentManager fragmentManager;
private PageFragment pageFragment;
private InformationFragment informationFragment;
private JobFragment jobFragment;
private SearchFragment searchFragment;
private PersonFragment personFragment;
private Fragment indexFragment;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
initView();
fragmentManager = getFragmentManager();
setTabSelection(0);
}
private void initView() {
toolbar = (android.support.v7.widget.Toolbar) this.findViewById(R.id.toolbar);
toolbar.setTitle("主页"); // 标题的文字需在setSupportActionBar之前,不然会无效
toolbar.setTitleTextColor(Color.WHITE);
this.setSupportActionBar(toolbar);
layout_fragment = (FrameLayout) findViewById(R.id.layout_fragment);
layout_fragment = (FrameLayout) findViewById(R.id.layout_fragment);
layout_page = (RelativeLayout) findViewById(R.id.layout_page);
layout_information = (RelativeLayout) findViewById(R.id.layout_information);
layout_job = (RelativeLayout) findViewById(R.id.layout_job);
layout_search = (RelativeLayout) findViewById(R.id.layout_search);
layout_person = (RelativeLayout) findViewById(R.id.layout_person);
tv_page = (TextView) findViewById(R.id.tv_page);
tv_information = (TextView) findViewById(R.id.tv_information);
tv_job = (TextView) findViewById(R.id.tv_job);
tv_search = (TextView) findViewById(R.id.tv_search);
tv_person = (TextView) findViewById(R.id.tv_person);
iv_page = (ImageView) findViewById(R.id.iv_page);
iv_information = (ImageView) findViewById(R.id.iv_information);
iv_job = (ImageView) findViewById(R.id.iv_job);
iv_search = (ImageView) findViewById(R.id.iv_search);
iv_person = (ImageView) findViewById(R.id.iv_person);
layout_page.setOnClickListener(this);
layout_information.setOnClickListener(this);
layout_job.setOnClickListener(this);
layout_search.setOnClickListener(this);
layout_person.setOnClickListener(this);
}
private void setTabSelection(int index) {
clearSelection();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (index){
case 0:
this.getSupportActionBar().setTitle("首页");
iv_page.setBackgroundResource(R.drawable.page_press);
tv_page.setTextColor(getResources().getColor(R.color.bottom_press));
if (pageFragment == null) {
pageFragment = new PageFragment();
transaction.add(R.id.layout_fragment, pageFragment,"PAGE");
} else {
indexFragment = fragmentManager.findFragmentByTag("PAGE");
transaction.show(indexFragment);
}
break;
case 1:
this.getSupportActionBar().setTitle("工程信息");
iv_information.setBackgroundResource(R.drawable.information_press);
tv_information.setTextColor(getResources().getColor(R.color.bottom_press));
if (informationFragment == null) {
informationFragment = new InformationFragment();
transaction.add(R.id.layout_fragment, informationFragment,"INFORMATION");
} else {
indexFragment = fragmentManager.findFragmentByTag("INFORMATION");
transaction.show(indexFragment);
}
break;
case 2:
this.getSupportActionBar().setTitle("求职招聘");
iv_job.setBackgroundResource(R.drawable.job_press);
tv_job.setTextColor(getResources().getColor(R.color.bottom_press));
if (jobFragment == null) {
jobFragment = new JobFragment();
transaction.add(R.id.layout_fragment, jobFragment,"JOB");
} else {
indexFragment = fragmentManager.findFragmentByTag("JOB");
transaction.show(indexFragment);
}
break;
case 3:
this.getSupportActionBar().setTitle("搜索查询");
iv_search.setBackgroundResource(R.drawable.search_press);
tv_search.setTextColor(getResources().getColor(R.color.bottom_press));
if (searchFragment == null) {
searchFragment = new SearchFragment();
transaction.add(R.id.layout_fragment, searchFragment,"SEARCH");
} else {
indexFragment = fragmentManager.findFragmentByTag("SEARCH");
transaction.show(indexFragment);
}
break;
case 4:
this.getSupportActionBar().setTitle("个人中心");
iv_person.setBackgroundResource(R.drawable.person_press);
tv_person.setTextColor(getResources().getColor(R.color.bottom_press));
if (personFragment == null) {
personFragment = new PersonFragment();
transaction.add(R.id.layout_fragment, personFragment,"PERSON");
} else {
transaction.remove(personFragment);
personFragment = new PersonFragment();
transaction.add(R.id.layout_fragment, personFragment);
//transaction.show(personFragment);
}
break;
default:
break;
}
transaction.commit();
}
private void clearSelection() {
iv_page.setBackgroundResource(R.drawable.page);
tv_page.setTextColor(getResources().getColor(R.color.bottom_font_color));
iv_information.setBackgroundResource(R.drawable.information);
tv_information.setTextColor(getResources().getColor(R.color.bottom_font_color));
iv_job.setBackgroundResource(R.drawable.job);
tv_job.setTextColor(getResources().getColor(R.color.bottom_font_color));
iv_search.setBackgroundResource(R.drawable.search);
tv_search.setTextColor(getResources().getColor(R.color.bottom_font_color));
iv_person.setBackgroundResource(R.drawable.person);
tv_person.setTextColor(getResources().getColor(R.color.bottom_font_color));
}
private void hideFragments(FragmentTransaction transaction) {
if (pageFragment != null) {
transaction.hide(pageFragment);
}
if (informationFragment != null) {
transaction.hide(informationFragment);
}
if (jobFragment != null) {
transaction.hide(jobFragment);
}
if (searchFragment != null) {
transaction.hide(searchFragment);
}
if (personFragment != null) {
transaction.hide(personFragment);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.layout_page:
setTabSelection(0);
break;
case R.id.layout_information:
setTabSelection(1);
break;
case R.id.layout_job:
setTabSelection(2);
break;
case R.id.layout_search:
setTabSelection(3);
break;
case R.id.layout_person:
setTabSelection(4);
break;
}
}
}