简体   繁体   English

是Android Fragments View还是ViewGroup

[英]Are Android Fragments View or ViewGroup

As the question reads, Fragments in android are View or ViewGroup. 正如问题所读,android中的片段是View或ViewGroup。 Can anyone explain 谁能解释

Here is the onCreateView method of Fragment from docs 这是来自文档的Fragment的onCreateView方法

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

The container parameter passed to onCreateView() is the parent ViewGroup (from the activity's layout) in which your fragment layout will be inserted 传递给onCreateView()的container参数是父ViewGroup(来自活动的布局),在其中插入片段布局

And

To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. 要为片段绘制UI,必须从此方法返回一个View,它是片段布局的根。

You cannot define fragments as views or viewgroups.Fragments are more than that.The easiest way to visualize fragment is to think of them as chunks of an activity having own xml appearance,own behaviour with own life cycle callbacks.They always run on top of an activity allowing you to perform operations such as add,replace etc on them at run time.This way you can switch between your layouts effectively and efficiently. 您不能将片段定义为视图或视图组。片段的含义远不止这些。可视化片段的最简单方法是将其视为具有自己的xml外观,具有自己的生命周期回调的行为的一部分。一种活动,允许您在运行时对其执行添加,替换等操作。这样,您可以有效地在布局之间进行切换。
To understand onCreateView method,consider the following explanation: 要了解onCreateView方法,请考虑以下说明:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
         View view =inflater.inflate(R.layout.example_fragment, container, false);
      return view;

    }

this returns a single View object, always a ViewGroup , with the set of View object that defines the Fragment's UI. 这将返回一个View对象(始终为ViewGroup ,其中包含定义Fragment UI的View对象集。 The Activity calls this event handler when it is time for the Fragment to provide its UI for display. 当Fragment需要提供其UI供显示时, Activity调用此事件处理程序。

碎片是一种不需要全屏显示的活动,它可以将屏幕分成很多碎片,因此非常适合手机和平板电脑使用。

我认为没有,activity(fragment)控制视图显示的内容,viewGroup扩展视图并实现ViewManger。只有与活动负载相关的View才能通过电话显示布局。Activity是一个Group,view可以在此容器中正常工作

Neither. 都不行 Fragment is a base class. Fragment是基类。

From https://developer.android.com/guide/components/fragments.html 来自https://developer.android.com/guide/components/fragments.html

A Fragment represents a behavior or a portion of user interface in an Activity Fragment代表活动中的行为或用户界面的一部分

Fragment contains a field: Fragment包含一个字段:

// The View generated for this fragment.
View mView;

that is generated in onCreateView which has the implementation: 在具有实现的onCreateView中生成的:

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        Bundle savedInstanceState) {
    return null;
}

So if you want a Fragment with a View then @Override that method. 因此,如果您想要带有ViewFragment ,则@Override该方法。 And then the Fragment can be shown to the user if you use the appropriate fragment transaction from an Activity or nested Fragment . 然后,如果您使用Activity或嵌套Fragment的适当片段事务,则可以将Fragment显示给用户。

片段是与主机活动生命周期相关联的自定义视图的包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM