Fragment就是一种可以嵌入活动当中的UI片段,可以把不同的UI片段放到同一个活动中,充分利用屏幕空间,经常使用在平板和车机等又大又长的屏幕中。
碎片可以理解为一个简单的小活动,通过碎片我们就可以在一个大活动中,加载不同的小活动
一,使用方法理解:
1.正常创建布局 xxx_fragment;例如:news_content_fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="@+id/news_title_in_content_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="hello"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000" />
<TextView
android:id="@+id/news_content_in_content_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"
android:text="shit bro"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
2.创建xxxFragment类继承Fragment,例如NewsContentFragment
仅仅只用重写onCreateView就可以将 对应的xml布局加载入碎片
package com.example.newsapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import org.w3c.dom.Text;
public class NewsContentFragment extends Fragment {
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.news_content_fragment, container, false);
return view;
}
public void refresh(String newsTitle, String newsContent) {
TextView newsTitleTv = view.findViewById(R.id.news_title_in_content_fragment);
TextView newsContentTv = view.findViewById(R.id.news_content_in_content_fragment);
newsTitleTv.setText(newsTitle);
newsContentTv.setText(newsContent);
}
}
3.在需要放碎片的布局中放入刚刚创建的NewsContentFragment,这里我放到了activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.newsapp.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
到此,一个简单的碎片就被我们加载到了需要的画面中。
二,动态更换碎片
j将布局替换为碎片
public void replaceFragment(Fragment fragment){
Fragemnt fragment=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
三,碎片和活动进行通信
活动和碎片常常处于两个类中,为了方便碎片和活动之间进行通信,提供了如下方法进行通信。
在Activity中,可以通过fragmentManager的findWithById找到碎片实例
NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(newsTitle, newsContent);
在Fragment中,可以通过getActivity找到与fragment相关联的活动
MainActivity activity=(MainActivity)getActivity();
四,碎片的生命周期
几个重点回调
onCreateView(),为碎片加载布局时调用,最常用
onActicityCreated(),确保碎片相关联的活动一定创建完毕时调用
onDestroy(),当与碎片关联的视图被移除时调用。
onDeatch(),当碎片和活动解除关联时调用
五,根据条件加载不同的布局
最小宽度限定符允许我i们对屏幕的宽度指定一个最小值,然后以这个最小值为零界点,大于这个值加载一个布局,小于这个值加载另一个布局。
比如,前提布局名字都叫activity_main,在res目录下新建layout-sw600dp的文件夹,创建activity_main.xml。屏幕宽度大于600dp加载layout-sw600dp里的activity_main布局,小于则加载layout里的activity_main布局