片段可以通过其父活动实现两个接口吗?

Bri*_*ian 2 android interface android-fragments

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

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");
        }
    }

...
}
Run Code Online (Sandbox Code Playgroud)

但是例如说父活动实现了另一个接口

public interface OnThreadCliked{
    void onThreadClicked(Post post);
}
Run Code Online (Sandbox Code Playgroud)

有没有办法获得对活动实现的第二个接口的引用?

ian*_*ake 5

当然,只需将其投放两次:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)