package com.lbs.jsonjiexi; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import com.lbs.jsonjiexi.Fragments.Fragment1; import com.lbs.jsonjiexi.Fragments.Fragment2; import com.lbs.jsonjiexi.Fragments.Fragment3; import com.lbs.jsonjiexi.Fragments.Fragment4; import com.lbs.jsonjiexi.Fragments.Fragment5; public class MainActivity extends AppCompatActivity { private ViewPager vp; private RadioGroup rg; private RadioButton rb1; private RadioButton rb2; private RadioButton rb3; private RadioButton rb4; private RadioButton rb5; private FrameLayout flay; private Fragment1 fragment1; private Fragment2 fragment2; private Fragment3 fragment3; private Fragment4 fragment4; private Fragment5 fragment5; private android.support.v4.app.FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); flay = findViewById(R.id.flay); //vp = findViewById(R.id.vp); rg = findViewById(R.id.rg); rb1 = findViewById(R.id.rb1); rb2 = findViewById(R.id.rb2); rb3 = findViewById(R.id.rb3); rb4 = findViewById(R.id.rb4); rb5 = findViewById(R.id.rb5); getfrage(); } public void getfrage() { fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); fragment4 = new Fragment4(); fragment5 = new Fragment5(); getraidgout(); } public void getraidgout() { rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rb1: rb1 = (RadioButton) findViewById(R.id.rb1); fragmentManager.beginTransaction().replace(R.id.flay, fragment1).show(fragment1).hide(fragment2).hide(fragment3).hide(fragment4).hide(fragment5).commit(); rb1.setChecked(true); break; case R.id.rb2: rb2 = (RadioButton) findViewById(R.id.rb2); fragmentManager.beginTransaction().replace(R.id.flay, fragment2).show(fragment2).hide(fragment1).hide(fragment3).hide(fragment4).hide(fragment5).commit(); rb2.setChecked(true); break; case R.id.rb3: rb3 = (RadioButton) findViewById(R.id.rb3); fragmentManager.beginTransaction().replace(R.id.flay, fragment3).show(fragment3).hide(fragment2).hide(fragment1).hide(fragment4).hide(fragment5).commit(); rb3.setChecked(true); break; case R.id.rb4: rb4 = (RadioButton) findViewById(R.id.rb4); fragmentManager.beginTransaction().replace(R.id.flay, fragment4).show(fragment4).hide(fragment2).hide(fragment3).hide(fragment1).hide(fragment5).commit(); rb4.setChecked(true); break; case R.id.rb5: rb5 = (RadioButton) findViewById(R.id.rb5); fragmentManager.beginTransaction().replace(R.id.flay, fragment5).show(fragment5).hide(fragment2).hide(fragment3).hide(fragment4).hide(fragment1).commit(); rb5.setChecked(true); break; } } }); } }
<?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="com.lbs.jsonjiexi.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/flay"/> <!--<android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/vp"> </android.support.v4.view.ViewPager>--> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rg"> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rb1" android:layout_weight="1" android:button="@null" android:text="首页"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rb2" android:layout_weight="1" android:button="@null" android:text="自选"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rb3" android:layout_weight="1" android:button="@null" android:text="行情"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rb4" android:button="@null" android:layout_weight="1" android:text="资讯"/> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rb5" android:button="@null" android:layout_weight="1" android:text="交易"/> </RadioGroup> </LinearLayout>