Android网易顶部导航栏(用ActivityGroup实现动态加载)
16lz
2021-01-26
随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候想网易新闻要显示的内容太多,而且又想在主页面全部显示出来,所以有加了顶部导航栏,但是android这样的移动设备内存是受限的,那么多界面缓存到内存中,很容易导致内存溢出,这个是比较致命的,所以不得不考虑。虽然我在之前也做过网易的顶部导航栏但是哪种方式并不好,就像使用viewpager做一些复杂的界面由于图片占用内存过多,很容易导致内存溢出,学习了今天的内容大家做一下对比相信就有所体会。
具体宽度样式大家可以自己调节,然后看一下核心类:
这里继承了ActivityGroup,没有使用过的朋友从百度搜索下就明白了。
先看一下今天要实现的效果:
至于顶部导航的具体要用到的图片和布局大家自己调整。
由于前面已经介绍了底部菜单栏了,所以一些重复性的代码就不贴上来了,最后我也会把下载地址贴上大家有兴趣自行下载。
首先看一些顶部导航栏的布局文件:
[html] view plain copy- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <includelayout="@layout/head"/>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <RadioGroup
- android:id="@+id/add_tab_group"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:paddingTop="6dp"
- android:paddingBottom="6dp"
- android:background="@drawable/big_button_up"
- android:orientation="horizontal"
- >
- <RadioButton
- android:id="@+id/main_tab_addExam"
- style="@style/MMTabButton1"
- android:layout_weight="1.0"
- android:checked="true"
- android:text="添加考试"/>
- <RadioButton
- android:id="@+id/main_tab_myExam"
- style="@style/MMTabButton1"
- android:layout_weight="1.0"
- android:text="我的考试"/>
- <RadioButton
- android:id="@+id/main_tab_message"
- style="@style/MMTabButton1"
- android:layout_weight="1.0"
- android:text="我的通知"/>
- <RadioButton
- android:id="@+id/main_tab_testing"
- style="@style/MMTabButton1"
- android:layout_weight="1.0"
- android:text="测试"/>
- <RadioButton
- android:id="@+id/main_tab_settings"
- style="@style/MMTabButton1"
- android:layout_weight="1.0"
- android:text="设置"/>
- </RadioGroup>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/container"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- </LinearLayout>
- </LinearLayout>
具体宽度样式大家可以自己调节,然后看一下核心类:
[html] view plain copy
- importandroid.app.ActivityGroup;
- importandroid.app.AlertDialog;
- importandroid.app.LocalActivityManager;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.view.animation.Animation;
- importandroid.view.animation.AnimationUtils;
- importandroid.widget.Button;
- importandroid.widget.LinearLayout;
- importandroid.widget.RadioGroup;
- importandroid.widget.TextView;
- importandroid.widget.RadioGroup.OnCheckedChangeListener;
- publicclassAddExamActivityextendsActivityGroup{
- protectedButtonbtn_leftTop,btn_rightTop;
- protectedTextViewtv_head;
- privatestaticLocalActivityManagermanager;
- privateRadioGroupradioGroup;
- privatestaticLinearLayoutcontainer;
- publicstaticContextcontext;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.addexam);
- context=this;
- initHead();
- manager=getLocalActivityManager();
- container=(LinearLayout)findViewById(R.id.container);
- radioGroup=(RadioGroup)this.findViewById(R.id.add_tab_group);
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_0",
- newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
- @Override
- publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
- //TODOAuto-generatedmethodstub
- switch(checkedId){
- caseR.id.main_tab_addExam://添加考试
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_0",
- newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- break;
- caseR.id.main_tab_myExam://我的考试
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_1",
- newIntent(context,MyMessageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- break;
- caseR.id.main_tab_message://我的通知
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_2",
- newIntent(context,SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- break;
- caseR.id.main_tab_testing://测试
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_3",
- newIntent(context,TestingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- break;
- caseR.id.main_tab_settings://设置
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_4",
- newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- break;
- default:
- //tabHost.setCurrentTabByTag("我的考试");
- break;
- }
- }
- });
- }
- publicstaticvoidchangeTo(){
- AnimationslideLeftIn=AnimationUtils.loadAnimation(context,R.anim.slide_bottom_in_no_alpha);
- container.removeAllViews();
- container.addView(manager.startActivity(
- "PAGE_4",
- newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
- .getDecorView());
- container.startAnimation(slideLeftIn);
- }
- protectedvoidinitHead(){
- btn_leftTop=(Button)findViewById(R.id.btn_leftTop);
- btn_rightTop=(Button)findViewById(R.id.btn_rightTop);
- tv_head=(TextView)findViewById(R.id.tv_head);
- btn_leftTop.setVisibility(View.INVISIBLE);
- tv_head.setText("添加考试");
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- //TODOAuto-generatedmethodstub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- AlertDialog.Builderbuilder=newAlertDialog.Builder(getParent());
- builder.setMessage("你确定退出吗?")
- .setCancelable(false)
- .setPositiveButton("确定",
- newDialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,
- intid){
- finish();
- System.exit(0);
- }
- })
- .setNegativeButton("返回",
- newDialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,
- intid){
- dialog.cancel();
- }
- });
- AlertDialogalert=builder.create();
- alert.show();
- returntrue;
- }
- returnsuper.onKeyDown(keyCode,event);
- }
- }
这里继承了ActivityGroup,没有使用过的朋友从百度搜索下就明白了。
使用了LocalActivityManager启动子activity,这里Context和LinearLayout使用了static静态的,这是因为变态的需求使我不得不这样做,希望大家不要把这两个变量设置成static的,因为static的生命周期很长特别是Context不要设置成static,这样的话当前的activity很难被销毁的。其实使用tabhost完全可以实现,但是为什么没使用tabhost的我相信大家都明白,如果不考虑内存的话我也会使用,哈哈!
更多相关文章
- Android(安卓)Developers:高效的加载大的位图
- 在android开发中应该如何管理内存或者是在开发过程中应该注意哪
- android LruCache的使用 (本地缓存+内存缓存)
- Android(安卓)图片内存缓存
- Android导航栏滑动折叠效果
- Android:Handler,内部类导致的可能内存泄露
- 《Pro Android(安卓)Graphics》读书笔记之第三节
- BottomNavigationView+ViewPager打造底部导航栏
- 内存优化二