gpt4 book ai didi

android - 如何正确使用fragment

转载 作者:行者123 更新时间:2023-12-02 16:23:45 25 4
gpt4 key购买 nike

我正在尝试学习如何在 android 中使用 Fragment。我创建单独的类和布局。我无法理解应该如何将它们联系起来。我的主课到底讲了什么?有人可以准确演示如何以非常基本的方式使用 fragment 吗?

最佳答案

在你的主类中,你生成一个或多个 fragment ...当你生成每个 fragment 时,它与 Activity 非常相似,但有自己的生命周期(google 一下)。

这是 fragment 的示例:

public class DummySectionFragment3 extends Fragment 
{
public DummySectionFragment3() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

View rootView = inflater.inflate(R.layout.exercise_layout,
container, false);
return rootView;
}
}

在 OnCreateView() 方法中,您可以执行通常对 Activity 执行的操作。我的主类包含在 fragment 之间切换的SectionsPagerAdapter(类似于API示例中的寻呼机)创建 2 或 3 个 fragment 并尝试一下...我没有找到任何好的例子,所以我只是尝试了上面的方法。

关于android - 如何正确使用fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17910221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com