建立fragment(动态静态)

Posted JIANGzihao0222

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了建立fragment(动态静态)相关的知识,希望对你有一定的参考价值。

fragment建立

一:静态建立

StaticFragmentActivity:
//建立StaticFragmentActivity来承载fragment
public class StaticFragmentActivity extends AppCompatActivity 

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static_fragment);


布局:提前绑定:
<?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_
android:layout_
tools:context=".StaticFragmentActivity">
<fragment
android:id="@+id/fragment"
android:layout_
android:layout_
tools:layout="@layout/fragment_static"
android:name="com.example.fragment.fragment.StaticFragment"
/>

</LinearLayout>
绑定内容:
<fragment
android:id="@+id/fragment"
android:layout_
android:layout_
tools:layout="@layout/fragment_static"
android:name="com.example.fragment.fragment.StaticFragment"
/>
建立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);

private TextView tvLike;
private RadioButton rbLike,rbDislike;
private RatingBar rbStar;
@Override

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
tvLike = view.findViewById(R.id.tv_like);
rbLike = view.findViewById(R.id.rb_like);
rbDislike = view.findViewById(R.id.rb_dislike);
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();


);

在onViewCreated内进行操作,设立具体的事件。
fragment布局:
<?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_
android:layout_
tools:context=".fragment.StaticFragment"
android:padding="10dp"
android:orientation="vertical">

<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_
android:layout_
android:text="喜欢"
android:textSize="20dp"
android:id="@+id/tv_like"/>
<RadioGroup
android:layout_
android:layout_
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_like"
android:layout_
android:layout_
android:text="喜欢"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/rb_dislike"
android:layout_
android:layout_
android:text="不喜欢"
android:textSize="20sp"
/>
</RadioGroup>

</LinearLayout>
<RatingBar
android:layout_
android:layout_
android:id="@+id/rb_star"
/>
</LinearLayout>

二:动态建立

activity方面:DynamicFragmentActivity

public class DynamicFragmentActivity extends AppCompatActivity 

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_fragment);
if(savedInstanceState == null)//判断第一次点入,防止操作丢失

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//事务
fragmentTransaction.add(R.id.fcv, DynamicFragment.class,null)
.setReorderingAllowed(true)
.addToBackStack(null)
.commit();



核心代码:
           FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//事务
fragmentTransaction.add(R.id.fcv, DynamicFragment.class,null)
.setReorderingAllowed(true)//可省
.addToBackStack(null)//可省

.commit();
DynamicFragmentActivity布局:
<?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_
android:layout_
tools:context=".DynamicFragmentActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_
android:layout_
/>

</LinearLayout>
//进行具体替换的地方
 <androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_
android:layout_
/>
动态fragment:DynamicFragment
public class DynamicFragment 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 DynamicFragment()
// 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 DynamicFragment.
*/
// TODO: Rename and change types and number of parameters
public static DynamicFragment newInstance(String param1, String param2)
DynamicFragment fragment = new DynamicFragment();
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_dynamic, container, false);

private TextView tvLike;
private RadioButton rbLike,rbDislike;
private RatingBar rbStar;
@Override

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
tvLike = view.findViewById(R.id.tv_like);
rbLike = view.findViewById(R.id.rb_like);
rbDislike = view.findViewById(R.id.rb_dislike);
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();


);

DynamicFragment 布局:
<?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_
android:layout_
tools:context=".fragment.DynamicFragment"
android:padding="10dp"
android:orientation="vertical">

<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_
android:layout_
android:text="喜欢"
android:textSize="20dp"
android:id="@+id/tv_like"/>
<RadioGroup
android:layout_
android:layout_
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_like"
android:layout_
android:layout_
android:text="喜欢"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/rb_dislike"
android:layout_
android:layout_
android:text="不喜欢"
android:textSize="20sp"
/>
</RadioGroup>

</LinearLayout>
<RatingBar
android:layout_
android:layout_
android:id="@+id/rb_star"
/>
</LinearLayout>


Android:Fragment

Fragment:小型的Activity,需要嵌套在Activity中使用,受宿主Activity的生命周期影响;

/*图片取自菜鸟教程,侵删*/

 1>静态加载流程.

 2>动态加载Fragment;

 

以上是关于建立fragment(动态静态)的主要内容,如果未能解决你的问题,请参考以下文章

Fragment学习总结

Android Fragment的使用

动态添加Fragment

Uep的静态下拉和动态下拉建立

Fragment的使用

Android之碎片Fragment