Fragment 一
作者:互联网
今天学习了Fragment 的创建:静态和动态.代码比较简单,加有注释,不再详解。
Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!
首先是文件
MainActivit
package com.example.fragment; import android.content.Intent; import android.os.Bundle; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //静态 public void staticFragment(View view) { Intent intent = new Intent(MainActivity.this, StaticFragmentActivity.class); startActivity(intent); } //动态 public void DynameicFragment(View view) { Intent intent = new Intent(MainActivity.this, DynameicFragmentActivity.class); startActivity(intent); } }
StacticFragementActivity
package com.example.fragment; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class StaticFragmentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_staticfragment); } }
package com.example.fragment; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import com.example.fragment.fragment.DynameicFragment; public class DynameicFragmentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dynameicfragment); if (savedInstanceState == null) { //得到FragmentManager FragmentManager fragmentManager = getSupportFragmentManager(); //得到FragmentTransaction 一次事务打包的操作 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.dynameicfragment, DynameicFragment.class, null) .setReorderingAllowed(true) //.addToBackStack(null) //压缩到栈里 .commit() ; } } }
DynameicFragment
package com.example.fragment.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.fragment.R; /** * A simple {@link Fragment} subclass. * Use the {@link DynameicFragment#newInstance} factory method to * create an instance of this fragment. */ public class DynameicFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public DynameicFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment DynameicFragment. */ // TODO: Rename and change types and number of parameters public static DynameicFragment newInstance(String param1, String param2) { DynameicFragment fragment = new DynameicFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_dynameic, container, false); } }
StaticFragment
package com.example.fragment.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; import com.example.fragment.R; /** * A simple {@link Fragment} subclass. * Use the {@link StaticFragment#newInstance} factory method to * create an instance of this fragment. */ public class StaticFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public StaticFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment StaticFragment. */ // TODO: Rename and change types and number of parameters public static StaticFragment newInstance(String param1, String param2) { StaticFragment fragment = new StaticFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_static, container, false); } }
activity_dynameicfragment.xml
<?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=".DynameicFragmentActivity"> <androidx.fragment.app.FragmentContainerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dynameicfragment" > </androidx.fragment.app.FragmentContainerView> </LinearLayout>
activity_main_xml
<?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=".MainActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="center" android:text="静态Fragment" android:gravity="center" android:onClick="staticFragment" android:layout_gravity="center_horizontal" android:textAllCaps="false" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="center" android:text="动态Fragment" android:gravity="center" android:layout_gravity="center_horizontal" android:textAllCaps="false" android:onClick="DynameicFragment" ></Button> </LinearLayout>
activity_staticfragment.xml
<?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=".StaticFragmentActivity"> <fragment android:id="@+id/staric" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.fragment.fragment.StaticFragment" tools:layout="@layout/fragment_static" ></fragment> </LinearLayout>
fragment_dynameic.xml
<?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=".fragment.DynameicFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="这是动态fragment" /> </FrameLayout>
fragment_static.xml
<?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=".fragment.StaticFragment" > <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这是静态fragment" android:gravity="center"/> </FrameLayout>
展示效果
标签:Fragment,savedInstanceState,Bundle,fragment,ARG,import,public 来源: https://www.cnblogs.com/zhaolei0419/p/16210631.html