http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2160|回复: 2

[新手开发之旅] Android新手开发之旅-Fragment的用法

[复制链接]
发表于 2018-12-17 15:03:31 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-17 11:54 编辑

Fragment的用法




本篇说一下怎么将Fragment添加到Activity中,有两种方法:


先新建两个Fragment(LeftFragment,RightFragment):     

         QQ截图20181217103006.png         

LeftFragment
[Java] 纯文本查看 复制代码
package com.company.helloworld.firstapplication;


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

public class LeftFragment extends Fragment {


    public LeftFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_left, container, false);
    }

}

fragment_left.xml
[XML] 纯文本查看 复制代码
<FrameLayout 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"
    android:background="@color/colorAccent"
    tools:context="com.company.helloworld.firstapplication.LeftFragment">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="左边Fragment"
        android:textColor="@android:color/white" />

</FrameLayout>
RightFragment
[Java] 纯文本查看 复制代码
package com.company.helloworld.firstapplication;


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


/**
 * A simple {@link Fragment} subclass.
 */
public class RightFragment extends Fragment {


    public RightFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_right, container, false);
    }

}

fragment_right.xml
[XML] 纯文本查看 复制代码
<FrameLayout 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"
    android:background="@color/colorPrimary"
    tools:context="com.company.helloworld.firstapplication.RightFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="右边Fragment"
        android:gravity="center"
        android:textColor="@android:color/white" />

</FrameLayout>


第一种方法(在布局中静态加载):
主布局activity_main.xml中:

[XML] 纯文本查看 复制代码
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <!--android:name指定加载的Fragment-->
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.company.helloworld.firstapplication.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.company.helloworld.firstapplication.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>


运行结果如下图:

         QQ截图20181217105434.png
        

第二种方法(动态加载):
主布局activity_main.xml:

[XML] 纯文本查看 复制代码
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

MainActivity:
[Java] 纯文本查看 复制代码
package com.company.helloworld.firstapplication;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LeftFragment leftFragment = new LeftFragment();

        FragmentManager supportFragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_content, leftFragment).commit();
    }

}


运行结果:

         QQ截图20181217113415.png
        


因为android 3.0以下版本没有fragment的api,所以必须借助V4包里面的getSupportFragmentManager()方法来间接获取FragmentManager()对象,同时继承FragmentActivity
而android 3.0版本之后有了fragment的API,所以通过getFragmentManager()方法直接就可以获取到FragmentManager对象,并且继承自Activity就可以嵌入使用fragment


注意:每个Fragment和Activity导的包要保持一致,例如都是import android.app或者都是import android.support.v4.app,不然容易出错





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-17 15:12:57 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-17 17:01:18 | 显示全部楼层
感恩无私的分享与奉献
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-18 17:58

© 2014-2021

快速回复 返回顶部 返回列表