Java Android活动可重用性
我创建了一个可重用的xml布局,如下所示 我想在不同的活动上使用相同的组件,我想做的是创建扩展活动的Java Android活动可重用性,java,android,oop,code-reuse,Java,Android,Oop,Code Reuse,我创建了一个可重用的xml布局,如下所示 我想在不同的活动上使用相同的组件,我想做的是创建扩展活动的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.saveBu
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);
}
}
活动没有构造函数类。在意图调用时只引用类名。
子类不能更改超类变量(隐藏)
我不想将BaseActivityClass
中的相同代码复制粘贴到其他类中。
包装器类可能会解决这个问题,但在我看来这似乎太草率了
我如何解决这个设计问题?
我可以自由评论任何想法听起来像是希望基类控制主容器布局,并允许派生类提供“内容”布局。对吗?如果是,您可以使用以下模式:
步骤1-将ViewStub
添加到基本布局中。一些伪代码可以让您了解:
<ConstraintLayout>
<!-- Common Stuff -->
<Button id="menu">
<Button id="save">
<!-- "Content" area to be filled by derived classes -->
<ViewStub id="viewStub" />
</ConstraintLayout>
步骤3-更新派生类以提供要显示的布局。一些伪代码:
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();
}
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。如果基本活动是抽象的,它将永远不会被实例化,并且不必遵循零参数构造函数规则。只有您的派生类可以。所以你可以这样做:
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
}
}
希望有帮助 布局xml可以
包含
其他xml-您是否考虑过仅使用布局和“包含”来实现组合。感谢@Andy的输入,我正在使用包含
,但问题是我的模板有按钮,每次使用按钮事件都是相同的,我不想在不同的活动
类上复制相同的事件。