TabLayout中的片段仅在用户滑动Android时加载 [英] Fragment in TabLayout only load when user slide Android

查看:77
本文介绍了TabLayout中的片段仅在用户滑动Android时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tablayoutviewpager制作一个带有A fragment和其中几个子fragment的应用程序.问题是我的所有子fragment(来自Tablayout)总是执行(每当用户单击父fragment时,将所有代码加载到内部.如何获取子fragment仅在用户滑动至子fragment时加载(例如我有2个标签,则只有当用户滑动到标签2时,标签2中的内容才会加载)

Hi I'm making an app with A fragment and few child fragment inside it using tablayout and viewpager. The problem is all my child fragment (from Tablayout) always execute (load all the code inside whenever user click the parent fragment. How to get the child fragment only load whenever the the user slide to it (for example I have 2 tab, content in tab 2 only load when user slide to tab 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];
        }

    }

}

还有我的孩子片段代码.我希望这些代码仅在用户滑动到子选项卡时才执行.非常感谢您的任何帮助,谢谢

And my child fragment code. I want these code only execute whenever the user slide to the child tab. Any help is much appreciate, Thanks

@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);

推荐答案

@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中获取片段,您可以这样做:

be aware to do not initialize views there or anything rather, init your views within onViewCreated and call the method that you wanna execute on setUserVisibleHint. another ugly way is to add a scroll listener to your ViewPager and get the current item position and trigger an action that is within the fragment. to get the fragment from the ViewPager Adapter you can do such :

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

然后您可以调用MyFragment

这篇关于TabLayout中的片段仅在用户滑动Android时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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