我正在尝试开发与智能手机(一个窗格布局)和平板电脑(两个窗格布局)兼容的应用程序。我的应用显示如下:
(来源:android10.org)
我使用了Google 开发者网站上提供的示例。
该示例工作正常。但是对于我的应用程序,我需要在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();
}
}
我该怎么做?我想将布局动态替换到片段中,但这是不可能的。
有人有解决方案吗?
提前致谢