当前位置:   article > 正文

FragmentLayout案例

fragmentlayout

第一步  注册布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context=".activity.MainActivity">
  10. <FrameLayout
  11. android:id="@+id/frag"
  12. android:layout_width="match_parent"
  13. android:layout_height="0dp"
  14. android:layout_weight="1"
  15. />
  16. <RadioGroup
  17. android:id="@+id/group"
  18. android:layout_width="match_parent"
  19. android:layout_height="66dp"
  20. android:orientation="horizontal"
  21. >
  22. <RadioButton
  23. android:id="@+id/q1"
  24. android:layout_width="0dp"
  25. android:layout_weight="1"
  26. android:text="首页"
  27. android:button="@null"
  28. android:gravity="center"
  29. android:textSize="20dp"
  30. android:background="@drawable/selects"
  31. android:layout_height="66dp" />
  32. <RadioButton
  33. android:id="@+id/q2"
  34. android:layout_width="0dp"
  35. android:layout_weight="1"
  36. android:text="服务"
  37. android:button="@null"
  38. android:gravity="center"
  39. android:textSize="20dp"
  40. android:background="@drawable/selects"
  41. android:layout_height="66dp" />
  42. <RadioButton
  43. android:id="@+id/q3"
  44. android:layout_width="0dp"
  45. android:layout_weight="1"
  46. android:text="我的"
  47. android:button="@null"
  48. android:gravity="center"
  49. android:textSize="20dp"
  50. android:background="@drawable/selects"
  51. android:layout_height="66dp" />
  52. </RadioGroup>
  53. </LinearLayout>

第二步 核心功能实现

  1. package com.bwie.moni7.activity;
  2. import android.support.v4.app.FragmentManager;
  3. import android.support.v4.app.FragmentTransaction;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.FrameLayout;
  8. import android.widget.RadioGroup;
  9. import com.bwie.moni7.R;
  10. import com.bwie.moni7.frag.FragmentOne;
  11. import com.bwie.moni7.frag.FragmentThree;
  12. import com.bwie.moni7.frag.FragmentTwo;
  13. public class MainActivity extends AppCompatActivity {
  14. private FrameLayout frag;
  15. private FragmentManager manager;
  16. private FragmentOne one;
  17. private FragmentTwo two;
  18. private FragmentThree three;
  19. private FragmentTransaction transaction;
  20. private RadioGroup group;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. frag = findViewById(R.id.frag);
  26. group = findViewById(R.id.group);
  27. manager = getSupportFragmentManager();
  28. one = new FragmentOne();
  29. two = new FragmentTwo();
  30. three = new FragmentThree();
  31. transaction = manager.beginTransaction();
  32. transaction.add(R.id.frag,one);
  33. transaction.add(R.id.frag,two);
  34. transaction.add(R.id.frag,three);
  35. transaction.show(one).hide(two).hide(three);
  36. transaction.commit();
  37. group.check(group.getChildAt(0).getId());
  38. group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  39. @Override
  40. public void onCheckedChanged(RadioGroup group, int checkedId) {
  41. FragmentTransaction transaction1 = manager.beginTransaction();
  42. switch (checkedId){
  43. case R.id.q1:
  44. transaction1.show(one).hide(two).hide(three);
  45. break;
  46. case R.id.q2:
  47. transaction1.show(two).hide(one).hide(three);
  48. break;
  49. case R.id.q3:
  50. transaction1.show(three).hide(one).hide(two);
  51. break;
  52. }
  53. transaction1.commit();
  54. }
  55. });
  56. }
  57. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/279647
推荐阅读
相关标签
  

闽ICP备14008679号