主页底部导航栏

主页底部导航栏

MainActivity.java

package com.example.smartcity.activity.main;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.smartcity.R;
import com.example.smartcity.utils.FragmentUtils;

import java.util.ArrayList;

import static com.example.smartcity.utils.FragmentUtils.allServiceFragment;
import static com.example.smartcity.utils.FragmentUtils.homeFragment;
import static com.example.smartcity.utils.FragmentUtils.newsFragment;
import static com.example.smartcity.utils.FragmentUtils.personFragment;
import static com.example.smartcity.utils.FragmentUtils.poorFragment;

public class MainActivity extends AppCompatActivity {

    private ViewPager mMainViewpager;
    private RadioButton mHome;
    private RadioButton mAllServices;
    private RadioButton mPoor;
    private RadioButton mNews;
    private RadioButton mHomePerson;
    private RadioGroup mHomeRg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    public void initData(){
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(allServiceFragment);
        fragments.add(poorFragment);
        fragments.add(newsFragment);
        fragments.add(personFragment);

        //绑定
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        mMainViewpager.setAdapter(fragmentPagerAdapter);
        //页面改变事件
        mMainViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mHome.setBackground(getResources().getDrawable(R.drawable.tool_main_normal));
                mAllServices.setBackground(getResources().getDrawable(R.drawable.tool_main_normal));
                mPoor.setBackground(getResources().getDrawable(R.drawable.tool_main_normal));
                mNews.setBackground(getResources().getDrawable(R.drawable.tool_main_normal));
                mHomePerson.setBackground(getResources().getDrawable(R.drawable.tool_main_normal));

                switch (position){
                    case 0:
                        mHome.setBackground(getResources().getDrawable(R.drawable.tool_main_blue));
                        break;
                    case 1:
                        mAllServices.setBackground(getResources().getDrawable(R.drawable.tool_main_blue));
                        break;
                    case 2:
                        mPoor.setBackground(getResources().getDrawable(R.drawable.tool_main_blue));
                        break;
                    case 3:
                        mNews.setBackground(getResources().getDrawable(R.drawable.tool_main_blue));
                        break;
                    case 4:
                        mHomePerson.setBackground(getResources().getDrawable(R.drawable.tool_main_blue));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mHomeRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.home:
                        mMainViewpager.setCurrentItem(0,true);
                        break;
                    case R.id.all_services:
                        mMainViewpager.setCurrentItem(1,true);
                        break;
                    case R.id.poor:
                        mMainViewpager.setCurrentItem(2,true);
                        break;
                    case R.id.news:
                        mMainViewpager.setCurrentItem(3,true);
                        break;
                    case R.id.home_person:
                        mMainViewpager.setCurrentItem(4,true);
                        break;
                }
            }
        });
    }

    public void initView(){
        mHomeRg = (RadioGroup) findViewById(R.id.home_rg);
        mMainViewpager = (ViewPager) findViewById(R.id.main_viewpager);
        mHome = (RadioButton) findViewById(R.id.home);
        mAllServices = (RadioButton) findViewById(R.id.all_services);
        mPoor = (RadioButton) findViewById(R.id.poor);
        mNews = (RadioButton) findViewById(R.id.news);
        mHomePerson = (RadioButton) findViewById(R.id.home_person);
    }
}

activity_main.xml

<?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"
    tools:context=".activity.main.MainActivity"
    android:orientation="vertical">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

    <RadioGroup
        android:id="@+id/home_rg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="首 页"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/tool_main_normal"
            android:textColor="@color/white"
            android:textAlignment="center"
            android:drawableTop="@drawable/ic_home"/>

        <RadioButton
            android:id="@+id/all_services"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="全部服务"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/tool_main_normal"
            android:textColor="@color/white"
            android:drawableTop="@drawable/ic_home"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/poor"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="精准扶贫"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/tool_main_normal"
            android:textColor="@color/white"
            android:drawableTop="@drawable/ic_home"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/news"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="新 闻"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/tool_main_normal"
            android:textColor="@color/white"
            android:drawableTop="@drawable/ic_home"
            android:textAlignment="center"/>

        <RadioButton
            android:id="@+id/home_person"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="个人中心"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/tool_main_normal"
            android:textColor="@color/white"
            android:drawableTop="@drawable/ic_home"
            android:textAlignment="center"/>
    </RadioGroup>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玛丽莲.梦露

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值