我正在尝试开发兼容智能手机(一个窗格布局)和平板电脑(两个窗格布局)的应用程序。我的应用程序显示如下:
fragment http://android10.org/images/stories/develoment_1/working_with_fragments_android10_02.png
我使用了Google developers website中提供的示例。
这个例子很好用。但对于我的应用,我需要在WebView fragment
中使用不同的布局。在ListView fragment
中选择的每个元素都会在右侧片段中显示特定的布局。
这是我点击元素时调用的代码:
public void onArticleSelected(int position) {
// The user selected the headline of an article from the
// HeadlinesFragment
// Capture the article fragment from the activity layout
BodyFragment articleFrag = (BodyFragment) getSupportFragmentManager().findFragmentById(R.id.body_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// If the frag is not available, we're in the one-pane layout and
// must swap frags...
// Create fragment and give it an argument for the selected
// article
BodyFragment newFragment = new BodyFragment();
Bundle args = new Bundle();
args.putInt(BodyFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this
// fragment,
// and add the transaction to the back stack so the user can
// navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
我该怎么做?我想将布局动态替换为片段,但这是不可能的。
有人有解决方案吗?
提前致谢