gpt4 book ai didi

java - 什么时候对 fragment 使用 onCreateView?

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:37 24 4
gpt4 key购买 nike

我正在按照以下步骤

http://developer.android.com/training/basics/fragments/creating.html#AddInLayout

我似乎不明白他们为什么说,“创建 fragment 时的一个区别是您必须使用 onCreateView() 回调来定义布局。”但没有为 HeadlinesFragment 使用 onCreateView,只有 ArticleFragment。他们似乎都设置了布局,所以我不明白为什么不使用 onCreateView。另外,fragment_container 只是 xml 文件吗?我猜它只在 MainActivity 中使用。代码贴在

Android Fragment Basics Tutorial

我在下面发布的 articleFragment 代码除外。到目前为止,我想我知道你有一个扩展 fragmentactivity 的类,它包含 fragment 容器和切换其他 fragment 的方法。那么您拥有的 fragment 是扩展 fragment 或 listFragment 的类?然而,该网站,

http://developer.samsung.com/android/technical-docs/Using-Fragments-to-Build-User-Interfaces-in-Android

显示了不扩展 fragment Activity 而只是扩展 Activity 的 Activity 示例,它应该包含其他 fragment Activity 。

这个网站:

http://developer.android.com/reference/android/app/Fragment.html

显示了有关 fragment 的更多信息,但生命周期并未阐明何时使用 OnCreate 与 onCreateView,我认为这与布局有关,但它指出 onCreate 也启动了 fragment ,因此我不确定要使用哪个利用。

谢谢!!!!!!

package com.example.android.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;

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

// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}

// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}

@Override
public void onStart() {
super.onStart();

// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// Set article based on saved instance state defined during onCreateView
updateArticleView(mCurrentPosition);
}
}

public void updateArticleView(int position) {
TextView article = (TextView) getActivity().findViewById(R.id.article_fragment);
article.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

// Save the current article selection in case we need to recreate the fragment
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}

最佳答案

如果仔细查看代码,您会发现 HeadlinesFragmentArticleFragment 之间的区别:

public class HeadlinesFragment extends ListFragment

public class ArticleFragment extends Fragment

当然,不同之处在于 HeadlinesFragment 是一个 ListFragment 而不仅仅是一个普通的 FragmentListFragment 已经实现了默认的 onCreateView(),它加载了带有 ListView 的布局。如果您想覆盖默认行为,您可以编写自己的 onCreateView() 方法来制作更复杂的布局。

关于java - 什么时候对 fragment 使用 onCreateView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543179/

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