我有一个接口,它有一个名为drawCharts()的方法,我有一个BaseFragmentClass
实现了这个接口,现在我再宣布一个ChildFrgament
类,它正在扩展BaseFragmentClass
,我试图覆盖ChildFragment
类中的drawCharts()方法,但它根本没有被触发。以下是我的代码段
interface IDraw{
public void drawCharts();
}
public class BaseFragment extends Fragment implements IDraw {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void drawCharts(){
}
}
public class ChildFragment extends BaseFragment{
@Override
public void drawCharts(){
// have some stuff to execute
// but not able to trigger
}
}
答案 0 :(得分:0)
方法不会因为它们在父级中声明而被触发,您应该从某个地方调用它们。
在以下示例中,您在单击某个按钮后调用此方法:
interface IDraw {
public void drawCharts();
}
public class BaseFragment extends Fragment implements IDraw {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// this method is triggered by a click on a button
public void onDrawButtonClick(View v) {
// this will call a method implemented by a particular fragment - a child
drawCharts();
}
@Override
public void drawCharts() {
}
}
public class ChildFragment extends BaseFragment {
@Override
public void drawCharts(){
// have some stuff to execute
// but not able to trigger
}
}
您也可以尝试从父级的onCreateView
或onResume
方法触发此方法,具体取决于您需要执行的操作。 Java将确保从实际类调用该方法,而不是从父类调用。