如何在不违反核心OO原则的情况下使用碎片?

时间:2012-09-23 18:01:06

标签: android oop android-fragments android-view

记录的Android片段样本(FragmentBasicsNewsReader违反了面向对象设计的核心原则。有多余的条件来建立当前显示的视图类型,将FragmentActivityMainActivity紧密耦合视图类型和片段类型。Fragments耦合到每个类(包括XML):

enter image description here

有人可能会为作者辩解他们试图“保持简单”,或者读者可能会感到困惑。如果这是意图 - 我认为读者可以处理它;相反,它正在教授糟糕的编程实践,这将导致应用程序的可维护性降低。

如何实现FragmentActivity以使视图和片段与{{1}}没有紧密耦合?

1 个答案:

答案 0 :(得分:3)

从更简单的FragmentBasics演示开始,gut 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不需要更改。

enter image description here

添加一个新类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的新类DoublePaneNewsViewAbstractNewsView,如下所示。这两个类负责在相应的视图类型中设置初始片段。它们还负责处理片段之间的转换(如果有的话)。

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代码上找到complete source