源码:https://github.com/bkhacker/business
欢迎star
2-1 首页框架—搭建铺垫
1 2 3 4 5
add、replace(先remove、再add)、remove、hide、show、detach
注意包和类和布局文件的命名规则:
首先看到ImoocApplication,它是整个程序的入口,写完在清单文件声明。
接下来创建Fragment,他们都继承BaseFragment.
Fragment和activity都要做一个base,方便公共部分的修改。
这个activity,我们在开发之前直接把所有控件初始化,这样开发比较有效率。
public class HomeActivity extends BaseActivity implements View.OnClickListener {
//Fragment
private android.app.FragmentManager fm;
private HomeFragment mHomeFragment;
private Fragment mCommonFragmentOne;
private MessageFragment mMessageFragment;
private MineFragment mMineFragment;
private Fragment mCurrent;
//底部导航布局
private RelativeLayout mHomeLayout;//首页
private RelativeLayout mPondLayout;//鱼塘
private RelativeLayout mMessageLayout;//消息
private RelativeLayout mMineLayout;//我的
//底部导航图片切换
private TextView mHomeView;
private TextView mPondView;
private TextView mMessageView;
private TextView mMineView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_layout);
//初始化我们页面中的所有控件
initView();
//添加默认显示的fragment
mHomeFragment = new HomeFragment();
fm = getFragmentManager();//获得FM
FragmentTransaction fragmentTransaction = fm.beginTransaction();
//replace 先remove再add
fragmentTransaction.replace(R.id.content_layout, mHomeFragment);
fragmentTransaction.commit();
}
//初始化控件 设置监听事件
private void initView() {
//底部导航的初始化和事件监听
mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
mHomeLayout.setOnClickListener(this);
mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
mPondLayout.setOnClickListener(this);
mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
mMessageLayout.setOnClickListener(this);
mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
mMineLayout.setOnClickListener(this);
//底部导航图片控件,用于点击切换选中图片
mHomeView = (TextView) findViewById(R.id.home_image_view);
mPondView = (TextView) findViewById(R.id.fish_image_view);
mMessageView = (TextView) findViewById(R.id.message_image_view);
mMineView = (TextView) findViewById(R.id.mine_image_view);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
}
//用来隐藏具体的Fragment
private void hideFragment(Fragment fragment, FragmentTransaction ft) {
if (fragment != null) {
ft.hide(fragment);
}
}
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fm.beginTransaction();
switch (v.getId()) {
//首页
case R.id.home_layout_view:
mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
mMineView.setBackgroundResource(R.drawable.comui_tab_person);
//隐藏其他两个Fragment
hideFragment(mMessageFragment, fragmentTransaction);
hideFragment(mMineFragment, fragmentTransaction);
//将我们的HomeFragment显示出来
if (mHomeFragment == null) {
mHomeFragment = new HomeFragment();
fragmentTransaction.add(R.id.content_layout, mHomeFragment);
} else {
//已经创建过了
fragmentTransaction.show(mHomeFragment);
}
break;
//消息
case R.id.message_layout_view:
mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMineView.setBackgroundResource(R.drawable.comui_tab_person);
//隐藏其他两个Fragment
hideFragment(mHomeFragment, fragmentTransaction);
hideFragment(mMineFragment, fragmentTransaction);
//将我们的mMessageFragment显示出来
if (mMessageFragment == null) {
mMessageFragment = new MessageFragment();
fragmentTransaction.add(R.id.content_layout, mMessageFragment);
} else {
//已经创建过了
fragmentTransaction.show(mMessageFragment);
}
break;
//我的
case R.id.mine_layout_view:
mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected);
mHomeView.setBackgroundResource(R.drawable.comui_tab_home);
mPondView.setBackgroundResource(R.drawable.comui_tab_pond);
mMessageView.setBackgroundResource(R.drawable.comui_tab_message);
//隐藏其他两个Fragment
hideFragment(mMessageFragment, fragmentTransaction);
hideFragment(mHomeFragment, fragmentTransaction);
//将我们的HomeFragment显示出来
if (mMineFragment == null) {
mMineFragment = new MineFragment();
fragmentTransaction.add(R.id.content_layout, mMineFragment);
} else {
//已经创建过了
fragmentTransaction.show(mMineFragment);
}
break;
}
fragmentTransaction.commit();
}
}
完成一些点击的逻辑和fragment的切换
必知必会
网友评论