Android学习笔记 软件框架RadioGroup+fragment (底部导航栏切换)

1、定义启动页面(欢迎页面)

设置延迟两秒进入

//两秒延迟进入主页面
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 //启动主页面
 startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
 finish();//关闭当前页面 }
 }, 2000);

注意延迟后要finish该页面 这样退出时就不会再出现启动页面

2、主页面

布局文件



 
 
 
 
 
 
 
 

将一些共性属性抽取出来得到style="@style/MainButtonStyle"

选择器中写点击radioButton的变化



 
 

3、基类BaseFragment

public abstract class BaseFragment extends Fragment {
 public Context mContext;
 @Override
 public void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 mContext = getActivity();
 }
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup 
container, Bundle savedInstanceState) {
 return initView();
 }
 @Override
 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 initData();
 }
 /**
 * 有子类实现,实现特有效果
 * @return
 */
 public abstract View initView();

 /**
 * 初始化数据
 */
 public void initData() {
 }
}

定义initView()方法为抽象 强制子类实现该方法初始化布局  initData()方法在子类需要初始化数据时调用

4、创建子Fragment

public class HomeFragment extends BaseFragment {
 private static final String TAG = 
HomeFragment.class.getSimpleName();
 private TextView textView;
 @Override
 public View initView() {
 Log.e(TAG,"主页视图被初始化了");
 textView = new TextView(mContext);
 textView.setGravity(Gravity.CENTER);
 textView.setTextSize(25);
 textView.setTextColor(Color.RED);
 return textView;
 }
 @Override
 public void initData() {
 super.initData();
 Log.e(TAG,"主页数据被初始化了");
 textView.setText("主页");
 }
}

继承BaseFragment实现父类方法initView()和initData()初始化视图和布局   后期定义布局后用

View view = View.inflate(mContext, R.layout.fragment_home, null);传入视图

5、在MainActivity中初始化各个Fragment

private void initFragment() {
 fragments = new ArrayList<>();
 fragments.add(new HomeFragment());
 fragments.add(new TypeFragment());
 fragments.add(new CommunityFragment());
 fragments.add(new ShoppingCartFragment());
 fragments.add(new UserFragment());
}

6、设置radioButton的监听 切换fragment

private void initListener() {
 rgMain.setOnCheckedChangeListener(new 
RadioGroup.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 switch (checkedId) {
 case R.id.rb_home:
 position = 0;
 break;
 case R.id.rb_type:
 position = 1;
 break;
 case R.id.rb_community:
 position = 2;
 break;
 case R.id.rb_cart:
 position = 3;
 break;
 case R.id.rb_user:
 position = 4;
 break;
 }
 BaseFragment baseFragment = getFragment(position);
 switchFragment(tempFragemnt, baseFragment);
 }
 });
 //默认设置首页
 rgMain.check(R.id.rb_home);
}

根据位置得到对应的fragment

private BaseFragment getFragment(int position) {
 if (fragments != null && fragments.size() > 0) {
 BaseFragment baseFragment = fragments.get(position);
 return baseFragment;
 }
 return null;
}

切换fragment

private void switchFragment(Fragment fromFragment, BaseFragment 
nextFragment) {
 if (tempFragemnt != nextFragment) {
 tempFragemnt = nextFragment;
 if (nextFragment != null) {
 FragmentTransaction transaction = 
getSupportFragmentManager().beginTransaction();
 //判断 nextFragment 是否添加
 if (!nextFragment.isAdded()) {
 //隐藏当前 Fragment
 if (fromFragment != null) {
 transaction.hide(fromFragment);
 }
 transaction.add(R.id.frameLayout, nextFragment).commit();
 } else {
 //隐藏当前 Fragment
 if (fromFragment != null) {
 transaction.hide(fromFragment);
 }
 transaction.show(nextFragment).commit();
 }
 }
 }
}

一定要用transaction  防止frangment被重复初始化 浪费资源

 

你可能感兴趣的:(Android)