一布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.radiogroup.MainActivity"<FrameLayout
android:id="@+id/framlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:button="@null"
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1"/>
<RadioButton
android:button="@null"
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn2"/>
<RadioButton
android:button="@null"
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn3"/>
</RadioGroup>
</LinearLayout>
二.与上一遍一样,创建三个fragment和缓存处理
三代码
public class MainActivity extends AppCompatActivity {
@Bind(R.id.framlayout)
FrameLayout mFramlayout;
@Bind(R.id.btn1)
Button mBtn1;
@Bind(R.id.btn2)
Button mBtn2;
@Bind(R.id.btn3)
Button mBtn3;
@Bind(R.id.radio_group)
RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
iniView();
iniEvent();
}
private void iniEvent() {
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
BaseFragment fragment=null;
switch (checkedId) {
case R.id.btn1:
fragment=FragmentFractory.getInstance().getBaseFragment(0);
break;
case R.id.btn2:
fragment=FragmentFractory.getInstance().getBaseFragment(1);
break;
case R.id.btn3:
fragment=FragmentFractory.getInstance().getBaseFragment(2);
break;
}
transaction.replace(R.id.framlayout,fragment).commit();
}
});
}
private void iniView() {
getSupportFragmentManager().
beginTransaction().
replace(R.id.framlayout, FragmentFractory.getInstance().getBaseFragment(0))
.commit();
}
}