【1-100】RadioGroup实现应用主界面「建议收藏」

【1-100】RadioGroup实现应用主界面

大家好,又见面了,我是全栈君。

首先,我们先来创建主界面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/group_tab"/>
    <RadioGroup
        android:id="@+id/group_tab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#FBFBFB"
        android:orientation="horizontal">
        
        <RadioButton
            android:id="@+id/rb_watch"
            style="@style/main_tab_style"
            android:drawableTop="@drawable/select_tab_watch"
            android:text="高速全览"/>
            
            .......省略
            
        <RadioButton
            android:id="@+id/rb_event"
            style="@style/main_tab_style"
            android:checked="true"
            android:drawableTop="@drawable/select_tab_event"
            android:text="事件管理"/>
    </RadioGroup>
</RelativeLayout>

可以看到,我们将最后一个先设置为选中状态,在Activity中再进行状态的切换

四个RadioButton的属性大多都是一样的,所以抽取到styles

<style name="main_tab_style">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@drawable/select_tab_text_color</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:drawablePadding">3dp</item>
        <item name="android:gravity">center</item>
    </style>

还有drawableToptext相应的Seletor
select_tab_bus.xml
这里只列出了一个,其他三个只是图片不同就不列出来了

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_bus_pre" android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@drawable/tab_bus_pre" android:state_checked="true" android:state_pressed="false" />
    <item android:drawable="@drawable/tab_bus_nomal" />
</selector>

select_tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#2e9ed0"/>
    <!-- not selected -->
    <item android:state_checked="false" android:color="#B2B2B2"/>
</selector>

到这里我们的布局就准备好了,接下来开始编写Fragment与Activity的代码。

fragment代码很简单,如下,

public class MainExpresswayWatchFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common, container, false);
        TextView tvContent = (TextView) view.findViewById(R.id.tv_content);
        tvContent.setText("高速全览");
        return view;
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        // 每个Fragment都需要实现`setMenuVisibility`来控制视图,
        // 当Fragment不可见的时候,需要隐藏相应的视图,不然会使界面重叠在一起。
        if (this.getView() != null) {
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
        }
    }
}

接下来是Activity的代码,关键代码都有注释,我就不多说了…

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private FrameLayout fragmentContainer;

    // 是否第一次进入主界面
    private boolean isFirstEnter = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_group_main);
        radioGroup = (RadioGroup) findViewById(R.id.group_tab);
        fragmentContainer = (FrameLayout) findViewById(R.id.fragment_container);

        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isFirstEnter) {
            isFirstEnter = false;
            // 准备显示界面的时候切换第一个RadioButton为选中状态
            radioGroup.check(R.id.rb_watch);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        int index = 0;
        switch (i) {
            case R.id.rb_watch:
                index = 0;
                break;
            case R.id.rb_bus_danger:
                index = 1;
                break;
            case R.id.rb_car:
                index = 2;
                break;
            case R.id.rb_event:
                index = 3;
                break;
        }
        // 从FragmentManager中查找Fragment,找不到就使用getItem获取
        Fragment fragment = (Fragment) fragments.instantiateItem(fragmentContainer, index);
        // 设置显示第一个Fragment
        fragments.setPrimaryItem(fragmentContainer, 0, fragment);
        // 提交事务
        fragments.finishUpdate(fragmentContainer);
    }

    // 使用FragmentStatePagerAdapter管理Fragment
    FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new MainExpresswayWatchFragment();
                    break;
                case 1:
                    fragment = new MainBusDangerFragment();
                    break;
                case 2:
                    fragment = new MainCarManageFragment();
                    break;
                case 3:
                    fragment = new MainEventDangerFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    };
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108869.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HTML5+开发移动app教程1-环境搭建「建议收藏」

    HTML5+开发移动app教程1-环境搭建「建议收藏」前言本教程是介绍使用html5+(nativejs)和mui开发移动app(包括android和ios),感兴趣请继续,不感兴趣请跳过,大部分来自官方api,增加了一些自己的总结。HBuilder说明用h5+开发app的环境,也是ide,对eclipse做了深度定制,以前使用过eclipse或者myeclipse可以直接上手官网http://www.dclo

    2022年5月31日
    33
  • zencart 引用模板语句「建议收藏」

    zencart 引用模板语句「建议收藏」例如引用:head模板:&lt;?php/***preparesanddisplaysheaderoutput**/if(CUSTOMERS_APPROVAL_AUTHORIZATION==1&amp;&amp;CUSTOMERS_AUTHORIZATION_HEADER_OFF==’true…

    2022年7月27日
    2
  • web服务器有哪几种_web服务器的虚拟目录

    web服务器有哪几种_web服务器的虚拟目录当我们打开电脑,通过浏览器看到的网站,所有网站服务器多是我们所说的web服务器,具体解释就是一种驻留在Internet上的计算机程序,web服务器通过存储网站文件,放置大小不一各类数据文件,来进行工作,所以世界上每个角落多有它的身影。我们常见的Web服务器协议有三种,1、HTTP协议,2、HTML文档格式,最后是浏览器统一资源定位器,也就是我们常见的URL。第一种:IISIIS是我们网站用的最普遍的web服务器,IIS允许在公共网络上或者普通网络上发布信息的服务器,使IIS成为使用最广的web服务器之

    2022年9月19日
    1
  • 10款滑动门代码_jquery 滑动门_js滑动门_tab滑动门_jquery 选项卡_js选项卡_tab选项卡效果(三)

    10款滑动门代码_jquery 滑动门_js滑动门_tab滑动门_jquery 选项卡_js选项卡_tab选项卡效果(三)jquerytab选项卡插件滑动选项卡淡隐淡现选项卡jquerytab选项卡插件轻量级tab选项卡插件支持鼠标滑过、点击、自动切换、数据回调等功能jquery选项卡插件jquerytab选项卡支持垂直选项卡滚动、水平选项卡滚动、自动选项卡切换等。jquerytab选项卡ajax选项卡静态选项卡鼠标点击选项卡鼠标滑过选项卡jquery图片延迟加载插件制作tab选项卡图片异步加载…

    2025年6月5日
    0
  • 卡方检验spss步骤_数据分析–学统计&amp;SPSS操作

    卡方检验spss步骤_数据分析–学统计&amp;SPSS操作笔记内容来源:拉勾教育数据分析实战训练营我是一个在教育留学行业8年的老兵,受疫情的影响留学行业受挫严重,让我也不得不积极寻找新的职业出路。虽然我本身是留学行业,但对数据分析一直有浓厚的兴趣,日常工作中也会做一些数据的复盘分析项目。加上我在留学行业对于各专业的通透了解,自2016年起,在各国新兴的专业–商业分析、数据科学都是基于大数据分析的专业,受到留学生的火爆欢迎,可见各行各业对于数据分析的人才…

    2022年5月13日
    152
  • 花生日记

    花生日记花生日记app是一款优秀的导购软件它能带你找到淘宝天猫隐藏的大额优惠券!领取优惠券、交易都是在【淘宝天猫】上完成的,绝对安全!

    2022年6月1日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号