Activity跳转fragment

Activity跳转fragmentfragment不能单独存在,必须依附在Activity上,所以在Activity跳转时,实际是跳到fragment的宿主上代码:Activity点击跳转里Intentintent=newIntent(ListDetailsActivity.this,MainActivity.class);intent.putExtra("id",2);startActivity(intent);frag…

大家好,又见面了,我是你们的朋友全栈君。

fragment不能单独存在,必须依附在Activity上,所以在Activity跳转时,实际是跳到fragment的宿主上

代码:

Activity点击跳转里

Intent intent=new Intent(ListDetailsActivity.this,MainActivity.class);
intent.putExtra("id",2);
startActivity(intent);

fragment宿主Activity

int id = getIntent().getIntExtra("id", 0);//获取intent值
if (id == 2) {
  
  //判断intent值
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.container,new MyFragent())//设置显示fragment
            .addToBackStack(null)
            .commit();
    mRadioButton2.setChecked(true);//给按钮设置状态
}

布局最好用

<FrameLayout  android:id="@+id/container"  android:layout_width="match_parent"  android:layout_height="0dip"  android:layout_weight="1.0" >
</FrameLayout>

<RadioGroup  android:id="@+id/main_radio"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_gravity="bottom"  android:layout_marginBottom="-6dp"  android:gravity="bottom"  android:orientation="horizontal" >

    <RadioButton  android:id="@+id/radio_button0"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:background="@drawable/tab_homepage"  android:button="@null"  android:layout_gravity="center_vertical"  android:gravity="center_horizontal"  />

    <RadioButton  android:id="@+id/radio_button1"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:button="@null"  android:background="@drawable/tab_class"  android:layout_gravity="center_vertical"  android:gravity="center_horizontal"  />

    <RadioButton  android:id="@+id/radio_button2"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:button="@null"  android:background="@drawable/tab_shopcar"  android:layout_gravity="center_vertical"  android:gravity="center_horizontal"  />

    <RadioButton  android:id="@+id/radio_button3"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:button="@null"  android:background="@drawable/tab_find"  android:layout_gravity="center_vertical"  android:gravity="center_horizontal"  />

    <RadioButton  android:id="@+id/radio_button4"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_weight="1"  android:button="@null"  android:background="@drawable/tab_mine"  android:layout_gravity="center_vertical"  android:gravity="center_horizontal"  />
</RadioGroup>

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

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

(0)
上一篇 2022年5月21日 下午7:00
下一篇 2022年5月21日 下午7:00


相关推荐

  • 括号匹配问题 栈c语言(c语言栈实现括号匹配)

    例如:{}[()]、{[()]}、()[]{}这种大中小括号成对出现(位置不限)则为括号匹配,反之则不匹配,如{()[接下来看一下实现方式栈的定义以及相关操作//栈的定义typedefstruct{ charelem[stack_size]; inttop;}seqStack;//栈的初始化voidinitStack(seqStack*s){ s->top=-…

    2022年4月13日
    59
  • 【python】分享一个多线程爬虫爬取表情包的代码

    【python】分享一个多线程爬虫爬取表情包的代码多线程爬虫可比单线程爬虫爬取速度多了好几倍 单线程就好比是一辆车来回运输货物 而多线程则是多辆车同时运输货物 需要用到的库 requests lxml os threading queue 该代码还能把数据存储到电脑桌面 明天添加 使用了线程池来高效爬取

    2026年3月18日
    2
  • python中怎么替换字符串中的内容_python怎么替换字符串的内容「建议收藏」

    python中怎么替换字符串中的内容_python怎么替换字符串的内容「建议收藏」Python中replace()函数把字符串中的old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过max次。replace()函数语法:str.replace(old,new[,max])参数:old–将被替换的子字符串。new–新字符串,用于替换old子字符串。max–可选字符串,替换不超过max次。返回值:返回字符串中的o…

    2022年5月9日
    155
  • Java的finalize方法干什么_Java finalize

    Java的finalize方法干什么_Java finalize我们通常用构造器来创建对象,而Finalize正好相反,构造方法执行对象的初始化操作,finalize方法执行对象的销毁操作.那我们什么时候需要使用finalize方法呢,我们都知道Java里垃圾回收器可以回收对象使用的内存空间,但是对象可能会持有很多资源比如Socket、文件句柄等,垃圾收集器无法回收这些资源,因此你需要使用finalize方法帮助GC回收这些资源,比如关闭打开的文件或者网元资源,删除临时文件等.一个例子Object类是所有类的父类,如果你去查看java.lang.Object类

    2026年1月26日
    3
  • GBDT算法简介_gbdt算法原理

    GBDT算法简介_gbdt算法原理在网上看到一篇GBDT介绍非常好的文章,GBDT大概是非常好用又非常好用的算法之一了吧(哈哈两个好的意思不一样)        GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一种迭代的决策树算法,该算法由多棵决策树组成,所有树的结论累加起来做最终答

    2022年10月12日
    5
  • spring boot + kafka 使用详细步骤[通俗易懂]

    spring boot + kafka 使用详细步骤[通俗易懂]一,Kafka的安装以及配置1.下载文件wgethttp://mirror.bit.edu.cn/apache/kafka/0.11.0.0/kafka_2.11-0.11.0.0.tgz2.安装tarxzvfkafka_2.11-0.11.0.0.tgz-C/usr/local/3.配置(服务器在阿里云ECS上)vi%kafka_home%/server…

    2022年5月9日
    143

发表回复

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

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