建立fragment(动态静态)

发布时间 2023-04-09 23:56:57作者: 实名吓我一跳

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_width="match_parent"
android:layout_height="match_parent"
tools:context=".StaticFragmentActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_static"
android:name="com.example.fragment.fragment.StaticFragment"
/>

</LinearLayout>
绑定内容:
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.StaticFragment"
android:padding="10dp"
android:orientation="vertical">

<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="20dp"
android:id="@+id/tv_like"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/rb_dislike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不喜欢"
android:textSize="20sp"
/>
</RadioGroup>

</LinearLayout>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".DynamicFragmentActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
//进行具体替换的地方
 <androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
动态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_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.DynamicFragment"
android:padding="10dp"
android:orientation="vertical">

<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="20dp"
android:id="@+id/tv_like"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/rb_dislike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不喜欢"
android:textSize="20sp"
/>
</RadioGroup>

</LinearLayout>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_star"
/>
</LinearLayout>