RadioButton+FrameLayout+Fragment实现底部导航
16lz
2021-01-26
截图:
布局:
<?xml version="1.0" encoding="utf-8"?>
样式:
主要java代码:
package com.shanjing.hotattention.activity;import android.Manifest;import android.content.Intent;import android.content.pm.PackageManager;import android.graphics.Bitmap;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.app.FragmentTransaction;import android.support.v4.content.ContextCompat;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import com.alibaba.android.arouter.facade.annotation.Route;import com.base.commonlib.BaseActivity;import com.base.commonlib.utils.StatusBarUtil;import com.base.commonlib.zxing.android.CaptureActivity;import com.shanjing.hotattention.R;import com.shanjing.hotattention.fragment.HotHomeFragment;import com.shanjing.hotattention.fragment.HotMallFragment;import com.shanjing.hotattention.fragment.HotMyFragment;import com.shanjing.hotattention.fragment.HotVideoFragment;import java.io.ByteArrayOutputStream;@Route(path = "/hotattention/main")public class HotAttentionHomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { public RadioGroup rg; public LinearLayout ll_sign; private RadioButton rb_home, rb_mall, rb_video, rb_my; private HotHomeFragment hotHomeFragment; private HotMallFragment hotMallFragment; private HotVideoFragment hotVideoFragment; private HotMyFragment hotMyFragment; private void assignViews() { iv_scan = findViewById(R.id.iv_scan); rg = findViewById(R.id.rg); ll_sign = findViewById(R.id.ll_sign); rb_home = findViewById(R.id.rb_home); rb_mall = findViewById(R.id.rb_mall); rb_video = findViewById(R.id.rb_video); rb_my = findViewById(R.id.rb_my); rg.setOnCheckedChangeListener(this); ll_sign.setOnClickListener(this); rb_home.setChecked(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hot_attention_home); //用来设置整体下移,状态栏沉浸 StatusBarUtil.getStatusBarHeight(HotAttentionHomeActivity.this); StatusBarUtil.setRootViewFitsSystemWindows(this, false); StatusBarUtil.setStatusBarColor(HotAttentionHomeActivity.this, Color.parseColor("#ffffff"));//设置背景颜色 assignViews(); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); hideAllFragment(transaction); switch (checkedId) { case R.id.rb_home: if (hotHomeFragment == null) { hotHomeFragment = new HotHomeFragment(); transaction.add(R.id.fragment_container, hotHomeFragment); } else { transaction.show(hotHomeFragment); } break; case R.id.rb_mall: if (hotMallFragment == null) { hotMallFragment = new HotMallFragment(); transaction.add(R.id.fragment_container, hotMallFragment); } else { transaction.show(hotMallFragment); } break; case R.id.rb_video: if (hotVideoFragment == null) { hotVideoFragment = new HotVideoFragment(); transaction.add(R.id.fragment_container, hotVideoFragment); } else { transaction.show(hotVideoFragment); } break; case R.id.rb_my: if (hotMyFragment == null) { hotMyFragment = new HotMyFragment(); transaction.add(R.id.fragment_container, hotMyFragment); } else { transaction.show(hotMyFragment); } break; } transaction.commit(); } public void hideAllFragment(FragmentTransaction transaction) { if (hotHomeFragment != null) { transaction.hide(hotHomeFragment); } if (hotMallFragment != null) { transaction.hide(hotMallFragment); } if (hotVideoFragment != null) { transaction.hide(hotVideoFragment); } if (hotMyFragment != null) { transaction.hide(hotMyFragment); } } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.ll_sign) {//跳转到发布页 startActivity(new Intent(this, IssueActivity.class)); } }}
再新建几个fragment作为各个模块就可以了
更多相关文章
- Android(安卓)通用Dialog中设置RecyclerView
- 有关ImageView的圆角或弧度角设置
- 网络获取图片实现无线自动轮播
- android 批量上传图片
- EditText中inputType="number"还可输入字母的设置
- Android设置屏幕方向
- Android——API23以上需要的动态权限
- MediaRecorder和MediaPlayer的简单使用
- Renderer (专门用于渲染3D 的接口 Renderer )()