Android RadioGroup Fragment Viewpager FragmentPagerAdapter 去哪网Fragment嵌套
RadioGroup中的各个选择器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="false" android:drawable="@color/bg_checked"/> 3 <item android:state_checked="true" android:drawable="@color/bg_normal"/> 4 </selector>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:drawable="@mipmap/icon_favor_press"/> 3 <item android:state_checked="false" android:drawable="@mipmap/icon_favor_normal"/> 4 </selector>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:drawable="@mipmap/icon_home_press"/> 3 <item android:state_checked="false" android:drawable="@mipmap/icon_home_normal"/> 4 </selector>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:drawable="@mipmap/icon_order_press"/> 3 <item android:state_checked="false" android:drawable="@mipmap/icon_order_normal"/> 4 </selector>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:drawable="@mipmap/icon_setting_press"/> 3 <item android:state_checked="false" android:drawable="@mipmap/icon_setting_normal"/> 4 </selector>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:drawable="@mipmap/icon_uc_press"/> 3 <item android:state_checked="false" android:drawable="@mipmap/icon_uc_normal"/> 4 </selector>
各个Fragment
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class FavorFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.BLUE); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("收藏"); 16 tv.setTextSize(50); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class OrderFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.RED); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("订单"); 16 tv.setTextSize(50); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class SettingFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.WHITE); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("设置"); 16 tv.setTextSize(50); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class UCFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.YELLOW); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("个人中心"); 16 tv.setTextSize(50); 17 } 18 }
首页中还要显示一个ViewPager,一页机票,一页景点,可以左右滑动,自定义指示器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class JiPiaoFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.DKGRAY); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("机票"); 16 tv.setTextSize(50); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class JingDianFragment extends Fragment{ 2 3 @Nullable 4 @Override 5 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6 View layout = inflater.inflate(android.R.layout.simple_list_item_1,null); 7 return layout; 8 } 9 10 @Override 11 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 view.setBackgroundColor(Color.MAGENTA); 14 TextView tv = (TextView) view.findViewById(android.R.id.text1); 15 tv.setText("景点"); 16 tv.setTextSize(50); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class HomeFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener { 2 3 List<Fragment> mList = new ArrayList<>(); 4 5 @Override 6 public void onCreate(@Nullable Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 mList.add(new JiPiaoFragment()); 9 mList.add(new JingDianFragment()); 10 } 11 12 @Nullable 13 @Override 14 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 15 View layout = inflater.inflate(R.layout.fragment_home,null); 16 return layout; 17 } 18 19 RadioGroup home_radioGroup; 20 View view_indicator; 21 ViewPager home_viewPager; 22 23 @Override 24 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 25 super.onViewCreated(view, savedInstanceState); 26 27 home_radioGroup = (RadioGroup) view.findViewById(R.id.home_radioGroup); 28 view_indicator = view.findViewById(R.id.view_indicator); 29 home_viewPager = (ViewPager) view.findViewById(R.id.home_viewPager); 30 31 //在Fragment中设置FragmentPagerAdapter必须使用getChildFragmentManager 32 FragmentManager fm = getChildFragmentManager(); 33 34 home_viewPager.setAdapter(new MyFragmentPagerAdapter(fm,mList)); 35 home_radioGroup.setOnCheckedChangeListener(this); 36 home_viewPager.addOnPageChangeListener(this); 37 //默认 38 home_radioGroup.check(R.id.jipiao); 39 initIndicator(); 40 } 41 42 int width; 43 public void initIndicator(){ 44 //指示器长度分一半 45 width = getResources().getDisplayMetrics().widthPixels / 2; 46 FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.MATCH_PARENT); 47 view_indicator.setLayoutParams(params); 48 } 49 50 51 @Override 52 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 53 FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view_indicator.getLayoutParams(); 54 int left = (int) ((position + positionOffset)*width); 55 params.setMargins(left,0,0,0); 56 view_indicator.setLayoutParams(params); 57 } 58 59 @Override 60 public void onPageSelected(int position) { 61 home_radioGroup.check(position == 0?R.id.jipiao:R.id.jingdian); 62 } 63 64 @Override 65 public void onPageScrollStateChanged(int state) { 66 67 } 68 69 @Override 70 public void onCheckedChanged(RadioGroup group, int checkedId) { 71 home_viewPager.setCurrentItem(checkedId == R.id.jipiao? 0 : 1); 72 } 73 }
由于需要创建很多Fragment实例,这里用工厂模式或者反射方式创建
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class FragmentUtils { 2 3 //工厂模式 4 5 public static final Fragment newInstance(int type){ 6 7 switch (type){ 8 case 0: 9 return new FavorFragment(); 10 case 1: 11 return new OrderFragment(); 12 case 2: 13 return new HomeFragment(); 14 case 3: 15 return new UCFragment(); 16 case 4: 17 return new SettingFragment(); 18 } 19 return null; 20 } 21 22 //反射方式 23 public static final String[] classNames = {"Favor","Order","Home","UC","Setting"}; 24 25 public static final Fragment getInstance(int type){ 26 try { 27 //通过一个类的字符串对象,转换成一个class类类型 28 Class className = Class.forName("com.example.lesson10_viewpager_fragmentpageradapter_childfragment."+classNames[type]+"Fragment"); 29 //通过类的类型去创建实例,必须有无参构造方法,并公开 30 return (Fragment) className.newInstance(); 31 32 } catch (ClassNotFoundException e) { 33 e.printStackTrace(); 34 } catch (InstantiationException e) { 35 e.printStackTrace(); 36 } catch (IllegalAccessException e) { 37 e.printStackTrace(); 38 } 39 40 return null; 41 } 42 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ 2 3 List<Fragment> mList; 4 public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList) { 5 super(fm); 6 this.mList = mList; 7 } 8 9 @Override 10 public Fragment getItem(int position) { 11 return mList.get(position); 12 } 13 14 @Override 15 public int getCount() { 16 return mList.size(); 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { 2 3 RadioGroup radioGroup; 4 Fragment[] fragments = new Fragment[5]; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 12 radioGroup.setOnCheckedChangeListener(this); 13 //默认显示首页 14 showFragment(2); 15 radioGroup.check(R.id.rb_home); 16 17 } 18 int currIndex = -1; 19 20 public void showFragment(int index){ 21 if(index==currIndex){ 22 return; 23 } 24 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 25 if(currIndex != -1){ 26 ft.hide(fragments[currIndex]); 27 } 28 29 if(fragments[index]==null){ 30 //创建,用工厂类 31 fragments[index] = FragmentUtils.newInstance(index); 32 ft.add(R.id.fl_content,fragments[index]); 33 }else{ 34 ft.show(fragments[index]); 35 } 36 37 currIndex = index; 38 ft.commit(); 39 40 } 41 42 @Override 43 public void onCheckedChanged(RadioGroup group, int checkedId) { 44 View view = group.findViewById(checkedId); 45 String tag = view.getTag().toString(); 46 showFragment(Integer.parseInt(tag)); 47 } 48 49 }