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();
    
        }
    }
  • 相关阅读:
    Service Mesh 在百度网盘数万后端的落地实践
    垃圾回收GC3种算法的衍生品 增量回收:预测和控制GC所产生的中断时间
    cv_list
    段式内存管理
    页式内存管理
    基本内存管理
    内存抖动
    内存管理 垃圾回收 C语言内存分配 垃圾回收3大算法 引用计数3个缺点
    DEDECMS后台传附件图片出现Upload filetype not allow解决办法
    浅谈dedecms模板引擎工作原理及自定义标签
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12236182.html
Copyright © 2011-2022 走看看