其他分享
首页 > 其他分享> > 如何在不违反OO核心原则的情况下使用Fragments?

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

作者:互联网

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

一个人也许可以为作者试图“保持简单”的借口,或者使读者感到困惑.如果是这样的话,我认为读者可以处理;取而代之的是,它教授不良的编程习惯,这将导致应用程序难以维护.

如何实现片段,以使视图和片段与FragmentActivity不紧密耦合?

解决方法:

从更简单的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代码上找到complete source.

标签:android-fragments,oop,android-view,android
来源: https://codeday.me/bug/20191127/2075465.html