在多个片段中重用回调接口

时间:2014-03-28 14:51:08

标签: android android-fragments interface callback fragment

我希望的目标是在MainActivity中为我要创建的所有片段设置一个实现的方法。它的目的是允许产生另一个片段(Ex.Fragment1是一个listview。它回调mainActivity来创建一个detailItemFragment。这个detailItemFragment可以反过来产生一个具有相同回调的片段)

每个片段包含相同的接口描述。回调根据所需的下一个片段传递Id。当我尝试将第二个回调附加到onAttach()中的Activity时,我的问题开始了。我得到一个ClassCastException。

我认为这是接口的概念性问题,我没有正确实现这一点。现在我将使用单独的回调。我的问题是这是正确的做法,如果是这样,为什么第二个片段onAttach()有问题?

我的第一个片段是:

public class MainFragment extends Fragment implements OnClickListener {

public interface OnItemSelected {
    public void launchNewFragment(FragId id);
}
private OnItemSelected callback;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        callback = (OnItemSelected) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnItemSelected");
    }
}

我的第二个片段是:

public class Offer1Fragment extends Fragment implements OnClickListener {

public interface OnItemSelected {
    public void launchNewFragment(FragId id);
}

private OnItemSelected callback;
private Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_offer1, container, false);
    context = getActivity();

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        callback = (OnItemSelected) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnItemSelected");
    }
}

我的MainActivity实现OnItemSelected并包含:

@Override
public void launchNewFragment(FragId id) {

    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.addToBackStack(null);
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    Fragment fragment = null;
    switch(id) {
        case Offer1:
            fragment = new Offer1Fragment();
            break;

        //Other fragment ids are used to instantiate here as well
    }
    fragmentTransaction.replace(android.R.id.content, fragment);
    fragmentTransaction.commit();

}

错误块是:

03-28 10:19:07.552:E / AndroidRuntime(6099):致命异常:主要 03-28 10:19:07.552:E / AndroidRuntime(6099):java.lang.ClassCastException:ca.test.code.MainActivity@4244bbe0必须实现OnItemSelected 03-28 10:19:07.552:E / AndroidRuntime(6099):at ca.test.code.fragments.Offer1Fragment.onAttach(Offer1Fragment.java:38) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:844) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.BackStackRecord.run(BackStackRecord.java:682) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:441) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.os.Handler.handleCallback(Handler.java:725) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.os.Handler.dispatchMessage(Handler.java:92) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.os.Looper.loop(Looper.java:137) 03-28 10:19:07.552:E / AndroidRuntime(6099):在android.app.ActivityThread.main(ActivityThread.java:5039)

1 个答案:

答案 0 :(得分:1)

问题是你在两个片段中定义了两个名为OnItemSelected的不同接口。 因此,要么您的活动实现两者,要么更好,您只需为两个活动创建一个界面