Supporting different screen sizes with dynamic or static fragments?

て烟熏妆下的殇ゞ 提交于 2019-12-25 16:54:35

问题


I am found it confusing to be able to support multi screens using fragments. I thought the idea was that i could offer more than one version of the activity and that activity could have 1 or more fragments (depending on real estate) statically embedded.

Supporting dynamic fragments where these are added from code rather than embedded <fragment> element types. How would this be done in code as I wouldn't know the configuration details of the xml from the activity.

Does anyone have an example of this ?

When would I use static or dynamic fragments.

One reason I could foresee using dynamic fragments is to support the retaining on the fragment so the activity could just ask for it in code but I am finding it difficult to understand how this would be supported without the activity (java) knowing exactly what xml files are available

Thanks


回答1:


I found it very useful to look through and understand the code of the Android Samples.

These sample projects are usually quite small and focus on only 1 or 2 main concepts each.

If you don't want to actually download and install the sample projects, you can also just browse the source code on GitHub.

For example in the NavigationDrawer sample the NavigationDrawerActivity class contains a dynamically created PlanetFragment class.




回答2:


There are 2 main reasons to use a "dynamic" fragment:

  1. You can pass parameters by code. For example, if you have a Fragment that displays an article, you create the Fragment by code and initialize it with the Article to display (using Fragment.setArguments(Bundle) ). This is the equivalent of passing parameters to an Activity using Intent.putExtras().
  2. You can interchange different fragments in the same container.

Example 1: in your main Activity you have a navigation menu with 3 sections. When the user chooses a new section, you replace Fragment "Section A" with Fragment "Section B" in the same FrameLayout container.

Example 2: You display a two-pane layout on tablet (items list + details). Each time the user selects an item in the left list, you create a new Fragment initialized for the new item to display and replace the existing Fragment on the right with the new one.

Example 3: You have a ViewPager which displays a long list of articles. You swipe right or left to navigate to the previous/next article. These fragments will be created programmatically inside a FragmentPagerAdapter; you can't do that using static Fragments.




回答3:


To answer your question on how to implement the dual pane with dynamic fragments, here is how:

  1. Create a folder named values-land in your resources. In this folder, add a config.XML file in which you will have a code like this:

    <resources> <item type="bool" name="dual_pane">true</item> </resources>

  2. In the "normal" values folder, in the config.xml file, add this code:

    <resources> <item type="bool" name="dual_pane">false</item> </resources>

  3. In your activity, you can detect in the onCreate() the boolean value corresponding to landscape config you just created:

    getResources().getBoolean(R.bool.dual_pane);

  4. Then based on the boolean value, you can decide to inflate a layout with just two fragment containers on top of the other (single pane layout) or with two fragment containers next to each other (dual pane layout).

  5. Finally add your fragments dynamically to the appropriate container and voila.

I like this approch because it is very Android like, by the use of the different values folder. Also you don't have to change much to an existing working Fragment pattern to make it support dual pane mode.



来源:https://stackoverflow.com/questions/31236052/supporting-different-screen-sizes-with-dynamic-or-static-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!