Kotlin Fragment的具体使用详解

 更新时间:2022年10月09日 10:45:13   作者:知奕奕  
Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段

概念

fragment 可以用作一个 activity 内部的小分块;

当我们从手机转换到 pad 上时,整体界面会发生变化(比如由单列视图变为双列),此时就需要 fragment 的参与了!

基本示例

在本实例中,我们要制作一个双列视图,左右列均为 fragment 构成

设置左右列布局文件

新建布局文件 left_frag.xmlright_frag.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">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_btn"
        android:text="左边的按钮"
        android:layout_gravity="center_horizontal"/>
</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="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_text"
        android:text="右边的frag"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

配置左右布局类

一般的,所有 fragment 都需要一个单独的类来对其页面进行渲染,以及部分事件处理;

创建类 LeftFrag.kt

使该类继承 Fragment,并实现方法,渲染 fragment:

这里使用了 inflater 对页面进行注册;

package com.zhiyiyi.listviewdemo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class LeftFrag : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.left_frag, container, false)
    }
}

主布局文件注册

我们需要在主 activity 的布局文件中使用这两个 fragment;

直接添加两个 fragment 标签,在 name 属性写上 fragment 布局处理的类即可;

<?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">
    <fragment
        android:id="@+id/leftfrag"
        android:name="com.zhiyiyi.listviewdemo.LeftFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/rightfrag"
        android:name="com.zhiyiyi.listviewdemo.RightFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

这边注册完毕后就大功告成了,直接运行看看成果把!

到此这篇关于Kotlin Fragment的具体使用详解的文章就介绍到这了,更多相关Kotlin Fragment内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • Android开发之ViewSwitcher用法实例

    Android开发之ViewSwitcher用法实例

    这篇文章主要介绍了Android开发之ViewSwitcher用法,结合实例形式分析了ViewSwitcher的功能、使用方法与相关注意事项,需要的朋友可以参考下
    2016-02-02
  • Android 接收推送消息跳转到指定页面的方法

    Android 接收推送消息跳转到指定页面的方法

    这篇文章主要介绍了Android 接收推送消息跳转到指定页面的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Android 选择相册照片并返回功能的实现代码

    Android 选择相册照片并返回功能的实现代码

    这篇文章主要介绍了Android 从相册中选择照片并返回功能,需要的朋友可以参考下
    2018-03-03
  • Android获取SD卡路径及SDCard内存的方法

    Android获取SD卡路径及SDCard内存的方法

    这篇文章主要介绍了Android获取SD卡路径及SDCard内存的方法,较为详细的分析了Android针对SD卡操作所涉及的类及其具体函数功能,非常具有实用价值,需要的朋友可以参考下
    2015-02-02
  • Android中的webview监听每次URL变化实例

    Android中的webview监听每次URL变化实例

    这篇文章主要介绍了Android中的webview监听每次URL变化实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android实现简单的banner轮播图

    Android实现简单的banner轮播图

    这篇文章主要为大家详细介绍了Android实现简单的banner轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • android编程实现的自定义注释模板实例

    android编程实现的自定义注释模板实例

    这篇文章主要介绍了android编程实现的自定义注释模板,以完整实例形式分析了Android自定义魔板的定义及具体实现与使用技巧,需要的朋友可以参考下
    2015-11-11
  • Android天气预报app改进版

    Android天气预报app改进版

    这篇文章主要为大家详细介绍了改进版的Android天气预报app,内容更加充实,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • 深入解析Android中View创建的全过程

    深入解析Android中View创建的全过程

    这篇文章主要给大家深入的解析了关于Android中View创建的全过程,文中介绍的非常详细,相信对大家会有一定的参考借鉴,需要的朋友们下面来一起学习学习吧。
    2017-03-03
  • Android 判断是否连接成功了指定wifi

    Android 判断是否连接成功了指定wifi

    本文主要介绍了Android 判断是否连接成功了指定wifi的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04

最新评论