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

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

设置延迟两秒进入

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

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

2、主页面

布局文件

<?xml version="1.0" encoding="utf-8"?>
<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:background="#ffffff"
 android:orientation="vertical"
 tools:context=".app.MainActivity">
 <FrameLayout
 android:id="@+id/frameLayout"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1" />
 <RadioGroup
 android:id="@+id/rg_main"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:background="@drawable/home_bottom_parent_bg"
 android:orientation="horizontal">
 <RadioButton
 android:id="@+id/rb_home"
 style="@style/MainButtonStyle"
 android:drawableTop="@drawable/home_button_selector"
 android:text="首页" />
 <RadioButton
 android:id="@+id/rb_type"
 style="@style/MainButtonStyle"
 android:drawableTop="@drawable/type_button_selector"
 android:text="分类" />
 <RadioButton
 android:id="@+id/rb_community"
 style="@style/MainButtonStyle"
 android:drawableTop="@drawable/community_button_selector"
 android:paddingTop="10dp"
 android:text="发现" />
 <RadioButton
 android:id="@+id/rb_cart"
 style="@style/MainButtonStyle"
 android:drawableTop="@drawable/cart_button_selector"
 android:text="购物车" />
 <RadioButton
 android:id="@+id/rb_user"
 style="@style/MainButtonStyle"
 android:drawableTop="@drawable/user_button_selector"
 android:text="个人中心" />
 </RadioGroup>
</LinearLayout>

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

<style name="MainButtonStyle">
 <!-- Customize your theme here. -->
 <item name="android:layout_width">0dp</item>
 <item name="android:layout_height">wrap_content</item>
 <item name="android:layout_weight">1</item>
 <item name="android:button">@null</item>
 <!-- <item name="android:drawablePadding">3dp</item>-->
 <item 
name="android:textColor">@drawable/bottom_button_text_selector</item>
 <item name="android:textSize">10sp</item>
 <item name="android:gravity">center</item>
</style>

选择器中写点击radioButton的变化

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="#535353" android:state_checked="false"></item>
 <item android:color="#ff4040" android:state_checked="true"></item>
</selector>

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被重复初始化 浪费资源

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值