基于数据挖掘的旅游推荐APP(二):主界面布局

        主界面布局通过Fragment实现,底部类似于Tab选项卡,效果如下:

                                                    

        底部导航栏是借鉴GitHub上的一个项目,不对,其实就是抄的。附上原地址链接,把库引进来直接用就行了。下面上代码:

    activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomNavigation"/>

</RelativeLayout>

    LaunchActivity.java

public class LaunchActivity  extends AppCompatActivity {
    private Fragment mFragmentLaunch;
    private Fragment mFragmentRecommend;
    private Fragment mFragmentRoute;
    private Fragment mFragmentUser;

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        initView();
        selectTab(0);
    }

    /**
     * 初始化布局
     */
    private void initView(){
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation);

        BottomNavigationItem bottomNavigationItem = new BottomNavigationItem
                ("热点", ContextCompat.getColor(this, R.color.firstColor), R.drawable.hotspot);
        BottomNavigationItem bottomNavigationItem1 = new BottomNavigationItem
                ("推荐", ContextCompat.getColor(this, R.color.secondColor), R.drawable.recommend);
        BottomNavigationItem bottomNavigationItem2 = new BottomNavigationItem
                ("路线", ContextCompat.getColor(this, R.color.thirdColor), R.drawable.route);
        BottomNavigationItem bottomNavigationItem3 = new BottomNavigationItem
                ("我的", ContextCompat.getColor(this, R.color.fourthColor), R.drawable.me);
        bottomNavigationView.addTab(bottomNavigationItem);
        bottomNavigationView.addTab(bottomNavigationItem1);
        bottomNavigationView.addTab(bottomNavigationItem2);
        bottomNavigationView.addTab(bottomNavigationItem3);

        bottomNavigationView.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {
            @Override
            public void onNavigationItemClick(int index) {
              selectTab(index);
            }
        });
    }


    /**
     * 进行选中Tab的处理
     * @param i
     */
    private void selectTab(int i){
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击的是热点的Tab时
            case 0:
                //如果热点对应的Fragment没有实例化,则进行实例化,并显示出来
                if (mFragmentLaunch == null) {
                    mFragmentLaunch = LaunchFragment.newInstance();
                    transaction.add(R.id.fragment_container, mFragmentLaunch);
                } else {
                    //如果热点对应的Fragment已经实例化,则直接显示出来
                    transaction.show(mFragmentLaunch);
                }
                break;
            case 1:
                if (mFragmentRecommend == null) {
                    mFragmentRecommend = RecommendFragment.newInstance();
                    transaction.add(R.id.fragment_container, mFragmentRecommend);
                } else {
                    transaction.show(mFragmentRecommend);
                }
                break;
            case 2:
                if (mFragmentRoute == null) {
                    mFragmentRoute =RouteFragment.newInstance();
                    transaction.add(R.id.fragment_container, mFragmentRoute);
                } else {
                    transaction.show(mFragmentRoute);
                }
                break;
            case 3:
                if (mFragmentUser == null) {
                    mFragmentUser =UserCenterFragment.newInstance();
                    transaction.add(R.id.fragment_container, mFragmentUser);
                } else {
                    transaction.show(mFragmentUser);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (mFragmentLaunch != null) {
            transaction.hide(mFragmentLaunch);
        }
        if (mFragmentRecommend != null) {
            transaction.hide(mFragmentRecommend);
        }
        if (mFragmentRoute != null) {
            transaction.hide(mFragmentRoute);
        }
        if (mFragmentUser != null) {
            transaction.hide(mFragmentUser);
        }
    }
}
        关于底部四个tab对应的fragment会在下面的博客中呈上。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值