写出完整的Android studio通过点击底部导航栏radiogroup有四个radiobutton切换对应viewpager包含四个fragment的代码,并逐句添加注释
时间: 2024-02-16 20:05:19 浏览: 116
好的,以下是完整的 Android Studio 代码,包含点击底部导航栏 RadioGroup 有四个 RadioButton 切换对应 ViewPager 包含四个 Fragment 的实现:
```
// 首先在布局文件中添加一个 RadioGroup 和四个 RadioButton
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/navigation_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/nav_home">
<RadioButton
android:id="@+id/nav_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Home" />
<RadioButton
android:id="@+id/nav_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<RadioButton
android:id="@+id/nav_notifications"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Notifications" />
<RadioButton
android:id="@+id/nav_profile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Profile" />
</RadioGroup>
<androidx.viewpager.widget.ViewPager
android:id="@+id/navigation_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/navigation_radio_group" />
</RelativeLayout>
```
接下来,在对应的 Activity 中找到 RadioGroup 和 ViewPager 并设置它们的监听器:
```
public class MainActivity extends AppCompatActivity {
private RadioGroup mNavigationRadioGroup;
private ViewPager mNavigationViewPager;
private NavigationPagerAdapter mNavigationPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到 RadioGroup 和 ViewPager
mNavigationRadioGroup = findViewById(R.id.navigation_radio_group);
mNavigationViewPager = findViewById(R.id.navigation_view_pager);
// 设置 ViewPager 的 Adapter
mNavigationPagerAdapter = new NavigationPagerAdapter(getSupportFragmentManager());
mNavigationViewPager.setAdapter(mNavigationPagerAdapter);
// 设置 RadioGroup 的监听器
mNavigationRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.nav_home:
// 切换到 HomeFragment
mNavigationViewPager.setCurrentItem(0);
break;
case R.id.nav_search:
// 切换到 SearchFragment
mNavigationViewPager.setCurrentItem(1);
break;
case R.id.nav_notifications:
// 切换到 NotificationsFragment
mNavigationViewPager.setCurrentItem(2);
break;
case R.id.nav_profile:
// 切换到 ProfileFragment
mNavigationViewPager.setCurrentItem(3);
break;
}
}
});
}
// 定义 ViewPager 的 Adapter
private class NavigationPagerAdapter extends FragmentPagerAdapter {
public NavigationPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// 返回 HomeFragment
return new HomeFragment();
case 1:
// 返回 SearchFragment
return new SearchFragment();
case 2:
// 返回 NotificationsFragment
return new NotificationsFragment();
case 3:
// 返回 ProfileFragment
return new ProfileFragment();
default:
return null;
}
}
@Override
public int getCount() {
return 4; // 四个 Fragment
}
}
}
```
最后,定义四个 Fragment,并在对应的布局文件中添加对应的 UI:
```
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
// 添加 HomeFragment 的 UI
return view;
}
}
public class SearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
// 添加 SearchFragment 的 UI
return view;
}
}
public class NotificationsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notifications, container, false);
// 添加 NotificationsFragment 的 UI
return view;
}
}
public class ProfileFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
// 添加 ProfileFragment 的 UI
return view;
}
}
```
以上就是完整的 Android Studio 代码,包含点击底部导航栏 RadioGroup 有四个 RadioButton 切换对应 ViewPager 包含四个 Fragment 的实现,希望对你有帮助!
阅读全文