Fragment的概念及使用(静态添加与动态添加碎片)
- 概念
- 案例:静态的插入碎片
- 案例:动态的插入碎片
概念
Fragment是Android3.0后引入的一个新的API,它出现的初衷是为了适应大屏幕的平板电脑,我们可以把他看成一个小型的Activity,又称Activity片段!使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,它需要嵌套在Activity 中使用,也可嵌套另一个Fragment,尽管它拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity被destory销毁了,它也会跟着销毁!
案例:静态的插入碎片
把碎片放到主界面中,即activity_main中
素材:left_fragment、right_fragment、LeftFragment、RightFragment、activity_main、MainActivity
下面展示代码
left_fragment代码如下:
// 平板中左边碎片1区域
<?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">
<Button
android:id="@+id/btn"
android:text="left fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
right_fragment代码如下:
// 平板中右边碎片2区域
<?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">
<TextView
android:id="@+id/tv"
android:text="right fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
LeftFragmet代码如下:
//新建的LeftFragment类必须继承自Fragment,导包时使用androidx,其它也必须使用androidx
//重写Fragment的oncreateview方法,在方法中通过解析器LayoutInflater的inflate方法将定义的左边布局动态left_fragment加载进来
package com.myapp.weixin;
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;
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
RightFragment代码如下:
package com.myapp.weixin;
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;
public class RightFragment 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;
}
}
把碎片放到主界面中,即activity_main中,代码如下:
<?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">
<fragment
android:id="@+id/left_fragment"
android:name="com.myapp.weixin.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/right_fragment"
android:name="com.myapp.weixin.RightFragment"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"/>
</LinearLayout>
运行效果如下:
目前未对按钮进行点击及跳转事件
案例:动态的插入碎片
效果:当点击左边碎片区域按钮时,右边碎片区域文本变为另一个文本
把碎片放到主程序中,即MainActivity.java中。在主界面中把右边的碎片放到FrameLayout容器中
素材:another_fragment、AnotherFragment、activity_main、MainActivity
activity_main代码与静态插入碎片有所变化,代码如下:
// 在主界面中把右边的碎片放到FrameLayout容器中
// <?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">
<fragment
android:id="@+id/left_fragment"
android:name="com.myapp.weixin.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_weight="3"
android:layout_width="0dp"
android:id="@+id/right_layout"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity代码如下:
// package com.myapp.weixin;
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new RightFragment());
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new AnotherFragment());
}
});
}
//替换Fragment
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
//开始事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
//提交
transaction.commit();
}
}
上述代码理解:
another_fragment代码如下:
// <?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=".AnotherFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello_blank_fragment" />
</FrameLayout>
AnotherFragment代码如下:
// package com.myapp.weixin;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AnotherFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.another_fragment, container, false);
}
}
运行效果:
注:
静态添加碎片代表那个页面已经固定了,不能再变动;而动态添加碎片表示可以对该页面进行变化