Android--使用反射和FragmentTabHost实现底部导航

       本篇文章将继续总结实现底部导航的另外两种方式,RadioGroup+反射以及FragmentTabHost的形式实现底部导航,而其中反射是我在开发中最常用的一种方式,另外RadioGroup和Tablayout的方式实现底部导航在上一篇博客已经总结过,有需要使用另两种方式的可以看另一篇博客:

           博客地址:http://blog.csdn.net/huohao_blogs/article/details/72638319

一、使用RadioGroup+Fragment+反射实现底部导航

       ReflectActivity的资源文件(和第一种方式的内容是一样的,图片选择器和文字选择器参考另一篇博客):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.linktestproject.RadioGroupActivity">

    <FrameLayout
        android:id="@+id/fl_reflect_show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></FrameLayout>
    <RadioGroup
        android:id="@+id/rg_reflect_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom">
        <RadioButton
            android:id="@+id/rb_reflect_homepage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/homepage"
            android:text="@string/homepage"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_reflect_subscription"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/subscription"
            android:text="@string/subscription"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_reflect_find"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/find"
            android:text="@string/find"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
        <RadioButton
            android:id="@+id/rb_reflect_mine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:drawableTop="@drawable/mine"
            android:text="@string/mine"
            android:button="@null"
            android:drawablePadding="5dp"
            android:textColor="@drawable/radio_button_selector"
            />
    </RadioGroup>
</LinearLayout>
然后进入ReflectActivity中:
public class ReflectActivity extends AppCompatActivity {

    private RadioGroup mRadioGroup;
    private FragmentManager mManager;
    private Fragment mCurrentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reflect);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                Class<? extends Fragment>clazz=null;
                switch(checkedId){
                    case R.id.rb_reflect_homepage:
                        clazz= HomepageFragment.class;
                        break;
                    case R.id.rb_reflect_subscription:
                        clazz= SubscriptionFragment.class;
                        break;
                    case R.id.rb_reflect_find:
                        clazz= FindFragment.class;
                        break;
                    case R.id.rb_reflect_mine:
                        clazz= MineFragment.class;
                        break;
                }
                 showAndHide(R.id.fl_reflect_show,clazz);
            }
        });

    }
    private void initData() {
        mManager=getSupportFragmentManager();
        //设置默认显示的
        mRadioGroup.check(R.id.rb_reflect_homepage);
        showAndHide(R.id.fl_reflect_show,HomepageFragment.class);
    }

    private void showAndHide(int contentId,Class<? extends Fragment>clazz) {
        //判断当前的fragment和需要替换的fragment是不是同一个
        if(mCurrentFragment!=null&&mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName())){
            return;
        }
        FragmentTransaction transaction=mManager.beginTransaction();
        Fragment fragment=null;
        //判断fragment有没有添加过
        try {
        Fragment  fragmentByTag=mManager.findFragmentByTag(clazz.getSimpleName());
        if(fragment!=null){
            //显示需要的fragment
            transaction.show(fragmentByTag);
            //隐藏当前的fragment
            transaction.hide(mCurrentFragment);
            //记录当前显示的fragment
            mCurrentFragment=fragmentByTag;
        }else{
            //通过无参的 公开的构造函数来创建fragment实例
            fragment=clazz.newInstance();  //需要try/catch声明异常
            //当前的fragment的没有添加过
            //把fragment添加到fragmentManager里面
            transaction.add(contentId,fragment,clazz.getSimpleName());
            //隐藏当前的fragment
            if(mCurrentFragment!=null){
                transaction.hide(mCurrentFragment);
            }
            //记录当前的fragment是哪个
            mCurrentFragment=fragment;
         }
         transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    private void initView() {
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
        mRadioGroup= (RadioGroup) findViewById(R.id.rg_reflect_navigation);
    }
}
实现的结果如下图所示:

  

二、使用FragmentTabHost实现底部导航

首先资源文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.linktestproject.TabHostActivity">
    <FrameLayout
        android:id="@+id/fl_tabhost_show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/fth_tabhost_nativation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!--下边可加可不加google说最好加上--->
    <FrameLayout
            android:id="@+id/fl_tabhost_tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp">
        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

为tab添加的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
       android:id="@+id/iv_tab_navigation"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center" />
    <TextView
        android:id="@+id/tv_tab_navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_gravity="center"
        android:textColor="@drawable/tab_text_color"/>
</LinearLayout>
图片资源的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@mipmap/tab4_down"></item>
    <item android:state_selected="false" android:drawable="@mipmap/tab4"></item>
</selector>
Textview的字体颜色选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/textChecked"></item>
    <item android:state_selected="false" android:color="@color/textUnChecked"></item>
</selector>
最后就是Activity中的代码:
public class TabHostActivity extends AppCompatActivity {

    private Class []mFragments;
    private List<String>mTitles;
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private  TextView mTextView;
    private ImageView mImageView;
    private View mView;
    private int [] mImageViews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host);
        initView();
        initData();
        setData();
    }
    private void setData() {
        //为FragmentTabHost来填充Fragment
          mTabHost.setup(this,getSupportFragmentManager(),R.id.fl_tabhost_show);
        for (int i = 0; i <mTitles.size() ; i++) {
            TabHost.TabSpec  tabSpec=mTabHost.newTabSpec(mTitles.get(i)).setIndicator(getTabItemVIew(i));
            mTabHost.addTab(tabSpec,mFragments[i],null);
            //取消默认的Tab间的竖线显示,不加这一句会导致每个导航按钮之间存在一条竖直的分割线
            mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        }
    }
    private void initData() {
        mTitles=new ArrayList<>();
        mTitles.add("首页");
        mTitles.add("订阅");
        mTitles.add("发现");
        mTitles.add("我的");
        mFragments=new Class[]{HomepageFragment.class,SubscriptionFragment.class,FindFragment.class,MineFragment.class};
        mImageViews=new int[]{R.drawable.hometablayout,R.drawable.subtablayout,R.drawable.findtablayout,R.drawable.minetablayout};
    }
    private void initView() {
         ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
         mTabHost= (FragmentTabHost) findViewById(R.id.fth_tabhost_nativation);
         mInflater=LayoutInflater.from(this);
    }
    private View getTabItemVIew(int index){
        mView=mInflater.inflate(R.layout.tab_indicator,null);
        mImageView= (ImageView) mView.findViewById(R.id.iv_tab_navigation);
        mImageView.setImageResource(mImageViews[index]);
        mTextView= (TextView) mView.findViewById(R.id.tv_tab_navigation);
        mTextView.setText(mTitles.get(index));
        return mView;
    }
}
最后实现的结果如下图:
 
    


       通过两篇博客总结了四种实现底部导航的方式,到这四种方式全部总结完毕。既是将自己用过的四种方式做一下总结,也是为需要的人提供一些帮助。

    源码地址:http://download.csdn.net/detail/huohao_blogs/9852863
    Github下载:https://github.com/huohaoliz/BottomNavigation











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值