Fragment 和 RadioGroup实现底部导航
首先布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_mainmain" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ht.testxyapp.com.testapp.activity.MainmainActivity"> <FrameLayout android:id="@+id/fragmentContent" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <RadioButton android:id="@+id/rb1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首页1" /> <RadioButton android:id="@+id/rb2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首页2" /> <RadioButton android:id="@+id/rb3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首页3" /> </RadioGroup> </RelativeLayout>
然后Java代码:
public class MainmainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener { private View fragmentContent; private RadioButton rb1, rb2, rb3; private RadioGroup rg; private RbFragment1 fg1; private RbFragment2 fg2; private RbFragment3 fg3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mainmain); fragmentContent = findViewById(R.id.fragmentContent); rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(this); rb1 = (RadioButton) findViewById(R.id.rb1); rb2 = (RadioButton) findViewById(R.id.rb2); rb3 = (RadioButton) findViewById(R.id.rb3); rb1.setChecked(true); } private void hideFragments(FragmentTransaction action) { if (fg1 != null) { action.hide(fg1); } if (fg2 != null) { action.hide(fg2); } if (fg3 != null) { action.hide(fg3); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction sd = getSupportFragmentManager().beginTransaction(); hideFragments(sd); switch (checkedId) { case R.id.rb1: if(fg1==null){ fg1 = new RbFragment1(); sd.add(R.id.fragmentContent,fg1); }else{ sd.show(fg1); } break; case R.id.rb2: if (fg2 == null){ fg2 = new RbFragment2(); sd.add(R.id.fragmentContent,fg2); }else{ sd.show(fg2); } break; case R.id.rb3: if (fg3 == null){ fg3 = new RbFragment3(); sd.add(R.id.fragmentContent,fg3); }else{ sd.show(fg3); } break; } sd.commit(); } }
再然后是这几个fragment代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one,container,false); oneListView = (ListView) view.findViewById(R.id.oneListView); precessScroll(); initAnimation(); return view;