经常要用到底部导航栏,所以就写了个demo,使用单例模式,高度模块化的思想。
里面设计一个导航栏的管理类和导航栏单独的布局文件(使用include标签加载到其他页面即可)
管理类:管理一个radiogroup
package com.example.manage;
import java.util.RandomAccess;
import com.example.reuse_headbotton.R;
import android.app.Activity;
import android.content.Context;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class BottomManager {
RadioGroup rg;
RadioButton news1, news2, news3, news4;
/*
* 单例模式,保证只存在一个类来处理所有调用
*/
public static BottomManager bottomManager=new BottomManager();
public static BottomManager getInstance() {
return bottomManager;
}
/*
* 找到组件,设置监听,需要手动调用
*/
public void initsDate(Activity ac) {
rg=(RadioGroup) ac.findViewById(R.id.id_radiogroup);
rg.setOnCheckedChangeListener(new LRadioGroupListener());
}
/*
* 给radiogroup设置的监听器,L是我自己添加的区分
* 在适配的界面进行你们的调用,或者传递某组数据都可以
*/
class LRadioGroupListener implements RadioGroup.OnCheckedChangeListener {
public void onCheckedChanged(RadioGroup group, int checkedId) {
System.out.println("打印当前点击的按钮"+checkedId);
switch (checkedId) {
case R.id.id_news:
//TODO:匹配上按钮1做的事
break;
case R.id.id_news2:
//TODO:匹配上按钮2做的事
break;
case R.id.id_news3:
//TODO::匹配上按钮3做的事
break;
case R.id.id_news4:
//TODO::匹配上按钮4做的事
break;
default:
break;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/id_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/id_news"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="新闻1" />
<RadioButton
android:id="@+id/id_news2"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="新闻2" />
<RadioButton
android:id="@+id/id_news3"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="新闻3" />
<RadioButton
android:id="@+id/id_news4"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="新闻4" />
</RadioGroup>
</LinearLayout>
本例源码可从http://pan.baidu.com/s/1qWPpDSC下载