Android 片段能否从其父活动实现两个接口?

Android 片段能否从其父活动实现两个接口?,android,android-fragments,interface,Android,Android Fragments,Interface,为了在片段之间进行通信,我们使用父活动实现的接口模式。。。就像文档中的例子一样,片段可以在其活动附件上获得父接口 public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; // Container Activity must implement this interface public interface OnHeadlineSelectedL

为了在片段之间进行通信,我们使用父活动实现的接口模式。。。就像文档中的例子一样,片段可以在其活动附件上获得父接口

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

...
}
但例如,假设父活动实现了另一个接口

public interface OnThreadCliked{
    void onThreadClicked(Post post);
}

是否有方法获取对activity实现的第二个接口的引用?

当然,只需将其强制转换两次:

OnThreadCliked mCallback2;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }

    // This makes sure that the container activity has implemented
    // the second callback interface. If not, it throws an exception
    try {
        mCallback2 = (OnThreadCliked) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnThreadCliked");
    }
}