主页底部导航栏
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>