3

记录的Android片段示例(FragmentBasicsNewsReader违反了面向对象设计的核心原则。建立当前显示的视图类型存在冗余条件,FragmentActivity与视图类型和片段类型紧密耦合。 MainActivity耦合到每个类(包括 XML) :

在此处输入图像描述

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

如何Fragments实现视图和片段不紧密耦合到FragmentActivity

4

1 回答 1

3

从更简单的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);
        }
   }
}

添加两个新类SinglePaneNewsViewDoublePaneNewsView实现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 代码上 找到完整的源代码。

于 2012-09-23T18:01:06.230 回答