简体   繁体   English

如何在Activity中初始化接口。

[英]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()).alertForDiscardDefaultProfileChang‌​‌​es(id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从其他活动更改视图。 - How to change a view from a different activity. 在Activity中初始化接口 - Initialize an interface in an Activity 在多个Fragment中实现相同的接口,从Activity调用。 - Implementing same interface in multiple Fragments, called from Activity. 在一个活动中滚动多个回收者视图。 (如何使其平滑) - Scrolling of multiple recycler view in an activity. (how to make it smooth) 如果我在另一个片段中覆盖接口而不是在活动中,如何在片段中初始化接口回调 - How to initialize the interface callback in fragment if i overriding the interface in another fragment not in activity 如何在接口中初始化对象? - How to initialize a object in a interface? 如何在mainActivity中初始化接口 - How to initialize interface in the mainActivity 无法暂停活动。 NPE - Unable to pause activity. NPE 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 如何初始化在 Activity 中定义的接口,该接口由通过 TabLayout 使用 ViewPager 设置的 3 个片段实现? - How to initialize an interface defined in an Activity which is implemented by 3 fragments setup with ViewPager via TabLayout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM