ViewPager.OnPageChangeListener中的片段接口

时间:2018-12-02 12:11:00

标签: android interface

在我的viewpager中,我设置了侦听器,从该侦听器中,我在界面中的标签片段中调用方法。 但是我的接口方法中此选项卡片段中的变量始终返回null。 告诉我为什么 ? ps。对不起,我的英语)

父片段:

public class MyFragment
extends Fragment {

protected Context mContext;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

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

具有PagerAdapter的容器片段:

public class FragmentContainer
    extends MyFragment {

private ViewPager viewPager;
private ViewPager.OnPageChangeListener onPageChangeListener;

public interface IPageSelected {
    void onPageSelected();
    void onPageUnselected();
}

public static FragmentContainer newInstance() {
    return new FragmentContainer();
}

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

    viewPager = view.findViewById(R.id.pager);
    PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
    viewPager.setAdapter(pagerAdapter);

    TabLayout tabLayout = view.findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for(int x = 0; x < pagerAdapter.getCount(); x++) {
                ((IPageSelected) pagerAdapter.getItem(x)).onPageUnselected();
            }

            ((IPageSelected) pagerAdapter.getItem(position)).onPageSelected();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    viewPager.addOnPageChangeListener(onPageChangeListener);

    return view;
}

@Override
public void onResume() {
    super.onResume();
    setCurrentItem();
}

private void setCurrentItem() {
    viewPager.post(() -> {
        viewPager.setCurrentItem(0);
        onPageChangeListener.onPageSelected(0);
    });
}


class PagerAdapter
        extends FragmentStatePagerAdapter {

    PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return TestFragment1.newInstance();
            case 1:
                return TestFragment2.newInstance();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Tab1";
            case 1:
                return "Tab2";
            default:
                return null;
        }

    }
} }

标签片段。 问题:为什么接口方法中的变量mContext总是返回null?

public class TestFragment1
    extends MyFragment
    implements FragmentContainer.IPageSelected {

public static TestFragment1 newInstance() {
    return new TestFragment1();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, container, false);
}

@Override
public void onPageSelected() {
    Log.d("myLog", "TestFragment1 onPageSelected: " + mContext); // WHY NULL ???
}

@Override
public void onPageUnselected() {
    Log.d("myLog", "TestFragment1 onPageUnselected: " + mContext); // WHY  NULL ???
} }

public class TestFragment2
    extends MyFragment
    implements FragmentContainer.IPageSelected {

public static TestFragment2 newInstance() {
    return new TestFragment2();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment2, container, false);
}

@Override
public void onPageSelected() {
    Log.d("myLog", "TestFragment2 onPageSelected: " + mContext); // WHY NULL ???
}

@Override
public void onPageUnselected() {
    Log.d("myLog", "TestFragment2 onPageUnselected: " + mContext); // WHY  NULL ???
} }

2 个答案:

答案 0 :(得分:0)

在您的片段容器中:

PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());就是这里。

为了将子片段附加到容器片段,请考虑将子片段管理器 getChildFragmentManager()传递到viewpager适配器。 (现在,您的片段将与容器片段连接到同一级别)

答案 1 :(得分:0)

我找到了解决方法。
适配器类。
旧的:

    class PagerAdapter
        extends FragmentStatePagerAdapter {

    PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return TestFragment1.newInstance();
            case 1:
                return TestFragment2.newInstance();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Tab1";
            case 1:
                return "Tab2";
            default:
                return null;
        }

    }
}

新功能:

    class PagerAdapter
        extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    PagerAdapter(FragmentManager fm) {
        super(fm);
    }

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

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

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

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

适配器初始化。
旧:

PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);

新功能:

PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
pagerAdapter.addFragment(TestFragment1.newInstance(), "tab1");
pagerAdapter.addFragment(TestFragment2.newInstance(), "tab2");
viewPager.setAdapter(pagerAdapter);

现在是一个新问题:第一个选项有什么区别?
现在我在容器片段中初始化适配器,这是原因吗?
为什么在适配器中初始化时不起作用?