2.x 与 3.x 设备上的片段
完成大量 Android 2.x 应用程序后,我现在开始使用兼容包在 2.x 和 3.x 设备上使用 Fragments。
到目前为止,我的所有布局都是各种 res/layoutxxx 文件夹的一部分,不需要在 src 代码中进行任何手动更改。
当查看如下所示的片段示例时,总是会在 src 代码中的不同设备功能之间做出决定。这真的是必需的吗?我不明白为什么新的 Fragment 设计需要针对不同的设备/布局/方向进行动态更改。
所以我的问题是:像往常一样,我想将所有不同的布局放入其 res/layout 文件夹中,并让 Android 完成其余工作 - 甚至 2 窗格(在平板电脑上)或 1 窗格(在手机上)显示。有某种包装吗?
//
// Helper function to show the details of a selected item, either by
// displaying a fragment in-place in the current UI, or starting a
// whole new activity in which it is displayed.
//
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
After finishing lots of Android 2.x apps I now start with Fragments on 2.x and 3.x devices using the compatibility package.
Up until now all my layouts were part of the various res/layoutxxx folders and didn't need any manual changes from within the src code.
When looking at the Fragment examples like that shown below there's always a decision between different device capabilities within the src code. Is this really required? I don't understand why the new Fragment design requires dynamic changes for different devices/layouts/orientations.
So my question: I would like to put, as usual, all different layouts in their res/layout folders and let Android do the rest - even 2-pane (on tablets) or 1-pane (on phones) displays. Is there some sort of wrapper for that?
//
// Helper function to show the details of a selected item, either by
// displaying a fragment in-place in the current UI, or starting a
// whole new activity in which it is displayed.
//
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您支持各种屏幕尺寸,则在某些屏幕上您有 2 个并排的片段,而在其他(较小的)屏幕上只有单个片段(加载器/充气)。
如果您不使用片段,同样的逻辑也适用,只需让 Android 为特定的屏幕配置选择不同的布局即可。
结果是,使用相同的代码,根据设备规格,您可能会存在或不存在一些屏幕元素,并且您的代码必须通过有条件地检查给定元素是否存在(膨胀)来应对这种情况。
这样您就可以为各种设备提供特定的使用体验。
If you support various screen sizes, on some screen you have 2 fragments side by side, while on other (smaller) screen there's just single fragment (loader / inflated).
Same logic applies also if you don't use fragments, just let Android choose different layout for particular screen configuration.
The result is that with same code, you may have some screen elements present or not depending on device specifications, and your code must cope with this situation by conditionally check if given element is present (inflated) or not.
This way you provide specific use experience for various devices.