当前位置: 移动技术网 > IT编程>开发语言>Java > Fragment的介绍以及加载详细说明

Fragment的介绍以及加载详细说明

2020年07月24日  | 移动技术网IT编程  | 我要评论

Fragment与Activity的区别
1.Feagment是安装3.0之后才有的
2.一个Activity可以运行多个Fragment
3.Fragment不能脱离Activity而单独存在
4.Activity是屏幕的主体,而Fragment是Activity的一个组成元素
Fragment的使用
先创建一个Frangment的java文件

public class ListFragment extends Fragment {   
    private String mTitle = "imooc";
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // new view
    View view = inflater.inflate(R.layout.fragment_list, container, false);

    TextView textView = view.findViewById(R.id.textView);

    textView.setText(mTitle);

创建出Fragment的视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent">


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="TextView"
    android:textColor="#FFFFFF"
    android:textSize="20sp"/>
</RelativeLayout>

一:静态加载
设置一个静态加载的按钮

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center"
    android:text="static load fragment"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

实现点击事件

findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // static load fragment.
        startActivity(new Intent(MainActivity.this, StaticLoadFragmentActivity.class));
        }
    });

创建相对应的java文件

public class StaticLoadFragmentActivity extends AppCompatActivity{
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_static_load_fragment);
}
}

做出对应的视图,要用Fragment

<RelativeLayout
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="vertical"
tools:context="iom.imooc.fragmentdemo.MainActivity">

<fragment
   android:id="@+id/listFragment"
   android:name="iom.imooc.fragmentdemo.ListFragment"
   android:layout_width="100dp"
   android:layout_height="100dp"/>


<fragment
   android:id="@+id/detailFragment"
   android:name="iom.imooc.fragmentdemo.ListFragment"
   android:layout_centerInParent="true"
   android:layout_width="100dp"
   android:layout_height="100dp"/>

</RelativeLayout>

如此便完成了静态加载的过程
二:动态加载
1.先创建容器
在主页面创建容器

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/listContainer"
    android:layout_width="150dp"
    android:layout_margin="1dp"
    android:layout_height="match_parent">

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/detailContainer"
    android:layout_width="200dp"
    android:layout_margin="1dp"
    android:layout_height="match_parent">

</LinearLayout>
</LinearLayout>

2.调用Fragment,同时将Fragment放入容器当中,在主要的java文件的Oncreate方法中写入
在这里插入图片描述

便完成了动态加载的方法

本文地址:https://blog.csdn.net/Derrick_itRose/article/details/107513498

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网