当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《TabLayout中的片段仅在用户滑动Android时加载》经验,为你挑选了1个好方法。

嗨,我正在使用和制作一个带有A fragment和少数孩子的应用程序.问题是我的孩子(从)总是执行(每当用户点击父母时加载所有代码.如何让孩子只在用户滑动时加载(例如我有2个标签,仅在标签2中的内容)用户滑动到标签2时加载fragmenttablayoutviewpagerfragmentTablayoutfragmentfragment

这是我的父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);

k0sh.. 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> k0sh..:
@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

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有