Fragment案例

47 篇文章 4 订阅
订阅专栏

Fragment也是常用的在Android中,之前只是总结了下知识点,今天写了个例子,对只是巩固了下,下面是例子:
这里写图片描述
MainActivity.class

package com.example.fragmentdemo1;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.fragment.A;

public class MainActivity extends FragmentActivity implements OnClickListener{

    private LinearLayout ll3,ll2,ll4,ll1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll1 = (LinearLayout) findViewById(R.id.ll1);
        ll1.setOnClickListener(this);

        ll2 = (LinearLayout) findViewById(R.id.ll2);
        ll2.setOnClickListener(this);


        ll3 = (LinearLayout) findViewById(R.id.ll3);
        ll3.setOnClickListener(this);

        ll4 = (LinearLayout) findViewById(R.id.ll4);
        ll4.setOnClickListener(this);
        onClick(ll1);

    }
    private A fgA,fgB,fgC,fgD;
    @Override
    public void onClick(View v) {
        ImageView icon;
        switch (v.getId()) {
        case R.id.ll1:{
            if(R.id.ll1 != curFragMent){
                this.resetIconBackground();
                curFragMent = R.id.ll1;
                icon = (ImageView) ll1.findViewById(R.id.iv1);
                icon.setBackgroundResource(R.drawable.icon);
                if(fgD == null){
                    fgD = new A();

                }
                getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgD).commit();
            }
            break;
        }
        case R.id.ll2:{
            if(R.id.ll2 != curFragMent){
                this.resetIconBackground();
                curFragMent = R.id.ll2;
                icon = (ImageView) ll2.findViewById(R.id.iv2);
                icon.setBackgroundResource(R.drawable.icon);
                if(fgC == null){
                    fgC = new A(2);
                }
                getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgC).commit();
            }
            break;
        }
        case R.id.ll3:{//消息
            if(R.id.ll3 != curFragMent){
                this.resetIconBackground();
                curFragMent = R.id.ll3;
                icon = (ImageView) ll3.findViewById(R.id.iv3);
                icon.setBackgroundResource(R.drawable.icon);
                if(fgA == null){
                    fgA = new A(3);
                }
                //                  fgA.enableTitleListener();\
                getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/,fgA).commit();
            }
            break;
        }
        case R.id.ll4:{
            if(R.id.ll4 != curFragMent){
                this.resetIconBackground();
                curFragMent = R.id.ll4;
                icon = (ImageView) ll4.findViewById(R.id.iv4);
                icon.setBackgroundResource(R.drawable.icon);
                if(fgB == null){
                    fgB = new A(4);
                }
                getFragmentManager().beginTransaction().replace(R.id.fl_main/*fl_drivingcar_main*/, fgB).commit();
            }
            break;
        }
        }
    }
    private int curFragMent = 0;
    private void resetIconBackground(){
        ImageView icon;
        switch (curFragMent) {
        case R.id.ll1:{
            icon = (ImageView) ll1.findViewById(R.id.iv1);
            icon.setBackgroundResource(R.drawable.ic_launcher);
            break;
        }
        case R.id.ll2:{
            icon = (ImageView) ll2.findViewById(R.id.iv2);
            icon.setBackgroundResource(R.drawable.ic_launcher);
            break;
        }
        case R.id.ll3:{
            icon = (ImageView) ll3.findViewById(R.id.iv3);
            icon.setBackgroundResource(R.drawable.ic_launcher);
            break;
        }
        case R.id.ll4:{
            icon = (ImageView) ll4.findViewById(R.id.iv4);
            icon.setBackgroundResource(R.drawable.ic_launcher);
            break;
        }
        }
    }

}

A.class

package com.example.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.fragmentdemo1.R;

public class A extends Fragment{
    int tv_type;
    TextView tv;


    public A() {
    }
    public A(int tv_type) {
        this.tv_type = tv_type;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_content, null);
        tv=(TextView) view.findViewById(R.id.tv_type);
        tv.setText(tv_type+"");
        return view;
    }

}

activity_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="77dp"
    android:orientation="vertical" >
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D3D3" />
    <LinearLayout
        android:baselineAligned="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECECEC"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" >
        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/iv1"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
            <TextView
                android:clickable="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >
                <ImageView
                    android:id="@+id/iv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_launcher" />
                <!-- <TextView
                    android:id="@+id/msg_number"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_launcher"
                    android:layout_alignBaseline="@id/message_icon"
                    android:gravity="center"
                    android:layout_marginLeft="15dp"
                    android:textSize="8sp"/> -->
            </RelativeLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="222"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/iv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
            <TextView
                android:clickable="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="不错"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/iv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
            <TextView
                android:clickable="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="444"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

activity_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center">
    <TextView 
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是内容"/>
    <TextView 
        android:id="@+id/tv_type"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是标识"/>

</LinearLayout>

activity_main.xml

<RelativeLayout 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">

    <include
        android:layout_width="match_parent"
        android:layout_height="57dp"
        layout="@layout/activity_title"
        android:layout_alignParentTop="true" />

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        android:layout_width="match_parent"
        android:layout_height="57dp"
        layout="@layout/activity_bottom"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

activity_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="12dp"
        android:text="111" />

</LinearLayout>
Fragment应用实例
huangyitingwoqu的博客
06-08 563
Fragment一般也叫碎片,常常为了同时适应小屏幕和大屏幕而使用,fragment必须嵌入在activity中使用,其生命周期也受到嵌入的activity生命周期的影响,activity暂停时,fragment也暂停,activity销毁时,fragment也被销毁,fragment的生命周期中较重要的函数大致如下: onAttach():将fragment嵌入在相应的activity中——对
android fragment案例,Android fragment使用实例
weixin_34544513的博客
05-29 757
下面通过一个实例介绍Fragment的使用。实例中的Activity通过两个Fragment将屏幕分为两个部分,左侧为一个显示标题的Fragment,右侧为一个显示内容的Fragment。当点击左侧某个标题时,高亮该标题并在右侧显示该标题对应的内容。具体效果如下:Fragment实例主Activity布局文件为了使主Activity适用屏幕的变化,在横屏时将标题和内容在同一页面显示,而当处于竖屏时...
Fragment使用案例
weixin_30437847的博客
02-28 94
import android.R.string; import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map; import android.app.Fragment; import android.app.ListFragment; impor...
Fragment__案例
全干工程师
03-03 605
附上一张不是效果的效果图 -----------------------------------------------以下是代码-------------------------------------------- MainActivity.java package com.example.fragmenttest; import android.os.Bundle; import a
Fragment使用实例
weixin_34061042的博客
06-29 259
2019独角兽企业重金招聘Python工程师标准>>> ...
Viewpager+Fragment案例
10-21
在这个"ViewPager+Fragment案例"中,我们将深入探讨如何结合这两个组件来创建一个高效的用户界面。 1. **ViewPager介绍** `ViewPager`是Android Support Library中的一个控件,它允许用户水平滑动查看多个页面。`...
Fragment(碎片)实用案例
Ashurol的博客
01-22 994
碎片很多时候都是在平板开发使用,但是这样开发手机版的程序就得多写一份代码,今天就来解决这个问题,一份代码同时兼容手机与平板。 先看效果图,清闻应用。 大概了解了,开始我们的编码 首先我们先准备一个新闻实体类,新建类News。 public class News { private String title; public String getTitle() { ret
Fragment实战案例
lks1139230294的博客
04-19 1298
对于Fragment,《Android编程权威指南》的作者极力推崇,坚持AUF(Always Use Fragment)原则,即总是使用FragmentFragment本身占用内存比Activity少很多,在流畅性,性能上具有很大的优势,尤其是在中低端手机上。此外,如果你的应用需要移植到平板上的话,那使用Fragment更是你的不二之选。 本文就是通过一个实战案例来解剖一下Fragment
Fragment的三个例子
03-18
1、fragment代替ActivityGroup 2、fragment之间传递数据 3、FragmentTabHost的使用
Fragment实例二(源码)
05-31
一个Fragment的实例开发,仿底部菜单栏实现效果,主要是通过实例让大家知道Fragment这个新引进的类的用法
Fragment三个简单示例
09-15
入门级的Fragment示例项目,分三示例,1,讲解加载两个Fragment, 2,讲解了动态加入Fragment,3,讲解了Fragment之间通信。
Android Fragment案例
12-26
Fragment应用实战案例,联系使用Android中的fragment,替换TabActivity,ActivityGroup;更多详细内容请访问 http://blog.csdn.net/qq_20889581/article/details/50407863 阅读浏览
Android Fragment使用之实例演示
01-20
FragmentAndroid honeycomb 3.0新增的概念,在如何使用Android Fragment中做了关于Fragment的详细介绍。本文则主要是通过实例的方式让大家更直观的了解Fragment的使用方法。        首先贴上实例的运行效果截图:        效果图的左边是一个列表,右边是列表item的详情。        先看一下布局文件(layout): XML/HTML代码 <?xml version=“1.0″ encoding=“utf-8″?> <LinearLayout xmlns:android=“http://schemas.android.co
Android Fragment使用案例
dreams_deng的博客
03-03 3662
fragment加载方式 1. 静态加载( 不行,没有 试出来) 2. 动态加载 1. Fragent使用 效果图: Java代码 MainActivity: **************************************************************************************************************...
Fragment例子
zhangliang
11-15 1653
Fragment翻译为碎片,功能和Activity类似,依赖于父Activity,有自己独立的声明周期,用来描述一些行为或一部分用户界面,在一个Activity中你可以合并多个fragment,在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中使用.你可以认为fragment作为一个activity中的一子模块 ,接收自己的输入事件,你可以向运行中的a
Android fragment使用详解及案例
格物致知,敬事而信!
10-21 1120
Fragment 设计理念 在设计应用时特别是Android 应用 ,有众多的分辨率要去适应,而fragments 可以让你在屏幕不同的屏幕上动态管理UI。例如:通讯应用程序(QQ),用户列表可以在左边,消息窗口在右边的设计。而在手机屏幕用户列表填充屏幕当点击某一用户时,则弹出对话窗口的设计,如下图:   Fragment的生命周期  每一个fragments 都有自己
fragment 简单的例子
qq_39388035的博客
12-03 3661
fragment是什么呢 fragment相当于一块画布 可以根据要求在fragment更改界面 就是说不换activity的情况下把局部布局给改了
Android Fragment简易新闻浏览界面设计案例
资源摘要信息:"在本案例中,将介绍如何使用Android中的Fragment组件来实现一个简易的新闻浏览界面。FragmentAndroid应用开发中一个非常重要的组件,它允许开发者在一个Activity中嵌入多个可复用的模块,这些模块...
写文章

热门文章

  • Bugly的使用 31887
  • 在App中打开淘宝 15370
  • 反编译 5739
  • Android组件化开发 3427
  • Retrofit2基本使用 3224

分类专栏

  • HarmonyOS学习 1篇
  • 工具 2篇
  • 知识点 47篇
  • 事件 7篇
  • android应用界面编辑 1篇
  • Java 2篇
  • 学习 4篇

最新评论

  • Mac系统配置java、Android_sdk、gradle、maven、ndk、flutter、tomcat环境变量

    蒸鹅心鸭: 额,为什么我这边安装了就有了,不用配置环境变量

  • Bugly的使用

    m0_52537868: 始终报错,楼主能帮忙解答一下吗?

  • Android组件化开发

    爱吃西蓝花i: 写的很好呀,为什么没有热度呢,加油喔博主!kisskiss

  • android studio3.0 生成apk安装到手机上失败(-15)

    Crypto_Thinker: Execution failed for task ':app:installDebug'. > com.android.builder.testing.api.DeviceException: No connected devices!这是什么意思啊,博主

  • 反编译

    water___Wang: 受教了~

大家在看

  • C++调试经验(4):Linux下调试CAN通信的方法
  • Android OpenGL光照效果
  • AUTOSAR_EXP_ARAComAPI的6章笔记(1) 758
  • WPF入门_04绑定 728
  • Linux进程间通信 404

最新文章

  • 鸿蒙(HarmonyOS)HelloWo入门
  • Android build.gradle读取String中文件及gradle.properties数据
  • Mac系统配置java、Android_sdk、gradle、maven、ndk、flutter、tomcat环境变量
2023年3篇
2021年9篇
2020年2篇
2019年3篇
2018年2篇
2017年13篇
2016年22篇
2015年15篇

目录

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值

玻璃钢生产厂家仙林商场新春美陈玻璃钢娃娃雕塑玻璃钢校园雕塑设计费用玻璃钢雕塑设计制作需要多少钱广东主题公园玻璃钢雕塑工厂树脂玻璃钢雕塑视频河南创意玻璃钢雕塑多少钱上海佛像玻璃钢雕塑优势铜川卡通玻璃钢雕塑安装艺修玻璃钢雕塑河南水景玻璃钢彩绘雕塑公司环保玻璃钢雕塑供应商专业生产玻璃钢花盆吉林玻璃钢雕塑企业湖南玻璃钢雕塑定做临高县玻璃钢雕塑定制个性化玻璃钢雕塑制作银川市玻璃钢雕塑定制商场美陈特装安装下城区玻璃钢雕塑设计厦门玻璃钢雕塑公司 鹤顶红宁安玻璃钢牌匾雕塑保山玻璃钢雕塑供应商呼和浩特玻璃钢仿铜雕塑十堰户外玻璃钢雕塑报价台州玻璃钢仿铜雕塑多少钱鼓楼商场美陈装饰芜湖步行街玻璃钢雕塑制作池州步行街玻璃钢雕塑市场广场玻璃钢卡通雕塑摆件香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化