Android Fragment实现导航栏

一、唠叨唠叨

人生第一次写博客,也不求有什么大神之作,就自己练练手,让自己学会表达,学会把自己心里的东西拿出来给人家看看,就像找女朋友一样,你不说出来自己多爱她,甚至不表现出来,哪里会有机会呢,是吧?让她自己发现你,爱上你,可能性好小的样子。。。

二、导航栏的实现方式
先上个图
这里写图片描述

像这种导航栏的实现方式有很多,有FragmentTabHost、RadioGroup+Fragment、ViewPager等等各种,他们都有自己的优势和方便之处,这是重点讲一下利用Fragment+button的方式来实现,也是我常用的方式,比较底层,可能没有其他方式简单,高手不要见笑。

三、实现步骤

废话不多说了,切重点。

1、 布局出我们想要的底部导航按钮、

这里写图片描述

 节省空间,这里只列出一项来
<RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" >

                <ImageView
                    android:id="@+id/img_main_home"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:clickable="false"
                    android:maxHeight="25dp"
                    android:maxWidth="25dp"
                    android:src="@drawable/bg_bottom_home_down" />

                <TextView
                    android:id="@+id/tv_main_home"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/img_main_home"
                    android:layout_centerHorizontal="true"
                    android:text="@string/txt_main_home"
                    android:textColor="@color/white"
                    android:textSize="9sp" />
            </RelativeLayout>

2 、 在Activity初始化的时候,设置首页卡项,也就是setTabSelection(0);

   这个方法的具体定义如下:
    public void setTabSelection(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (index) {
        case 0:
            if (fragment_Main == null) {
                fragment_Main = new Fragment_Main(this, getWindowManager());
                transaction.add(R.id.layout_main_replace, fragment_Main);
            } else {
                transaction.show(fragment_Main);
            }
            setBottomButtonBgFalse();
            img_main_home.setImageResource(R.drawable.bg_bottom_home_down);
            setBottomClickTrue();
            btn_main_home.setClickable(false);
            break;
        case 1:
            if (fragment_identify == null) {
                fragment_identify = new Fragment_Map(this, getWindowManager());
                transaction.add(R.id.layout_main_replace, fragment_identify);
            } else {
                transaction.show(fragment_identify);
            }
            setBottomButtonBgFalse();
            img_main_friend.setImageResource(R.drawable.bg_bottom_friend_down);
            setBottomClickTrue();
            btn_main_friend.setClickable(false);
            break;
        case 2:
            if (fragment_sort == null) {
                fragment_sort = new Fragment_Sort(this,
                        getWindowManager());
                transaction.add(R.id.layout_main_replace, fragment_sort);
            } else {
                transaction.show(fragment_sort);
            }
            setBottomButtonBgFalse();
            img_main_explore.setImageResource(R.drawable.bg_bottom_explore_down);
            setBottomClickTrue();
            btn_main_explore.setClickable(false);
            break;
        case 3:
            if (fragment_Chat == null) {
                fragment_Chat = new Fragment_Chat(this, getWindowManager());
                transaction.add(R.id.layout_main_replace, fragment_Chat);
            } else {
                transaction.show(fragment_Chat);
            }
            setBottomButtonBgFalse();
            img_main_chat.setImageResource(R.drawable.bg_bottom_chat_down);
            setBottomClickTrue();
            btn_main_chat.setClickable(false);
            break;
        default:
            break;
        }
        transaction.commit();
    }

3 、 然后再点击下部导航按钮的时候,我们就可以切换上方的Fragment

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_main_home:
            setTextColor();
            tvMainHome.setTextColor(Color.rgb(97, 164, 253));
            setTabSelection(0);
            break;
        case R.id.btn_main_friend:
            setTextColor();
            tvFind.setTextColor(Color.rgb(97, 164, 253));
            setTabSelection(1);
            break;
        case R.id.btn_main_explore:
            setTextColor();
            tvJolly.setTextColor(Color.rgb(97, 164, 253));
            setTabSelection(2);
            break;
        case R.id.btn_main_chat:
            setTextColor();
            tvChat.setTextColor(Color.rgb(97, 164, 253));
            setTabSelection(3);
            break;

        default:
            break;
        }
    }

其中颜色、背景以及fragment的显示隐藏的切换,偷懒了一下,让他们统一设置为初始状态,然后根据点击的位置,单独更换。

先统一

private void setTextColor() {
        tvMainHome.setTextColor(Color.WHITE);
        tvFind.setTextColor(Color.WHITE);
        tvJolly.setTextColor(Color.WHITE);
        tvChat.setTextColor(Color.WHITE);
    }

再单独

tvMainHome.setTextColor(Color.rgb(97, 164, 253));

显示与隐藏也是同样的道理,就不再累赘了。
其中Fragment的定义以及使用,我就不在详述了,还有欠缺的同学自行百度脑补。

四、还有话说

做开发的时间也挺长的了,虽然没有多牛的技术,但是身为一个数学人,感觉本科的数学还是没有白学,至少在开发中遇到的各种逻辑,大部分可以从容的应付。还有一句不吐不快的话,所谓高手是经验和逻辑都很强的人,那种只有经验,而连自己的经验都不知道是什么逻辑的人,算个屁的高手。(注:此句话没有任何攻击性,只是强调做人还是不要太傲的好。)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值