fragment学习和使用
1.新学期目标2.打卡3.打卡24.打卡35.打卡 46.打卡57.打卡68.打卡79.打卡810.打卡911.打卡1012.结对作业(地铁查询项目)0113.结对作业(地铁项目)0214.结对作业(地铁项目)0315.结对作业(地铁项目)0416.结对作业(地铁项目)0517.结对作业(地铁项目)0618.结对作业(地铁项目)0719.结对作业(地铁项目)0820.结对作业(地铁项目)0921.结对作业(地铁项目)1022.结对作业(地铁项目)1123.结对作业(地铁项目)1224.结对作业(地铁项目)1325.结对作业(地铁项目)1426.五一冲刺(政策查询系统)127.五一冲刺(政策查询系统)228.政策查询系统(安卓)129.政策查询系统(安卓)230.政策查询系统(安卓)331.政策查询系统(安卓)432.政策查询系统(安卓)533.政策查询系统(安卓)634.政策查询系统(安卓)735.JS开发36.安卓app开发相关37.第一次个人作业(安卓学习记录系统)0138.第一次个人作业(安卓学习记录系统)0239.第一次个人作业(安卓学习记录系统)0340.第一次个人作业(安卓学习记录系统)0441.第一次个人作业(安卓学习记录系统)0542.第一次个人作业(安卓学习记录系统)0643.第一次个人作业(安卓学习记录系统)0744.打卡1145.打卡1246.打卡1347.打卡1448.打卡1549.打卡1650.打卡1751.打卡1852.打卡1953.打卡2054.python学习55.python学习256.python学习357.Javaweb58.打卡21
59.fragment学习和使用
60.mybits学习161.mybits学习262.mybits学习363.课程总结64.个人总结
所花时间(包括上课): |
4h |
代码量(行): |
300左右 |
搏客量(篇): |
1 |
了解到的知识点: |
fragment |
备注(其他): |
这里先介绍以下Fragment的两种创建方式:静态创建和动态创建
静态创建和动态创建的生命周期:
静态创建是先创建一个fragment,在创建activity
动态创建是先创建一个activity,在创建一个fragment
一、静态创建
源码如下:
fragment文件
package com.example.fragment.fragment; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RatingBar; import android.widget.TextView; import android.widget.Toast; import com.example.fragment.R; public class StaticFragment1 extends Fragment { 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; private TextView tvlike; private RadioButton rblike, rbDislike; private RatingBar rbstar; public StaticFragment1() { // 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 StaticFragment1. */ // TODO: Rename and change types and number of parameters public static StaticFragment1 newInstance(String param1, String param2) { StaticFragment1 fragment = new StaticFragment1(); 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_static1, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); rblike = view.findViewById(R.id.rb_like); rbDislike = view.findViewById(R.id.rb_dislike); tvlike = view.findViewById(R.id.tv_like); rbstar = view.findViewById(R.id.rb_star); rblike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { tvlike.setText("app喜欢"); } } }); rbDislike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { tvlike.setText("app不喜欢"); } } }); rbstar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { if (fromUser) { Toast.makeText(getActivity(), "点了" + rating, Toast.LENGTH_SHORT).show(); } } }); } }
java文件
package com.example.fragment; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class StaticFragmentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static_framgent); } }
1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".fragment.StaticFragment1" android:orientation="vertical" android:background="#748751" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢" android:textSize="20sp" android:id="@+id/tv_like" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_like" android:text="喜欢" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_dislike" android:text="不喜欢" /> </RadioGroup> </LinearLayout> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_star"> </RatingBar> </LinearLayout>
2.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" android:orientation="vertical" tools:context=".StaticFragmentActivity"> <fragment android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" tools:layout="@layout/fragment_static1 " android:name="com.example.fragment.fragment.StaticFragment1" android:id="@+id/fragment" /> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragment2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" tools:layout="@layout/fragment_static1 " android:name="com.example.fragment.fragment.StaticFragment1" /> </LinearLayout>
实现效果如下:
二、动态创建
源码如下:
fragment文件
package com.example.fragment.fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RatingBar; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.example.fragment.R; public class ExampleFragment extends Fragment { public static final String ARG_PARAM1 = "param1"; public static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private TextView tvlike,tvcontent; private RadioButton rblike, rbDislike; private RatingBar rbstar; public ExampleFragment() { // 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 StaticFragment1. */ // TODO: Rename and change types and number of parameters public static ExampleFragment newInstance(String param1, String param2) { ExampleFragment fragment = new ExampleFragment(); 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.frangment_example, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); rblike = view.findViewById(R.id.rb_like); rbDislike = view.findViewById(R.id.rb_dislike); tvlike = view.findViewById(R.id.tv_like); rbstar = view.findViewById(R.id.rb_star); tvcontent=view.findViewById(R.id.tv_content); rblike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { tvlike.setText("app喜欢"); } } }); rbDislike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { tvlike.setText("app不喜欢"); } } }); rbstar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { if (fromUser) { Toast.makeText(getActivity(), "点了" + rating, Toast.LENGTH_SHORT).show(); } } }); tvcontent.setText(mParam1); } }
java文件
package com.example.fragment; import static androidx.fragment.app.FragmentManager.TAG; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.util.Log; import com.example.fragment.fragment.ExampleFragment; public class DynamicFragmentActivity extends AppCompatActivity { private static final String TAG="DynamicFragmentActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dynamic_fragment); Log.d(TAG,"onCreate:saveInstance"+savedInstanceState); if(savedInstanceState==null) //防止多次添加 { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Bundle bundle=new Bundle(); bundle.putString(ExampleFragment.ARG_PARAM1,"这是动态Fragment"); fragmentTransaction.add(R.id.fcv, ExampleFragment.class, bundle) .setReorderingAllowed(true)//顺序可以更改 .commit(); } } }
1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".fragment.ExampleFragment" android:orientation="vertical" android:background="#748751" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢" android:textSize="20sp" android:id="@+id/tv_like" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_like" android:text="喜欢" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_dislike" android:text="不喜欢" /> </RadioGroup> </LinearLayout> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_star"> </RatingBar> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_content" android:textSize="25sp" /> </LinearLayout>
2.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" android:orientation="vertical" tools:context=".DynamicFragmentActivity"> <androidx.fragment.app.FragmentContainerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fcv" /> </LinearLayout>
效果展示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效