动态添加RadioButton并切换Fragment
来源:互联网 发布:免费代理vpn软件 编辑:程序博客网 时间:2024/06/01 10:03
公司项目中有这样一个需求,当从网络获取json数据并解析后,动态的添加按钮,点击时切换对应按钮下存储的各种数据。
如下图:
这里只是单单为了动态添加RadioButton而已,所以数据直接用Fragment替换,效果如下:
MainActivity:
package com.ut.radiobuttondynamicadd;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.FrameLayout;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import com.ut.radiobuttondynamicadd.factory.UserFragmentFactory;import butterknife.ButterKnife;import butterknife.InjectView;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { @InjectView(R.id.radiogroup_single) RadioGroup mRadioGroup; @InjectView(R.id.fl_info_framlayout) FrameLayout mFlInfoFramlayout; //注意,因为是测试,所以这个数组个数不能超过自定义的UserFragment个数,原因什么的自己应该都明白 private String[] mStrContents = {"孙悟空","猪八戒","沙和尚","白龙马","唐僧"}; private RadioButton mFirstButton;//第一个按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); initRadioButton(); } /** * 动态添加RadioButton */ private void initRadioButton() { for (int i = 0; i < mStrContents.length; i++) { RadioButton tempButton = new RadioButton(this); tempButton.setBackgroundResource(R.drawable.main_radiobutton_bg_selector);// 设置RadioButton的背景图片 tempButton.setButtonDrawable(android.R.color.transparent); // 设置按钮的样式 tempButton.setPadding(30, 0, 0, 0); // 设置文字距离按钮四周的距离 tempButton.setText(mStrContents[i]); if (i == 0) { mFirstButton = tempButton; } mRadioGroup.addView(tempButton, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); } mRadioGroup.setOnCheckedChangeListener(this); if (mFirstButton != null) { mFirstButton.setChecked(true); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb = (RadioButton) findViewById(checkedId); if (rb.isChecked()) { for (int i = 0; i < mStrContents.length; i++) { if (rb.getText().equals(mStrContents[i])) { Log.d("onCheckedChanged", "当前选中界面:" + i); changeFragment(UserFragmentFactory.createFragment(i)); } } } } private void changeFragment(Fragment targetFragment) { getSupportFragmentManager().beginTransaction() .replace(R.id.fl_info_framlayout, targetFragment, "fragment") .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); }}
以上基本上便是核心代码。
下面是关于Fragment:
MyBaseFragment:
package com.ut.radiobuttondynamicadd.fragment;import android.support.v4.app.Fragment;/** * Description: * Author:Giousa * Date:2016/4/29 * Email:giousa@chinayoutu.com */public class MyBaseFragment extends Fragment {}
UserFragment1:
package com.ut.radiobuttondynamicadd.fragment;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * Description:我的界面 * Author:Giousa * Date:2016/4/28 * Email:giousa@chinayoutu.com */public class UserFragment1 extends MyBaseFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getContext()); textView.setText("第一个界面"); textView.setTextColor(Color.RED); return textView; }}
剩下的同上。
存储Fragment的工厂类:
UserFragmentFactory
package com.ut.radiobuttondynamicadd.factory;import com.ut.radiobuttondynamicadd.fragment.MyBaseFragment;import com.ut.radiobuttondynamicadd.fragment.UserFragment1;import com.ut.radiobuttondynamicadd.fragment.UserFragment2;import com.ut.radiobuttondynamicadd.fragment.UserFragment3;import com.ut.radiobuttondynamicadd.fragment.UserFragment4;import com.ut.radiobuttondynamicadd.fragment.UserFragment5;import com.ut.radiobuttondynamicadd.fragment.UserFragment6;import com.ut.radiobuttondynamicadd.fragment.UserFragment7;import com.ut.radiobuttondynamicadd.fragment.UserFragment8;import java.util.HashMap;/** * Description: * Author:Giousa * Date:2016/4/29 * Email:giousa@chinayoutu.com */public class UserFragmentFactory { private static HashMap<Integer, MyBaseFragment> hashMap = new HashMap<Integer, MyBaseFragment>(); public static MyBaseFragment createFragment(int arg0) { //内存中如果已经有当前根据索引生成的fragment,复用之前的fragment对象,内存中没有索引指向的fragment对象,创建 MyBaseFragment fragment = hashMap.get(arg0); if(fragment!=null){ return fragment; }else{ switch (arg0) { case 0: fragment = new UserFragment1(); break; case 1: fragment = new UserFragment2(); break; case 2: fragment = new UserFragment3(); break; case 3: fragment = new UserFragment4(); break; case 4: fragment = new UserFragment5(); break; case 5: fragment = new UserFragment6(); break; case 6: fragment = new UserFragment7(); break; case 7: fragment = new UserFragment8(); break; } //集合将创建过的fragment,管理起来 hashMap.put(arg0, fragment); return fragment; } }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <RadioGroup android:id="@+id/radiogroup_single" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_single" android:orientation="horizontal"/> <!--RadioButton实现点击切换Fragment--> <FrameLayout android:id="@+id/fl_info_framlayout" android:layout_width="200dp" android:layout_height="200dp" android:background="#4400ffff"/></LinearLayout>
这里顺便补充一句,项目中我虽然采用了动态添加RadioButton,但是下面可以按钮点击左右切换数据的界面并非是用Fragment,而是使用ViewPager实现的。
~Demo下载~
1 0
- 动态添加RadioButton并切换Fragment
- fragment中添加ViewPager实现界面切换,动态添加radiobutton
- Android_静态添加Fragment&RadioButton切换
- RadioButton实现Fragment切换
- RadioGroup+RadioButton切换Fragment
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- 底部RadioButton实现Fragment切换
- Fragment+RadioButton点击切换页面
- BottomTabBar替换RadioButton切换Fragment
- RadioButton动态添加drawable
- 动态添加RADIOBUTTON
- 自定义RadioGroup动态添加RadioButton,并获取选中radioButton的位置
- Fragment切换。radiobutton加fragment切换(附件源码下载)
- 用FragmentTabHost保存Fragment状态并添加切换动画
- fragment+Radiobutton实现顶部导航切换
- android的RadioButton切换不同的fragment
- Fragment+RadioButton实现点击切换页面效果
- 主页面RadioButton切换Fragment模板
- 图像深度
- 封装对象时候时间处理方式
- android 开机动画和关机动画修改与制作
- SharePoint 2013 图文介绍匿名访问,及问题解决方法。
- 15年编程生涯,资深架构师总结的7条经验
- 动态添加RadioButton并切换Fragment
- 欢迎使用CSDN-markdown编辑器
- Linux whatis man man: nothing appropriate
- Android Gradle 介绍 官方教程
- 防范 DDoS 攻击的 15 个方法
- 欢迎使用CSDN-markdown编辑器
- apktoools反编译报错
- 判断2的幂
- 区间dp