先导部分
在开发有些应用程序的时候,平板设备和大尺寸手机屏幕较大,显示的功能和正常手机显示部分不相同,不过希望都能够有较好的视觉效果,因而界面的设置不同,需要有灵活多变的UI设计。但是activity自身并不能具有这样的灵活性,fragment就这样被用来管理应用UI,绕开Android系统activity使用规则的限制(也就是UI fragment)。
定义 UI fragment
管理用户界面的fragment又被称为UI fragment,它自己有产生于布局文件的视图,也包含了用户可以交互的可视化UI元素。而fragment是一种控制器对象,activity可委派他执行任务,这些任务通常就是管理用户界面,收管的用户界面可以是一整屏或是一整屏的一部分。
fragement切换
管理fragment生命周期
如下图所示,fragment生命周期与activity生命周期类似,它具有停止、暂停以及运行状态,也拥有可以覆盖的方法,用来在关键节点完成一些任务。而fragment生命周期与activity生命周期的一个关键区别在于,fragment生命周期由托管activity而不是操作系统调用,操作系统不关心activity用来管理视图的fragment。fragment的使用是activity内部的事。
创建 UI fragment
步骤1.定义用户布局文件
步骤2.创建fragment类并设置器视图为定义的布局
步骤3.编写代码以实例化组件
初始化工作
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="center_vertical" android:text="这里是MainActivity" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/ButtonShowFragmentNow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示FragmentNow" android:textAllCaps="false" /> <Button android:id="@+id/ButtonShowFragmentTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示FragmentTwo" android:textAllCaps="false" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/ButtonAmendFragmentTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改FragmentTwo" android:textAllCaps="false" /> <Button android:id="@+id/ButtonReplaceFragmentNow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="替换" /> <Button android:id="@+id/ButtonDeleteFragmentNow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除FragmentNow" android:textAllCaps="false" /> </LinearLayout> <TextView android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:text="这就是用来被Fragment修改的" android:textAllCaps="false" android:textSize="18sp" /> <FrameLayout android:id="@+id/FrameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#D02090" /> </LinearLayout>
定义两个fragment布局文件
fragment_now
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#9aff9a"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ButtonFragmentNow" android:text="修改Activity" android:textAllCaps="false" android:layout_gravity="center"/> </LinearLayout>
fragment_two
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d1eeee" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextVIewFragmentTwo" android:text="注定被修改" android:layout_gravity="center" android:textSize="30sp"/> </LinearLayout>定义两个Fragment类
public class FragmentTwo extends Fragment { private View mView; private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment_two, null); return mView; } public void Amend(){ mTextView = (TextView) mView.findViewById(R.id.TextVIewFragmentTwo); mTextView.setText("这是FragmentTwo中已经被修改了的值"); } }
public class FragmentNow extends Fragment { private View mView; private Button mButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment_now, null); mButton = (Button) mView.findViewById(R.id.ButtonFragmentNow); return mView; } @Override public void onStart() { super.onStart(); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity mainActivity = (MainActivity) getActivity(); mainActivity.Amend(); } }); } }
写入MainActivity
private void init() { mFragmentNow = new FragmentNow(); mFragmentTwo = new FragmentTwo(); mButtonShowFragmentNow = (Button) findViewById(R.id.ButtonShowFragmentNow); mButtonShowFragmentTwo = (Button) findViewById(R.id.ButtonShowFragmentTwo); mButtonAmendFragmentTwo = (Button) findViewById(R.id.ButtonAmendFragmentTwo); mButtonReplaceFragmentNow = (Button) findViewById(R.id.ButtonReplaceFragmentNow); mButtonDeleteFragmentNow = (Button) findViewById(R.id.ButtonDeleteFragmentNow); mBoolean = true; }
mButtonAmendFragmentTwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mFragmentTwo.Amend(); } }); mButtonDeleteFragmentNow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction() .remove(mFragmentNow).commit(); } }); public void Amend() { TextView textView = (TextView) findViewById(R.id.TextView); textView.setText("你看吧,Activity中的TextView已经被修改了。"); }对于fragment,需要坚持AUF(Always Use Fragments)原则,即“总是使用fragment”。
参考自https://blog.csdn.net/cc_xz/article/details/61618922