目录
Fragment回退栈
ContentFragment fragment = new ContentFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_content,fragment);
ft.addToBackStack(); //回退栈方法
ft.commit();
清空回退栈
FragmentManager fm= getFragmentManager();
int count = fm.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
fm.popBackStack();//清空回退栈中的内容
}
Fragment接口回调
两个Fragment之间无法直接传递数据,需要一个中间桥梁可以是Hrealer或接口回调、

什么是接口回调

定义一个外部接口
public interface CallBackListeners {
void setData(String s);
}
在第一个Fragment中传递数据
HomeFragment homeFragment =new HomeFragment ();
homeFragment .setData("我是接口回掉传递的数据");
在第二个Fragment实现接口并重写方法
public class MyFragment extends Fragment implements CallBackListeners{
...
public void setData(String data) {
Toast.makeText(this,data, Toast.LENGTH_SHORT).show(); }
}
Fragment通信
在Fragment里传递数据
public class F2AActivity extends AppCompatActivity implements ShowTitleFragment.MyListener {
private TextView textView;
@Override
public void sendMessage(String string) {
textView.setText(string);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f_2_a);
textView = findViewById(R.id.f2a_tv_id);
}
}
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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".F2AActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/f2a_tv_id"
android:textSize="30sp"
android:hint="提示而已"/>
<fragment
android:name="com.example.day004.fragment.ShowTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/f2a_fm_id">
</fragment>
</LinearLayout>
Fragment中代码
public class ShowTitleFragment extends Fragment {
private EditText editText;
private Button button;
private MyListener listener;
public ShowTitleFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
//拿到与当前fragment关联的Activity.
listener = (MyListener) getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_show_title, container, false);
//取到所有控件
editText = inflate.findViewById(R.id.fm_title_et_id);
button = inflate.findViewById(R.id.fm_title_button_id);
//点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
if(string != null){
//注意了.这里是父类引用指向了子类对象,其实是activity中的sendmessage方法在执行.
listener.sendMessage(string);
}
}
});
//返回fragment中的布局视图
return inflate;
}
Fragment中的多层嵌套
直接上代码
/**
* 底部导航
*/
public class SelectActivity extends AppCompatActivity {
private FrameLayout frameLayoutId;
private RadioGroup radioGroupId;
private RadioButton personId;
private RadioButton messageId;
private static final String TAG = "SelectActivity";
private FragmentManager manager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
frameLayoutId = findViewById(R.id.frame_layout_id);
radioGroupId = findViewById(R.id.radio_group_id);
radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.person_id:
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
// fragmentTransaction.replace(R.id.frame_layout_id,new FristFragment());
fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
fragmentTransaction.addToBackStack("11");
Log.i(TAG, "onCheckedChanged: "+SelectActivity.class.getName());
fragmentTransaction.commit();
break;
case R.id.message_id:
manager = getSupportFragmentManager();
manager.popBackStack();
SelectActivity.this.fragmentTransaction = manager.beginTransaction();
SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
// fragmentTransaction.addToBackStack(null);
SelectActivity.this.fragmentTransaction.commit();
break;
default:
break;
} } });
}
}
练习:

本文介绍了Fragment在Android开发中的高级用法,包括如何使用回退栈进行管理,如何实现Fragment之间的数据交互,以及如何处理Fragment的多层嵌套。通过实例演示,帮助开发者理解关键概念并进行实践操作。
&spm=1001.2101.3001.5002&articleId=117279296&d=1&t=3&u=5c395dab962244928ed3a35437f54fc8)
2570

被折叠的 条评论
为什么被折叠?



