Fragment+RadioGroup点击切换Fragment
来源:互联网 发布:机房机柜网络整改 编辑:程序博客网 时间:2024/06/05 08:36
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup rg;
private RadioButton[] rbArray;
private ArrayList<Fragment> listFragemnt;
private FragmentManager fm;
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
rg = (RadioGroup) findViewById(R.id.rg);
rbArray = new RadioButton[rg.getChildCount()];
for (int i = 0; i <rbArray.length ; i++) {
rbArray[i] = (RadioButton) rg.getChildAt(i);
}
fm = getSupportFragmentManager();
listFragemnt = new ArrayList<>();
FragmentA fma = new FragmentA();
FragmentB fb = new FragmentB();
FragmentC fc = new FragmentC();
listFragemnt.add(fma);
listFragemnt.add(fb);
listFragemnt.add(fc);
FragmentTransaction tt = fm.beginTransaction();
tt.add(R.id.layout,listFragemnt.get(0));
tt.commit();
rbArray[0].setChecked(true);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
showFragment(i);
}
private void showFragment(int j) {
FragmentTransaction tt = fm.beginTransaction();
for (int i = 0; i <rbArray.length ; i++) {
if(rbArray[i].getId()== j){
if(listFragemnt.get(i).isAdded()){
tt.show(listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
}else {
tt.add(R.id.layout,listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
}
count = i;
break;
}
}
}
private RadioGroup rg;
private RadioButton[] rbArray;
private ArrayList<Fragment> listFragemnt;
private FragmentManager fm;
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
rg = (RadioGroup) findViewById(R.id.rg);
rbArray = new RadioButton[rg.getChildCount()];
for (int i = 0; i <rbArray.length ; i++) {
rbArray[i] = (RadioButton) rg.getChildAt(i);
}
fm = getSupportFragmentManager();
listFragemnt = new ArrayList<>();
FragmentA fma = new FragmentA();
FragmentB fb = new FragmentB();
FragmentC fc = new FragmentC();
listFragemnt.add(fma);
listFragemnt.add(fb);
listFragemnt.add(fc);
FragmentTransaction tt = fm.beginTransaction();
tt.add(R.id.layout,listFragemnt.get(0));
tt.commit();
rbArray[0].setChecked(true);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
showFragment(i);
}
private void showFragment(int j) {
FragmentTransaction tt = fm.beginTransaction();
for (int i = 0; i <rbArray.length ; i++) {
if(rbArray[i].getId()== j){
if(listFragemnt.get(i).isAdded()){
tt.show(listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
}else {
tt.add(R.id.layout,listFragemnt.get(i)).hide(listFragemnt.get(count)).commit();
}
count = i;
break;
}
}
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.app3.MainActivity"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout" android:orientation="vertical" ></LinearLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="30dp" android:orientation="horizontal" android:id="@+id/rg" android:layout_alignParentBottom="true"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:id="@+id/rb1" android:layout_weight="1" android:gravity="center" android:text="内容1" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:id="@+id/rb2" android:layout_weight="1" android:gravity="center" android:text="内容2" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:id="@+id/rb3" android:layout_weight="1" android:gravity="center" android:text="内容3" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:id="@+id/rb4" android:layout_weight="1" android:gravity="center" android:text="内容4" />
阅读全文
0 0
- Fragment+RadioGroup点击切换Fragment
- 点击RadioGroup切换Fragment
- RadioGroup点击RadioButtion切换Fragment
- RadioGroup+RadioButton切换Fragment
- [Android学习笔记]fragment配合RadioGroup实现点击切换内容
- RadioGroup+Fragment实现点击切换,类似美团有Demo
- ViewPager+Fragment+RadioGroup实现页面联动\点击切换
- 滑动或点击按钮切换页面 ViewPager+RadioGroup+Fragment
- RadioGroup+Fragment实现Tab切换
- RadioGroup+Fragment实现界面切换
- RadioGroup和Fragment相结合实现点击RadioButton进行Fragment的切换
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- Android 使用ViewPager和RadioGroup配合Fragment实现标签页,可点击按钮切换、滑动切换。
- RadioGroup+Fragment
- radioGroup+fragment
- RadioGroup+Fragment 实现简单选项卡切换
- RadioGroup+Fragment实现页面之间的切换
- RadioGroup+Fragment实现页面之间的切换
- HTTP中报文的请求以及响应以及相关指令
- 迭代器iterator的一点总结
- 食物链
- 存储引擎
- altium designer 笔记1
- Fragment+RadioGroup点击切换Fragment
- 导出excel
- 操作系统、驱动和硬件的关系的思考
- 10分钟带你看懂冒泡排序Yu选择排序
- Python3爬取某教育平台题库保存为Word文档
- U盘安装CentOS7 的注意要点
- JVM监控
- 一点点前端代码,使用ECharts插入柱状图
- 自动查找进程PID 并关闭