zoukankan      html  css  js  c++  java
  • Android基础——Fragment的创建和添加

    新建一个Fragment的过程

    package com.example.myactivityiiii;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    /*
    * 新建一个Fragment的过程
    *   1.自定义一个类,基础Fragment
    *   2.配置该类的layout文件
    *   3.重写onCreateView方法,加载该类的布局文件,返回一个View
    * */
    public class ListFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(
                    R.layout.fragment_list,container,false
            );
            return view;
        }
    }
    package com.example.myactivityiiii;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    import java.util.zip.Inflater;
    
    public class DetailFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(
                    R.layout.fragment_detail,container,false
            );
            return view;
        }
    }

    在Activity中添加Fragment

    1.直接通过layout添加:在Main的布局文件中添加以下代码

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:name="com.example.myactivityiiii.ListFragment"
        android:id="@+id/list"
        android:layout_weight="1"/>
    
    <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:name="com.example.myactivityiiii.DetailFragment"
        android:id="@+id/detail"
        android:layout_weight="2"/>

    2.在Activity运行时添加Fragment,主要修改的是Main中的代码

    在Main中的layout中添加一个布局容器,把新建的Fragment丢到这个布局容器里就好

    Main的layout

    <?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="horizontal"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Main"
            >
        </TextView>
    
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </FrameLayout>
    
    </LinearLayout>

    Main的java文件

    package com.example.myactivityiiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.os.Bundle;
    
    /*
    * Activity运行时添加Fragment
    * */
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            DetailFragment detailFragment = new DetailFragment();
            //获取Fragment的transaction实例
            FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
            //指定一个容器来添加detailFragment
            ft.add(R.id.frame,detailFragment);
            //提交事物
            ft.commit();
    
        }
    }
  • 相关阅读:
    Ex 6_20 最优二叉搜索树..._第六次作业
    Ex 6_12 凸多边形的最优三角剖分..._第六次作业
    Ex 6_9 某个字符串处理语言提供了一个将字符串一分为二的基本操作..._第六次作业
    Ex 6_4 判断序列是否由合法单词组成..._第六次作业
    maven配置阿里云镜像时(私服设置~JEECG)
    node、npm、webpack、vue-cli傻傻分不清?
    设计模式~观察者模式和发布订阅模式的比较:
    前端~定位属性position(relative、absolute、fixed)的分析
    debounce防抖函数减少函数调用的逻辑分析(包裹上时间的外衣,在时间还没来时,kill)
    js原生滚动与使用插件better-scroll不起作用原因
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12236182.html
Copyright © 2011-2022 走看看