TabLayout中的片段仅在用户滑动Android时加载

时间:2015-12-28 01:58:19

标签: android android-fragments android-viewpager android-tablayout

您好我正在使用fragmentfragment在其中使用tablayout和少数viewpager制作应用。问题是我的孩子fragment(来自Tablayout)总是执行(每当用户点击父fragment时加载所有代码。如何让孩子fragment仅加载每当用户滑动它时(例如,我有2个选项卡,选项卡2中的内容仅在用户滑动到选项卡2时加载)

这是我的父fragment代码

public class ManageEventFragment extends Fragment {


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


    TabLayout tabLayout;
    ViewPager viewPager;

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

        viewPager = (ViewPager) viewFragment.findViewById(R.id.viewPager);

        tabLayout = (TabLayout) viewFragment.findViewById(R.id.tabLayout);

        viewPager.setAdapter(new CustomAdapter(getChildFragmentManager(), getContext()));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        return viewFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        //        tabLayout.setupWithViewPager(viewPager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
        });

        if (ViewCompat.isLaidOut(tabLayout)) {
            tabLayout.setupWithViewPager(viewPager);
        } else {
            tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    tabLayout.setupWithViewPager(viewPager);
                    tabLayout.removeOnLayoutChangeListener(this);
                }
            });
        }

        super.onActivityCreated(savedInstanceState);

    }


    //TabLayout and ViewPager class
    private class CustomAdapter extends FragmentPagerAdapter {

        private String fragments[] = {"Edit Event", "Create Event"};

        public CustomAdapter(FragmentManager fragmentManager, Context context) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new EditEventFragment();
                case 1:
                    return new CreateEventFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position];
        }

    }

}

我的孩子片段代码。我希望这些代码只在用户滑动到子选项卡时执行。任何帮助都非常感谢,谢谢

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

        //Initializing our listEvents list
        listEvents = new ArrayList();

        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new EventAdapterEdit(getContext(),listEvents);
        recyclerView.setAdapter(adapter);


        requestQueue = Volley.newRequestQueue(getContext());

        adapter = new EventAdapterEdit(getContext(),listEvents);
        recyclerView.setAdapter(adapter);

        getEventDetailRespond(requestQueue);

2 个答案:

答案 0 :(得分:5)

@Override public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
}

您可以在ViewPager Adapter

中的片段中使用上述方法
if(isVisibleToUser){//dosomething when the fragment is visible}
else{//dosomething else.}

要注意不要在那里初始化视图,而是在onViewCreated中初始化您的视图并调用您想要在setUserVisibleHint上执行的方法。另一个丑陋的方法是向您的ViewPager添加一个滚动侦听器并获取当前项目位置并触发片段内的操作。要从ViewPager Adapter获取片段,您可以这样做:

 MyFragment frag = (MyFragment) pager.getAdapter().instantiateItem(pager, position);

然后你可以调用MyFragment

内的方法

答案 1 :(得分:1)

Viewpager默认加载相邻的片段以确保使应用程序流畅,这样当用户滑动到片段(已经加载)时它就在那里。要更改是默认行为,请使用 viewpager.setOffscreenPageLimit(int limit)其中,限制是您预先加载的片段旁边的片段数量。 希望这有帮助