这一篇是看了郭神的《第一行代码》写的会有很多的借用主要是做个笔记,后面有新的内容会添加,如果有错误帮忙指正。
首先是导入对应的依赖
implementation 'androidx.fragment:fragment:1.0.0'
Fragment和Activity一样都由一个java类和一个布局文件构成。所以接下来是创建一个Fragment的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#00ff00"
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:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"/>
</LinearLayout>
再是与之对应的java类
package com.example.thefirstcodestudy.FragmentPackge;
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 com.example.thefirstcodestudy.R;
public class right_fragmnet extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment,container,false);
//代码
return view;
}
}
在创建类的时候要注意这个类是继承于Fragment的,同时要实现他的onCreateView方法,在这个方法中我们要用inflater来刚才创建的布局文件。如果在这一个Fragment中要实现一些东西的话可以和在activity中类似的形式来进行编写。
Fragment是依附于Activity而存在的,所以我们不能单独的使用Fragment,要创建对应的Activity。
Activity布局文件
<?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"
tools:context=".FragmentPackge.reStudyFragment">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/right_layout"
/>
</LinearLayout>
Activity的java文件
package com.example.thefirstcodestudy.FragmentPackge;
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.thefirstcodestudy.R;
public class reStudyFragment extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_re_study_fragment);
replaceFragment(new right_fragmnet());
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
要实现Fragment的相关操作不可避免的会使用到FragmentManger和FragmentTransation。我们可以通过FragmentManger得到FragmentTransation而替换操作都可以通过他来实现。在进行操作之后通过commit方法来提交才可以实现效果。而代码中的transaction.addToBackStack(null);可以用来实现类似于Activity的返回栈的效果。