[英]How to initialize interface in Activity.
I need to call a method in fragment when a certain item is selected in navigation drawer activity. 在导航抽屉活动中选择某个项目时,我需要在片段中调用一个方法。 For this, I've created one interface which I Will be initializing & calling a method from the activity, Also I'll implement this interface in Fragment and override this method.
为此,我创建了一个接口,该接口将初始化并从活动中调用方法。此外,我还将在Fragment中实现此接口并覆盖此方法。 Here is code snippet for declaring an interface.
这是用于声明接口的代码段。
public interface AlertForDiscardDefaultProfileChanges {
void alertForDiscardDefaultProfileChanges(int navigationItemID);
}
And this is how I'm initializing in activity. 这就是我在活动中进行初始化的方式。
private AlertForDiscardDefaultProfileChanges alertForDiscardDefaultProfileChanges;
@Override
public void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
alertForDiscardDefaultProfileChanges = (AlertForDiscardDefaultProfileChanges) this;
}
Here I'm getting java.lang.ClassCastException
for initializing it. 在这里,我正在获取
java.lang.ClassCastException
进行初始化。 Not sure what I'm missing here or what's wrong. 不知道我在这里想念的是什么还是错了。
you have to implements this interface to your activity/fragment for eg : 您必须为您的活动/片段实现此接口,例如:
MainActivity : 主要活动 :
public class MainActivity extends AppCompatActivity implements FragmentClassName.AlertForDiscardDefaultProfileChanges{
//override methods
@Override
public void alertForDiscardDefaultProfileChanges(String navigationItemID) {
// now use navigationItemID here...
}
}
Step-1 第1步
public interface AlertForDiscardDefaultProfileChanges {
void alertForDiscardDefaultProfileChanges(int navigationItemID);
}
define this method from which activity and class you want transfer your data 定义您要从哪个活动和类中传输数据的方法
Step 2 - 第2步 -
Define this method also in step 1 class. 在步骤1类中也定义此方法。
private AlertForDiscardDefaultProfileChanges favListner;
public void setAlertOnDiscardListner(AlertForDiscardDefaultProfileChanges
favOnTouchListner) {
favListner = favOnTouchListner;
}
Step -3 . 步骤-3。
pass value from Step 1 class like below 传递来自步骤1类的值,如下所示
favListner.alertForDiscardDefaultProfileChanges(int navigationItemID);
Step-4 In which class you want data first implement that interface like. 步骤4您要在哪个类中的数据首先实现该接口。
Class A implements YourActivity_where_interface define.AlertForDiscardDefaultProfileChanges{
override Method.
}
Step 5. 步骤五
You Should have to do one thing also in that class where you want data . 您还应该在需要数据的那个类中做一件事。
You should have to initialise interface from on Create method like below. 您必须从如下所示的Create方法初始化接口。
YourActivity_where_interface define. YourActivity_where_interface定义。
setAlertOnDiscardListner(this);
Done now you can play with it. 完成后,您就可以玩了。
Here is how I achieved it, I called a method through an object of a fragment in which I wanted to implement a method. 这是我如何实现的,我通过一个片段对象调用了一个方法,我想在其中实现一个方法。
Fragment Class - 片段类-
public class DefaultProfileFragment extends Fragment implements
AlertForDiscardDefaultProfileChanges {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_default_profile, container,
false);
mContext = view.getContext();
return view;
}
@Override
public void alertForDiscardDefaultProfileChanges(int navigationItemID) {
showDismissWarning(navigationItemID);
}
Activity from which I need to call interface method. 我需要从中调用接口方法的活动。 A Just simple object of a fragment class and name of a method.
片段类的Just对象和方法名。 Nothing fancy needs to be done.
没什么需要做的。 No need to initialize an interface.
无需初始化接口。
(new DefaultProfileFragment()).alertForDiscardDefaultProfileChanges(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.