Android活动可重用性

问题描述 投票:1回答:1

我创建了一个看起来像这样的可重用的xml布局

enter image description here

我想在不同的活动上使用相同的组件,我想做的是创建扩展活动的BaseActivityClass

public class BaseActivityClass extends Activity {
    int layout_id = R.layout.SomeLayout;
    final int menu_button_id = R.id.menuButton;
    final int save_button_id = R.id.saveButton;

    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(layout_id);

       Button btn = findViewById(menu_button_id);
       btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //this functionality will be same on every child class
        }
       });
   }
}

我想将该类扩展为

public class SomeActivityClass extends BaseActivityClass {
    int layout_id = R.layout.SomeOtherLayout;

    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    }
}

没有Activity的构造函数类。在意图调用中,仅引用了类名。并且子类无法更改(隐藏)超类变量。

我不想将粘贴在BaseActivityClass中的相同代码复制到其他类中。包装器类可以解决问题,但imo似乎太草率。

我该如何解决这个设计问题?我可以对任何想法发表评论

java android oop code-reuse
1个回答
0
投票

听起来像您想要base类控制主容器布局,并允许derived类提供“内容”布局。那是对的吗?如果是这样,您可以使用此模式:

步骤1-将ViewStub添加到基本布局。一些伪代码给你的想法:

<ConstraintLayout>
   <!-- Common Stuff -->
   <Button id="menu">
   <Button id="save">

   <!-- "Content" area to be filled by derived classes -->
   <ViewStub id="viewStub" />

</ConstraintLayout>

[步骤2-更新您的基本布局,以提供一种将内容膨胀到“内容区域”的方法。一些伪代码:

public abstract class BaseActivityClass extends Activity {
    int layout_id = R.layout.SomeLayout;
    final int menu_button_id = R.id.menuButton;
    final int save_button_id = R.id.saveButton;

    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(layout_id);

       Button btn = findViewById(menu_button_id);
       btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //this functionality will be same on every child class
        }
       });

       // Get the ViewStub, set the derived class's content layout on it and inflate it,
       // thereby displaying the derived class's view layout within the base class's 
       // "content area"
       ViewStub vs = findViewById(viewStub);
       vs.setLayout(getContentLayoutId());
       vs.inflate();
   }

   // Define abstract method that all derived classes must implement to provide
   // the id of the layout to show in the "content area"
   @LayoutRes
   public abstract int getContentLayoutId();
}

第3步-更新派生类以提供要显示的布局。一些伪代码:

public class SomeActivityClass extends BaseActivityClass {
    int layout_id = R.layout.SomeOtherLayout;

    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    }

    @Override
    @LayoutRes
    public int getContentLayoutId() { return layout_id; }
}

现在创建SomeActivityClass时,它将调用超类onCreate,该类会夸大您的基本布局,然后向派生类询问要推送到“内容区域”的主要内容布局。

我已经在我的项目中使用了这种模式,并且效果很好。


[另一种选择是仅通过超类构造函数传递布局ID。如果基本Activity为abstract,它将永远不会被实例化,也不必遵守零参数构造函数规则。只有您的派生类可以。因此,您可以执行以下操作:

public abstract class BaseActivityClass extends Activity {
    private final int mContentLayoutId;
    protected BaseActivityClass(int contentLayoutId) {
        mContentLayoutId = contentLayoutId;
    }

    protected void onCreate(Bundle state) {
        // Same code to load ViewStub, but use mContentLayoutId instead
    }
}

public SomeOtherActivity extends BaseActivity {
    public SomeOtherActivity() {
        super(R.layout.SomeOtherLayout); // Call super with derived layout
    }
}

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.