Fragment

Android在Fragment中调用另一个Fragment 在Fragment中调用另一个Fragment 需求源于,想要在一个Fragment的内ListView中点击item,然后在相同的界面上覆盖显示对应item的内容(一个LinecChart) 最初想的是在一个PopUpwindow中加载LineChart,但是失败了,遂作罢,转向用Fragment加载。附效果图 阅读详情
1.Fragment基本使用 3.0  碎片 片段
2.Fragment之间的跳转(返回栈)
3.Fragment的生命周期(文档)
4.带侧边栏的Fragment(新建带侧边栏的模板的Activity)
5.Tabbed Activity(新建左右滑动的模板的Activity)
6.ViewPager+Fragment


layout-sw600dp
1.320,针对以下屏幕配置的设备:
    240x320ldpi(QVGA手持设备)
    320x480mdpi(手持设备)
    480x800hdpi(高分辨率手持设备)
2.480,针对480x800mdpi的屏幕(平板或手持设备)
3.600,针对600x1024mdip的屏幕(7英寸平板)

4.720,针对720x1280mdip的屏幕(10英寸平板)

微信效果图


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.zking.administrator.myapplication5.MainActivity">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/vp_main_viewpager"
        android:layout_weight="1"
        ></android.support.v4.view.ViewPager>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/rg_main_radiogroup"
        android:gravity="center">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="微信"
            android:drawableTop="@drawable/weixin_selctor"
            android:id="@+id/rb_main_weixin"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="通讯录"
            android:drawableTop="@drawable/contacts_selctor"
            android:id="@+id/rb_main_contacts"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="发现"
            android:drawableTop="@drawable/find_selctor"
            android:id="@+id/rb_main_find"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="我"
            android:drawableTop="@drawable/me_selctor"
            android:id="@+id/rb_main_me"/>
    </RadioGroup>
</LinearLayout>

写四个fragment碎片的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="微信"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#66ff0000"/>
</LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通讯录"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#66ff0000"/>
</LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发现"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#66ff0000"/>
</LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#66ff0000"/>
</LinearLayout>

package com.zking.administrator.myapplication5;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp_main_viewpager;
    private RadioGroup rg_main_radiogroup;
    List<Fragment> fragments=new ArrayList<>();
    int radio[]={R.id.rb_main_weixin,R.id.rb_main_contacts,R.id.rb_main_find,R.id.rb_main_me};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp_main_viewpager = (ViewPager) findViewById(R.id.vp_main_viewpager);
        rg_main_radiogroup = (RadioGroup) findViewById(R.id.rg_main_radiogroup);
        WeiXinFragment weiXinFragment=new WeiXinFragment();
        ContactsFragment contactsFragment=new ContactsFragment();
        FindFragment findFragment=new FindFragment();
        MeFragment meFragment=new MeFragment();
        fragments.add(weiXinFragment);
        fragments.add(contactsFragment);
        fragments.add(findFragment);
        fragments.add(meFragment);
        vp_main_viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        rg_main_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                for (int i = 0; i < radio.length; i++) {
                    if(checkedId==radio[i]){
                        vp_main_viewpager.setCurrentItem(i);
                    }
                }
            }
        });
        vp_main_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(state==2){
                    rg_main_radiogroup.check(radio[vp_main_viewpager.getCurrentItem()]);
                }
            }
        });
    }
    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
写四个Fragment片段,命名要规范

package com.zking.administrator.myapplication5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15.
 */

public class WeiXinFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.weixin_fragment,null);
    }
} 
package com.zking.administrator.myapplication5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15.
 */

public class ContactsFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.contacts_fragment,null);
    }
}

package com.zking.administrator.myapplication5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15.
 */

public class FindFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.find_fragment,null);
    }
}
package com.zking.administrator.myapplication5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15.
 */

public class MeFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.me_fragment,null);
    }
}



改善侧滑菜单Fragment切换速度(解决Fragment切换加载慢的问题) 1.现状: 在做项目的时候很多时候需要使用侧滑菜单。侧滑菜单项一般都是用Fragment。 一般设计上是在Home page的layout中使用FrameLayout 作为一个container 如:content_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 阅读详情

相关推荐

Fragment之间的切换

背景和要求:在一个FragmentA中设置一个View的监听,当这个view被点击时,就进入FragmentB,然后当点击FragmentB中的某个view的时候要重新返回到FragmentA中。这个就是类似于网易云音乐的切换歌词显示的效果。      首先有一个MusicPlayFragment类,该类继承了抽象类AbsMusicPlayFragment,他主要完成音乐播放的一些逻辑,因为主要

sd_zhuzhipeng的专栏 1559

简单的Fragment使用

返回博客列表 原 简单的使用Fragment   祭奠我们无处安放的青春 发布时间: 2016/07/01 14:02 阅读: 2 收藏: 0 点赞: 0 评论: 0 ( 编辑  | 删除  | 自荐  ) 摘要 简单的使用Fragment public class MainActivity extends FragmentActivi

xiaowangxiansheng的博客 221

Alibaba Java Coding Guidelines,以后我的Java代码规范,就靠它了

前言 对于Java代码规范,业界有统一的标准,不少公司对此都有一定的要求。但是即便如此,庞大的Java使用者由于经验很水平的限制,未必有规范编码的意识,而且即便经验丰富的老Java 程序员也无法做到时刻将规范牢记于心。所以对于代码规范扫描工具,一经问世就广受青睐,阿里巴巴出品的Alibaba Java Coding Guidelines(阿里巴巴Java代码规约扫描,以下简称为AJCG)插件便是其...

java梦工厂 859

Android移动开发之【Android实战项目】DAY2-使用Fragment实现底部菜单栏

由于TabActivity在Android4.0以后已经被完全弃用,那么我就不再浪费口水继续讲解它了,取而代之的是Fragment。Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Activity十分的相似,这一篇讲解Fragment的介绍和使用方法。 一、Fragment的基础知识介绍 1.1概述 1.1.1 特性 Fragment是ac...

NJU phd 在读。 1129

maya权重拷贝一对一,一对多

是不是有时候盔甲很多很碎头很大,用代理一个个拷贝很麻烦吧 用下面代码直接运行进行一对多或者一对一拷贝 第一个先选择参考物体,加选其他一堆有的没的有权重或没权重的物体执行一键拷贝,就可以啦 以下是源码送上,带走吧 ------------------------ #! -*- coding:utf -8 -*- #author:liubingdong #date:20...

weixin_30852367的博客 1532

国产数据库PolarDB-X实战:5分钟快速搭建本地开发环境(附Docker一键部署脚本)

本文提供了一份详尽的国产分布式数据库PolarDB-X本地开发环境搭建指南。通过Docker Compose一键部署脚本,开发者可在5分钟内快速启动一个功能完整的PolarDB-X实例,涵盖环境准备、脚本解析、连接验证及日常维护,极大简化了国产数据库的入门和体验流程。

weixin_29246195的博客 357

android 碎片技术,【移动开发】Android中强大的适配功能----Fragment(碎片)总结

【移动开发】Android中强大的适配功能----Fragment(碎片)总结发布时间:2020-06-27 00:32:58来源:51CTO阅读:10233作者:zhf651555765作为大多数刚接触Android应用开发的人来说,在一个强大的Activity类中,就可以完成丰富多彩的UI工作,但是杂乱的屏幕分辨率,使得本来好不容易写好的UI,变得不堪入目。。。该怎么办那?查阅了好多资料,才发...

weixin_30327815的博客 361

无感无刷电机三段式启动避坑指南:从预定位到闭环切换的STM32代码实现

本文深入解析无感无刷电机的三段式启动流程,涵盖预定位、启动加速与闭环切换三大阶段。重点对比了恒频升压、恒压升频与升频升压三种核心启动策略的优劣,并提供了基于STM32的详细状态机代码实现与关键参数调试指南,帮助工程师有效规避启动失步、线圈过热等常见问题,实现稳定可靠的无感驱动。

herb5的博客 535

Fragment 用法总结(一)

Fragment 用法总结(一)Fragment有点类似View,一种增强版的View,不仅可以show、hide,而且有大量的生命周期方法,处理事件,更好的和Activity交互,在显示title和content的层级界面上有独特的优势,更好的兼容适应平板和手机。下面主要分三个部分讲解Fragment的创建及基本用法、生命周期和高级用法。本文主要参考官方文档并加入自己整理的内容。创建fragmen

带着键盘走江湖。。。 8217

第一章:Profibus DP通信简介

PROFIBUS (Process Fieldbus)的缩写,是一种国际化的、开放的、不依赖于设备生产商的现场总线标准。它广泛应用于制造业自动化、流程工业自动化和楼宇、交通、电力等其他工业自动化领域。串行现场总线主要用来连接数字现场设备或中、低功能的系统,如传感器、执行机构、变送器、可编程序控制器(PLC)、数控装置(NC)、编程设备、本地人机接口等。

qq_22560021的博客 4893

simulink的电动汽车制动系统热管理仿真

本文系统研究了电动汽车制动系统热管理仿真技术。通过建立机电液多物理场耦合模型,分析了不同工况下的动态热平衡特性。提出基于MPC的动态调节和NSGA-II多目标优化算法,使电池温度波动降低60%。创新性采用石墨烯相变材料与微通道液冷复合技术,冷却系统能耗减少40%。研究构建了完整的工程验证平台,涵盖200kW电制动测试台和高精度热成像系统。展望指出,未来将结合Transformer预测和强化学习算法,进一步优化热管理策略。该研究为电动汽车制动系统热管理提供了从理论建模到工程实施的完整解决方案。

xiaoheshang_123的博客 121

Fragment简单介绍

Fragment简单介绍,全面的了解Fragment的使用

旧路 2万+

工业级向量检索核心技术:IVF-PQ原理与全流程解析

在大规模高维向量检索场景中,IVF-PQ(倒排索引结合乘积量化)凭借“粗筛范围+细算距离”的双重加速策略,成为核心解决方案。本文将深入解析其原理与全流程,助力开发者掌握这一关键技术。

进一步有进一步的欢喜~ 1197

activity嵌套fragment,fragment内嵌fragment之间传值切换fragment

至于fragment的用法,fragment+viewpager的滑动切换代码请参考博文 Android基础:翻页控件Viewpager的使用方法,viewpager+fragment混合使用 fragment与Activity 当在使用activity嵌套fragment,fragment再嵌套子fragment时,相当于有两层fragment,假设最外层为第一层,其中

fanfan 1万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Fragment嵌套Fragment

问题1、fragment嵌套fragment不显示问题 通常时候,我们制作底部Tab切换,会用到fragment。即一个Activity下,使用4种fragment。这次遇到的问题是关于fragment再次嵌套fragmen的问题,功能需要在其中一个fragment中再次嵌套frament,使用viewPager嵌套,此时需要注意一点,使用viewpager包裹住子fragment,我们都知道v...

CHZKAL的博客 6780

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Fragment动态实现及解决Fragment重叠问题

一、知识回顾(1)onSaveInstanceState()和onRestoreInstanceState()调用机制 地址:https://blog.youkuaiyun.com/qq_34490018/article/details/80069365(2)Fragment基础详解 地址:https://blog.youkuaiyun.com/qq_34490018/article/details/80100941...

我的书包哪里去了 5866

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Fragment 跳转到指定的Fragment 与 Activity跳转到指定的Fragment 的方法

Fragment 跳转到指定的Fragment 主要分为2中情况 1 同一个Activity上的Fragment 跳转到指定Fragment 例如MainActivity 上的Fragment 跳转问题 2 不在同一个Activity的2个Fragment 跳转 如果对Fragment 跳转问题没有思路,首先需要想到的就是Fragment 不可能独立存在的,它依附于Activity 这样...

小牧在一直在学习,在前进的道路上大家一起学习,进步。 5792

Activity管理Fragment之Fragment关闭当前回到之前Fragment

直接上代码说明: 首先默认你已经写好了一个Activity并至少添加了一个Fragment,下面是我的Activity中Fragment的布局代码和java代码; Activity中Fragment的布局代码: xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/an

成长笔记 1万+

android view 添加fragment,Android中Fragment怎么addView?

Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍、AndroidFragment使用、Android FragmentManageFragmentTransaction介绍中做了关于Fragment的详细介绍。这一片主要通过一个实例了解Fragment的使用。先看一下布局文件(layout):android:orientation=“...

weixin_39613540的博客 2003

单Activity+多Fragment以及多模块Activity+多Fragment“的设计模式编写的架构

单Activity+多Fragment以及多模块Activity+多Fragment"的设计模式编写的架构Rocket一、特性二、Github地址三、分享设计Rockt架构的思路四、踩坑经验之旅1、Can not perform this action after onSaveInstanceState2、FragmentManager is already executing transacti...

博由心生,福泽一生 1845

Fragment完全总结

一.动态添加fragment 第一步,动态创建管理器对象 第二步,获取fragment的事务对象并开启 第三步,调用事务中相应的动态操作fragment方法的执行 第四步,提交事务 二.静态添加fragment 1、继承Fragment,重写onCreateView决定Fragemnt的布局 2、在Activity中声明此Fragment,就当和普通的View一样 三.frag...

jinyu_mobao的博客 2142

android fragment addview,Android中Fragment怎么addView?

Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍、AndroidFragment使用、Android FragmentManageFragmentTransaction介绍中做了关于Fragment的详细介绍。这一片主要通过一个实例了解Fragment的使用。先看一下布局文件(layout):android:orientation=“...

weixin_33467739的博客 929

Android Fragment解析以及Fragment中碰到的各种坑

对Fragment 的梳理和总结,从Fragment 的坑中脱离出来。

Egos 的专栏 6921

Fragment启动方式

一:通过代码启动 包名:import android.app.FragmentTransaction; 1 、定义一个frameLayout布局 <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="

tianchong89的博客 6313

fragment保存数据后返回到指定fragment

移出后添加的fragment,展示指定的fragment: 添加fragment时候 需要fragment设置Tag,利用Tag找到指定fragment public void rollBackAppointFragment(String fragmentName) { int fragmentCount = getSupportFragmentManager().get...

jifenglie的博客 741
上一篇: RecyclerView
下一篇: Activity生命周期和Activity页面之间数据传递
XHui_Lin
博客等级 码龄10年 27粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值