从更简单的FragmentBasics演示开始,MainActivity
如下所示:
public class MainActivity extends FragmentActivity
implements OnHeadlineSelectedListener {
AbstractNewsView abstractNewsView;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
abstractNewsView = new AbstractNewsViewProvider(this).get();
abstractNewsView.onCreate(savedInstanceState);
}
@Override public void onArticleSelected(int position) {
abstractNewsView.onArticleSelected(position);
}
}
现在它的依赖关系图是这样的。您现在可以news_articles
为不同的设备类型添加您想要的所有视图变体,MainActivity
而无需更改。
添加一个新类AbstractNewsViewProvider
,其唯一职责是确定用于给定设备的视图类型(单窗格或双窗格)。如果您使用 Guice 或 RoboGuice 进行依赖注入,这将是绑定模块中的Provider方法。
public class AbstractNewsViewProvider {
private final FragmentActivity fragmentActivity;
public AbstractNewsViewProvider(FragmentActivity activity) {
this.fragmentActivity = activity;
}
public AbstractNewsView get() {
if (fragmentActivity.findViewById(R.id.fragment_container) != null) {
return new SinglePaneNewsView(fragmentActivity);
} else {
return new DoublePaneNewsView(fragmentActivity);
}
}
}
添加两个新类SinglePaneNewsView
并DoublePaneNewsView
实现AbstractNewsView
如下所示。这两个类负责在各自的视图类型中设置初始片段。他们还负责处理片段之间的转换(如果有的话)。
interface AbstractNewsView extends OnHeadlineSelectedListener {
public void onCreate(Bundle savedInstanceState);
@Override public void onArticleSelected(int position);
}
public class SinglePaneNewsView implements AbstractNewsView {
private final FragmentActivity fragmentActivity;
public SinglePaneNewsView(FragmentActivity fragmentActivity) {
this.fragmentActivity = fragmentActivity;
}
@Override public void onCreate(Bundle savedInstanceState) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(fragmentActivity.getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
fragmentActivity.getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
@Override public void onArticleSelected(int position) {
// 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
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction =
fragmentActivity.getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
// 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();
}
public class DoublePaneNewsView implements AbstractNewsView {
private final FragmentActivity fragmentActivity;
public DoublePaneNewsView(FragmentActivity fragmentActivity) {
this.fragmentActivity = fragmentActivity;
}
@Override public void onCreate(Bundle savedInstanceState) {
}
@Override public void onArticleSelected(int position) {
((ArticleFragment) fragmentActivity.getSupportFragmentManager()
.findFragmentById(R.id.article_fragment)).updateArticleView(position);
}
}
您可以在 Google 代码上 找到完整的源代码。