ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Fragment(碎片)

2021-09-19 19:04:18  阅读:180  来源: 互联网

标签:Fragment inflater 碎片 fragment import view View


Fragment(碎片)

碎片:可以当做一种可以嵌入在活动当中的UI控件,它开始是为了让程序更加合理和充分地利用大屏幕的空间;但是,目前fagement更多的当做一种布局形式;

1.fragment的生命周期

fragment的生命周期和activity的生命周期十分类似,除了onCreate(),onStart(),onResume(),onPause(),onStop(),onDestory()又多出了以下的生命周期。

  • 1.onAttach()

一般是给Fragment添加回调接口,让Activity继承并实现。

  • 2.onCreateView()

fragment通过重写该方法获取view页面。这个方法在每一个Fragment实体类中都会被重写;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, container, false);
        return view;
    }
  • 3.onActivityCreate()

当Activity的onCreate方法返回时调用。

  • 4.onDestoryView()

与onCreateView想对应,当该Fragment的视图被移除时调用

  • 5.onDetach()

与onAttach相对应,当Fragment与Activity关联被取消时调用。

Activity与Fragment生命周期对比图

2.创建一个简单的Fragment项目

  • 创建FragmentTest项目

  • 创建一个碎片OneFragment;

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

    碎片都是实现了Fragment类,总是需要重写onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) 方法;

  • 创建一个与上面OneFragement相对应的布局;

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".fragment.OneFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment" 
            android:layout_gravity="center"
            android:gravity="center"/>
    
    </FrameLayout>
    

    这是一个位于res/layout文件夹下的布局xml文件

  • 改写OneFragment中的onCreateView()方法

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);
            return view;
        }
    

    此方法作为Activity从Fragment中获取到View的依据;

  • 修改activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        tools:context=".MainActivity">
    
    <fragment
        android:id="@+id/one_fragment"
        android:name="com.example.fragmenttest.fragment.OneFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
    
    </LinearLayout>
    

    可以看到,直接使用标签进行引用,通过name调用fragment

    展示视图

3.实现页面跳转

通过上面的例子的看出,fragment最适合做的事情不止是兼容不同屏幕,布局中可以非常方便展示;在OneFragment中仅仅使用了一块碎片,还可以使用碎片使页面跳转

  • 在上面的项目中再次添加fragment,创建TwoFragment.java

    public class TwoFragment extends Fragment {
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_two, container, false);
            return view;
        }
    }
    
  • 同样需要创建该碎片的布局文件fragment_two.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".fragment.TwoFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="blank_fragment" 
            android:layout_gravity="center"
            android:gravity="center"/>
    
    </FrameLayout>
    
  • 此时activity_main需要进行修改

    <?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">
    <LinearLayout
        android:id="@+id/frgment"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_weight="17">
    </LinearLayout>
    
    <Button
        android:id="@+id/one"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="One"
        />
    <Button
        android:id="@+id/two"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Two"
        />
    
    </LinearLayout>
    

    创建了两个按钮one和two实现跳转

  • 修改MainActivity.java中的代码

    package com.example.fragmenttest;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.example.fragmenttest.fragment.OneFragment;
    import com.example.fragmenttest.fragment.TwoFragment;
    
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button one = findViewById(R.id.one);
            Button two = findViewById(R.id.two);
            one.setOnClickListener(this);
            two.setOnClickListener(this);
        }
        
        public void replaceFragment(Fragment fragment){
            FragmentManager supportFragmentManager = getSupportFragmentManager();
             FragmentTransaction fragmentTransaction =supportFragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frgment,fragment);
            fragmentTransaction.commit();
        }
    
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.one:
                    replaceFragment(new OneFragment());
                    break;
                case R.id.two:
                    replaceFragment(new TwoFragment());
                    break;
                default:
                    break;
            }
        }
    }
    
  • MainActivity.java中实现页面跳转的核心方法

private void replace(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //1.传递的是id名
        fragmentTransaction.replace(R.id.main, fragment);
        fragmentTransaction.commit();
    }

点击one展示:

点击two展示

标签:Fragment,inflater,碎片,fragment,import,view,View
来源: https://www.cnblogs.com/ouyangbo12/p/15312358.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有