FragmentContainerView extends FrameLayoutFragmentContainerView是专门为Fragments设计的自定义布局。它扩展了FrameLayout,因此它可以可靠地处理Fragment 事
public class MainActivity extends AppCompatActivity { private Button btn_First, btn_Second; private FragmentTransaction fragmentTransaction; private FirstFragment firstFragment = null; private SecondFragment secondFragment = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initClick(); initFragment(); } private void initView() { btn_First = findViewById(R.id.btn_First); btn_Second = findViewById(R.id.btn_Second); } private void initData() { } private void initClick() { btn_First.setOnClickListener(v -> { showFragment(firstFragment); }); btn_Second.setOnClickListener(v -> { showFragment(secondFragment); }); } private void initFragment() { fragmentTransaction = getSupportFragmentManager().beginTransaction(); firstFragment = new FirstFragment(); fragmentTransaction.add(R.id.fcv, firstFragment); secondFragment = new SecondFragment(); fragmentTransaction.add(R.id.fcv, secondFragment); fragmentTransaction.commit(); showFragment(firstFragment); } private void hideFragment() { if (null != firstFragment) { fragmentTransaction.hide(firstFragment); } if (null != secondFragment) { fragmentTransaction.hide(secondFragment); } } private void showFragment(Fragment fragment) { if (null != fragment) { fragmentTransaction = getSupportFragmentManager().beginTransaction(); hideFragment(); fragmentTransaction.show(fragment); fragmentTransaction.commit(); } }}
来源地址:https://blog.csdn.net/weixin_43969550/article/details/127880837
--结束END--
本文标题: Android 第十四章 FragmentContainerView
本文链接: https://lsjlt.com/news/373319.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0