一、整体代码
二、导航栏实现步骤
1.定义主布局页面,分成上下两部分,上部分为RadioButton单选按钮,下部分的页面为片段
2.创建一个布局文件为该页面存放的容器,才能将每个片段放进去。
3.定义一个片段的子类,继承该片段,放入容器和绑定容器对应的id,每个页面就可以通过继承片段的子类,可增加代码的复用性。
4.定义页面片段,绑定对应的布局,通过继承HomeFragment,就可以用HomeFragment的方法
5.写出每一个片段页面对应的内容布局
6.添加剩下的布局页面和对应的java文件,绑定对应的布局文件和填入对应的布局内容
7.在主java逻辑文件中实现点击切换,显示和隐藏
(1)初始化控件<br /><br /> (2)添加事务,默认片段隐藏为true,将所有片段先隐藏<br /><br /> (3)开启事务,若页面为空,将页面添加进去,则显示该页面,否则页面为空的话就隐藏<br /><br /> (4)进行点击时间的判断,当点击的时候,就为false,即为显示,判断页面如果为空,创建实例,将实例添加进去,隐藏该页面,如果不为空,则显示该页面。<br /><br />
三、整体代码
1.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"
android:orientation="vertical"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rg_home"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:gravity="end">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="光峰智慧党建"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不忘初心 牢记使命"
android:textSize="12sp"/>
</LinearLayout>
<RadioButton
android:id="@+id/rb_home"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_news"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="时政要闻"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_book"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="书记面对面"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_party"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="党政专题"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_lesson"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="三会一课"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_stack"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="组工状态"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
<RadioButton
android:id="@+id/rb_will"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:text="两学一做"
android:textSize="12sp"
android:textColor="#d9000000"
android:button="@null"
android:gravity="center"/>
</RadioGroup>
<FrameLayout
android:id="@+id/fra_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
2.layout_home.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_home"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
3.HomeFragment文件代码
package com.example.homepagefragment.home;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.homepagefragment.R;
public abstract class HomeFragment extends Fragment {
private View view;
@Nullable
@Override
public View onCreateView(@Nullable LayoutInflater inflater,
@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
view=inflater.inflate(R.layout.layout_home,container,false);
((ViewGroup)view.findViewById(R.id.fl_home))
.addView(getLayoutInflater().inflate(getLayoutId(),null));
return view;
}
protected abstract int getLayoutId();
}
4.HomeFragmentOne片段文件代码
package com.example.homepagefragment.fragment;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.example.homepagefragment.R;
import com.example.homepagefragment.home.HomeFragment;
public class HomeFragmentOne extends HomeFragment {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
protected int getLayoutId(){
return R.layout.fra_home_one;
}
@Override
public void onViewCreated(@Nullable View view,@Nullable Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
}
}
5.fra_home_one.xml片段布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="首页"
android:textSize="50dp"
android:gravity="center"
/>
</FrameLayout>
6.MainActivity主文件代码
package com.example.homepagefragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.homepagefragment.fragment.BookFragmentThree;
import com.example.homepagefragment.fragment.HomeFragmentOne;
import com.example.homepagefragment.fragment.LessonFragmentFive;
import com.example.homepagefragment.fragment.NewsFragmentTwo;
import com.example.homepagefragment.fragment.PartyFragmentFour;
import com.example.homepagefragment.fragment.StackFragmentSix;
import com.example.homepagefragment.fragment.WillFragmentSeven;
import com.example.homepagefragment.home.HomeFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private FrameLayout fra_home;
private RadioGroup rg_home;
private RadioButton rb_home;
private RadioButton rb_news;
private RadioButton rb_book;
private RadioButton rb_party;
private RadioButton rb_lesson;
private RadioButton rb_stack;
private RadioButton rb_will;
private List<Fragment> list=new ArrayList<>();
private HomeFragmentOne homeFragmentOne;
private NewsFragmentTwo newsFragmentTwo;
private BookFragmentThree bookFragmentThree;
private PartyFragmentFour partyFragmentFour;
private LessonFragmentFive lessonFragmentFive;
private StackFragmentSix stackFragmentSix;
private WillFragmentSeven willFragmentSeven;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fra_home=findViewById(R.id.fra_home);
rg_home=findViewById(R.id.rg_home);
rb_home=findViewById(R.id.rb_home);
rb_news=findViewById(R.id.rb_news);
rb_book=findViewById(R.id.rb_book);
rb_party=findViewById(R.id.rb_party);
rb_lesson=findViewById(R.id.rb_lesson);
rb_stack=findViewById(R.id.rb_stack);
rb_will=findViewById(R.id.rb_will);
initView();
}
private void initView(){
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
rg_home.check(R.id.rb_home);
homeFragmentOne= new HomeFragmentOne();
list.add(homeFragmentOne);
hideOthersFragment(homeFragmentOne,true);
rg_home.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb_home:
hideOthersFragment(homeFragmentOne,false);
break;
case R.id.rb_news:
if (newsFragmentTwo==null){
newsFragmentTwo=new NewsFragmentTwo();
list.add(newsFragmentTwo);
hideOthersFragment(newsFragmentTwo,true);
}else{
hideOthersFragment(newsFragmentTwo,false);
}
break;
case R.id.rb_book:
if (bookFragmentThree==null){
bookFragmentThree=new BookFragmentThree();
list.add(bookFragmentThree);
hideOthersFragment(bookFragmentThree,true);
}else{
hideOthersFragment(bookFragmentThree,false);
}
break;
case R.id.rb_party:
if (partyFragmentFour==null){
partyFragmentFour=new PartyFragmentFour();
list.add(partyFragmentFour);
hideOthersFragment(partyFragmentFour,true);
}else{
hideOthersFragment(partyFragmentFour,false);
}
break;
case R.id.rb_lesson:
if (lessonFragmentFive==null){
lessonFragmentFive=new LessonFragmentFive();
list.add(lessonFragmentFive);
hideOthersFragment(lessonFragmentFive,true);
}else{
hideOthersFragment(lessonFragmentFive,false);
}
break;
case R.id.rb_stack:
if (stackFragmentSix==null){
stackFragmentSix=new StackFragmentSix();
list.add(stackFragmentSix);
hideOthersFragment(stackFragmentSix,true);
}else{
hideOthersFragment(stackFragmentSix,false);
}
break;
case R.id.rb_will:
if (willFragmentSeven==null){
willFragmentSeven=new WillFragmentSeven();
list.add(willFragmentSeven);
hideOthersFragment(willFragmentSeven,true);
}else{
hideOthersFragment(willFragmentSeven,false);
}
break;
}
}
});
}
public void hideOthersFragment(Fragment showFragment,boolean add){
fragmentTransaction=fragmentManager.beginTransaction();
if (add){
fragmentTransaction.add(R.id.fra_home,showFragment);
}
for (Fragment fragment:list){
if (showFragment.equals(fragment)){
fragmentTransaction.show(fragment);
}else{
fragmentTransaction.hide(fragment);
}
}
fragmentTransaction.commit();
}
}
7.设计图:
8.结果图: