底部菜单实现(二):RadioGroup和fragment
2016-08-16 15:41
363 查看
一布局文件
二.与上一遍一样,创建三个fragment和缓存处理
<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(); } }
相关文章推荐
- Android布局的小窍门?
- Web布局连载——两栏固定布局(五)
- 样式表CSS布局经验
- 在winform下实现左右布局多窗口界面的方法之续篇
- css网页布局中注意的几个问题小结
- DL.DT.DD实现左右的布局简单例子第1/2页
- 使用CSS框架布局的缺点和优点小结
- div+CSS网页布局的意义与副作用原因小结第1/2页
- 在winform下实现左右布局多窗口界面的方法
- Android布局技巧之创建可重用的UI组件
- Android编程之代码创建布局实例分析
- CSS顶级技巧大放送,div+css布局必知
- 用div实现像table一样的布局方法
- Bootstrap三种表单布局的使用方法
- 精彩的Bootstrap案例分享 重点在注释!(选项卡、栅格布局)
- 第七篇Bootstrap表单布局实例代码详解(三种表单布局)
- jQuery EasyUi实战教程之布局篇
- jQuery EasyUI 布局之动态添加tabs标签页
- jquery自适应布局的简单实例
- Bootstrap页面布局基础知识全面解析