Android:在外部类中实现内部类;内部类和外部类的回调 [英] Android: Implement inner class in outer class; callback from inner class and outer class

查看:212
本文介绍了Android:在外部类中实现内部类;内部类和外部类的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中有多个片段,应该能够在它们之间传递数据。我用过教程来实现回调。我的MainActivity是我的片段类所在的外部类。此外,我有一个处理片段转换的FragmentPagerAdapter。
嗯,问题是,Eclipse不会让我实现我的回调接口,它包含在一个Fragments中,用于我的MainActivity外部类。

I have multiple fragments in one activity that should be able to pass data inbetween them. I have used tutorials to implement callbacks. My MainActivity is the outer class in which my fragment classes are. Furthermore I have a FragmentPagerAdapter that handles the fragment transitions. Well the thing is, Eclipse wont let me implement my callback interface, that is included in one of the Fragments, for my MainActivity outer class.

这是我的代码结构:

public class MainActivity extends FragmentActivity **implements ConnectionFragment.dataCallback**{ 
//compiler error here:"ConnectionFragment cannot be resolved to a type" 
//when i leave this out i get runtime error: "MainActivity java must 
//implement dataCallback"

...

    public class SectionsPagerAdapter extends FragmentPagerAdapter  implements ConnectionFragment.dataCallback{
        @Override
        public void updateLog(View v, String line) {
            DataFragment dataFrag = (DataFragment)getSupportFragmentManager().findFragmentByTag(DataFragment.class.getName());
                if (dataFrag != null){
                    dataFrag.updateLog(v,line);
                }
        }
    ...

    }
    public static class ConnectionFragment extends Fragment {
    ...
        public interface dataCallback{
        public void updateLog(View v, String line);
        }
        dataCallback mCallback;
        private static dataCallback dummyCallback=new dataCallback(){
             @Override
             public void updateLog(View v, String line){    
             }
        };

        @Override
        public void onAttach(Activity activity){
            super.onAttach(activity);
            try {
                mCallback = (dataCallback)activity;
            } catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement dataCallback");
         }
    }
    }
    public static class DataFragment extends Fragment implements ConnectionFragment.dataCallback{
        public void updateLog(View v,String line){

             TextView logTextView=(TextView)v.findViewById(R.id.log_view);
             logTextView.append("\n"+line);
    }

    ...
    }
    public static class GraphFragment extends Fragment {
    ...
    }
}

ConnectionFragment应该能够将数据发送到DataFragment。

ConnectionFragment should be able to send data to DataFragment.

感谢您的帮助!

推荐答案

您无法实现内部接口或扩展内部类。只需将 ConnectionFragment 移动到自己的文件中。

You can't implement an inner-interface or extend an inner-class. Simply move ConnectionFragment to its own file.

这是因为在编译时,这些内部类依赖于父类 - 从不相反。作为证明,如果您查看已编译的 .class 文件,这些内部对象将编译为 MainActivity $ ConnectionFragment.class 或其他内容。但是,如果 ConnectionFragment 被编译到自己的文件中( ConnectionFragment.cass ),那么 MainActivity.class 可以依赖它,Eclipse将自动处理构建顺序。

This is because at compile time, these inner classes are dependent on the parent class - and NEVER the other way around. As proof, if you look at the compiled .class files, these inner-Objects are compiled as MainActivity$ConnectionFragment.class or something there-abouts. If, however, ConnectionFragment is compiled into its own file (ConnectionFragment.cass), then MainActivity.class can depend on it, and Eclipse will automatically handle the build-order.

这篇关于Android:在外部类中实现内部类;内部类和外部类的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆