Activity中实现Fragment接口的循环继承

时间:2016-11-29 10:19:14

标签: java android android-fragments android-viewpager

我正在使用接口将数据从片段传递到父活动。但是,在Activity中实现接口之后,引入了循环继承。请提供可能的解决方案?

代码的基本部分:

public class FirstActivity extends AppCompatActivity implements FirstActivity.FirstFragment.PassSituationData {

    ViewPager viewPager;
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //.......

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new SecondFragment(), "A");
        adapter.addFragment(new FirstFragment(), "B");
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDataPass(String nameText) {
        Log.d("data",nameText);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }


    public static class SecondFragment extends Fragment {

        public SecondFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView = inflater.inflate(R.layout.fragment_action, container, false);

            return rootView;
        }

    }

    public static class FirstFragment extends Fragment {

        PassSituationData dataListener;


        public FirstFragment() {
            // Required empty public constructor
        }

        public interface PassSituationData {
            public void onDataPass(String nameText);
        }


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

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView = inflater.inflate(R.layout.fragment_situation, container, false);

            return rootView;
        }


        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == Constants.SITUATION_REQUEST_CODE) {
                if (data != null) {
                    String nameText = data.getStringExtra("name");
                    dataListener.onDataPass(nameText);
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

首先在你的片段实现方法onDetach中, 在dataListener中明确引用

@Override
public void onDetach() {
    super.onDetach();        
    dataListener = null;
}

您正在存储对活动的引用(因为dataListener是您的活动转换为接口),您需要在不再需要时清除它。

还有一件事请不要将片段和适配器作为内部类:)