简体   繁体   English

如何正确使用Android中的碎片

[英]How to use Fragments in Android properly

I asked a question a while back: 我回答了一个问题:

Replacing Fragments isn't working/Am I executing this the proper way? 替换碎片不起作用/我是否以正确的方式执行此操作?

Because I was having a really tough time wrapping my head around the entire concept. 因为我很难将整个概念包裹起来。 Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. 过去的Google I / O视频也有所帮助,但我仍然没有理由使用片段。 I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. 我再次遇到了我的问题,通过更多的搜索和Google I / O 2013视频,我又遇到了另一个问题。 After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment> . 在开始赏金以获得关于此主题的更专家意见之后,我被引导使用FrameLayouts,但在此I / O 13视频中19:05进入视频显示<fragment> Can someone clear up any discrepencies or misconceptions I have? 有人可以澄清我的任何不满或误解吗?

Do I use <fragment> or <frameLayout> ? 我使用<fragment>还是<frameLayout> Bonus points if you look at my original question to know where I'm coming from. 如果你看看我原来的问题,知道我来自哪里,可以获得奖励积分。

There are 2 seperate questions I see in here; 我在这里看到两个单独的问题; (1) what is the <fragment> element? (1)什么是<fragment>元素? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout ?. (2)我(意思是你)通过使用FrameLayout以最好的方式做我在上一个问题中尝试做的事情吗?

I will try to answer both. 我会尽力回答这两个问题。

The <fragment> XML tag is just another way to implement a fragment. <fragment> XML标签只是实现片段的另一种方式。 As a refresher, a Fragment is like a part of an activity. 作为复习, Fragment就像是活动的一部分。 It is a stand alone class, however, referring back to the fragment documentation , because a Fragment is intended to be modular, all fragments must be housed within a seperate FragmentActivity . 然而,它是一个独立的类,回顾片段文档 ,因为Fragment是模块化的,所有片段必须存放在单独的FragmentActivity Think of this as FragmentActivity as a container. 将此视为FragmentActivity作为容器。 By loading your Fragment into the FragmentActivity the FragmentActivity provides other valuable functions that the Fragment class does not have access to on its own. 通过将Fragment加载到FragmentActivityFragmentActivity提供了Fragment类本身无法访问的其他有价值的函数。 It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience. 它还为您提供动态交换片段的机会,从而提供更灵活的用户体验。

How you load your Fragment into its containing FragmentActivity depends on what you are trying to acomplish. 如何将Fragment加载到其包含的FragmentActivity取决于您要尝试的内容。 One way to do this is to use the <fragment> tag. 一种方法是使用<fragment>标签。 With this approach, rather than declaring the Fragment in the activity java code and then loading it with a FragmentTransaction as you did in your earlier question, the Fragment class is loaded via the XML layout for the containing FragmentActivity . 使用这种方法,不是像在前面的问题中那样在活动java代码中声明Fragment然后使用FragmentTransaction加载它,而是通过包含FragmentActivity的XML布局加载Fragment类。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"         
    android:orientation="horizontal"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent">    


    <fragment 
        android:name="com.example.android.fragments.HeadlinesFragment"                 
        android:id="@+id/headlines_fragment"               
        android:layout_weight="1"               
        android:layout_width="0dp"               
        android:layout_height="match_parent" />      

</LinearLayout> 

You can see the fragment declared here in the example taken from the android docs. 您可以在Android文档中看到示例中声明的片段。 The fragment is then loaded by the FragmentActivty when setContentView() is called onCreate . 然后,当onCreate调用setContentView()时, FragmentActivtyFragmentActivty加载。

public class MainActivity extends FragmentActivity {     
    @Override     
    public void onCreate(Bundle savedInstanceState) {         
    super.onCreate(savedInstanceState);         
    setContentView(R.layout.news_articles);     
    } 
}

The other way to display fragments is as you have done it in your prior example. 显示片段的另一种方式就像您在前面的示例中所做的那样。 Using a FragmentManager you can create a new instance of your Fragment class and load it programatically within the java code. 使用FragmentManager您可以创建Fragment类的新实例,并在java代码中以编程方式加载它。

So which technique makes sense when? 那么哪种技术有意义呢? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, ie you are not dynamically swapping fragments in and out. 作为android docs demonstraits的例子,当你使用固定片段时,XML片段方法是有意义的,即你不是动态地交换片段。 This is because you cannot edit the XML layout you create on the fly the same way you can the java code. 这是因为您无法像在java代码中那样编辑动态创建的XML布局。 Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. 另外,从我所知道的,使用XML方法将片段更像是用于不同控制的布局元素。 In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. 在android示例中,他们使用XML方法在平板电脑上并排显示2个fragmens。 By controling the fragments more like layout elements, eg having the ability to adjust layout_weight you can achieve a better result for that purpose. 通过控制片段更像布局元素,例如具有调整layout_weight的能力,您可以为此目的获得更好的结果。

However, if you are designing a highly dynamic UX, say with a ViewPager or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment classes and replace those Fragments as they become necessary. 但是,如果您正在设计一个高度动态的用户体验,例如使用ViewPager或其他一些片段将定期轮换的功能,那么使用单独的Fragment类并在必要时替换这些Fragments更有意义。

Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation. 基于您之前的问题以及动态交换片段的需要,我认为您为该实现做出了正确的选择。

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

相关问题 如何使用Android Fragments? - How to use Android Fragments? 如何在Android的片段中使用Volley? - How to use Volley in fragments in Android? 如何在Fragments Android / Java中使用textView? - How to use textView with Fragments Android/Java? Android-如何使用ActionBar在片段之间导航 - Android - How to use the ActionBar to navigate between Fragments 如何在Android中使用onAcitivtyResult分解成一些片段 - How to use onAcitivtyResult into some fragments int Android 如何在Android中执行后台操作,使用Fragments正确更新UI? - How to perform background operations in Android that update the UI properly using Fragments? 如何在Android上正确使用AsyncTask? - How to properly use AsyncTask on Android? 如何使用 Kotlin 片段内的按钮进行 android 开发? - How do I use buttons inside fragments in Kotlin for android development? Android:如何将活动转换为片段以正确使用导航抽屉? - Android: how to convert activities to fragments in order to use the navigation drawer correctly? 如何使用Firestore在片段中使用RecyclerView-Android - How can I use RecyclerView in fragments using Firestore - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM