<bdo id='1avbM'></bdo><ul id='1avbM'></ul>

    <tfoot id='1avbM'></tfoot>
  • <i id='1avbM'><tr id='1avbM'><dt id='1avbM'><q id='1avbM'><span id='1avbM'><b id='1avbM'><form id='1avbM'><ins id='1avbM'></ins><ul id='1avbM'></ul><sub id='1avbM'></sub></form><legend id='1avbM'></legend><bdo id='1avbM'><pre id='1avbM'><center id='1avbM'></center></pre></bdo></b><th id='1avbM'></th></span></q></dt></tr></i><div id='1avbM'><tfoot id='1avbM'></tfoot><dl id='1avbM'><fieldset id='1avbM'></fieldset></dl></div>
  • <legend id='1avbM'><style id='1avbM'><dir id='1avbM'><q id='1avbM'></q></dir></style></legend>

    1. <small id='1avbM'></small><noframes id='1avbM'>

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

        时间:2023-10-04
            <tbody id='88BmJ'></tbody>
          <i id='88BmJ'><tr id='88BmJ'><dt id='88BmJ'><q id='88BmJ'><span id='88BmJ'><b id='88BmJ'><form id='88BmJ'><ins id='88BmJ'></ins><ul id='88BmJ'></ul><sub id='88BmJ'></sub></form><legend id='88BmJ'></legend><bdo id='88BmJ'><pre id='88BmJ'><center id='88BmJ'></center></pre></bdo></b><th id='88BmJ'></th></span></q></dt></tr></i><div id='88BmJ'><tfoot id='88BmJ'></tfoot><dl id='88BmJ'><fieldset id='88BmJ'></fieldset></dl></div>
        • <small id='88BmJ'></small><noframes id='88BmJ'>

          <legend id='88BmJ'><style id='88BmJ'><dir id='88BmJ'><q id='88BmJ'></q></dir></style></legend>

            1. <tfoot id='88BmJ'></tfoot>
                <bdo id='88BmJ'></bdo><ul id='88BmJ'></ul>

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

                  问题描述

                  您好,我正在使用 tablayoutviewpagerfragment 和少数子 fragment 的应用程序>.问题是我所有的孩子 fragment(来自 Tablayout)总是执行(每当用户单击父 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)

                  这是我的父片段代码

                  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 中的方法

                  then you could call a method that is within MyFragment

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

                  上一篇:平滑滚动在 ViewPager 中不起作用(支持库) 下一篇:Android viewPager 图片从右向左滑动

                  相关文章

                  1. <small id='oW0BG'></small><noframes id='oW0BG'>

                      <legend id='oW0BG'><style id='oW0BG'><dir id='oW0BG'><q id='oW0BG'></q></dir></style></legend>

                        <bdo id='oW0BG'></bdo><ul id='oW0BG'></ul>
                      <tfoot id='oW0BG'></tfoot>
                      <i id='oW0BG'><tr id='oW0BG'><dt id='oW0BG'><q id='oW0BG'><span id='oW0BG'><b id='oW0BG'><form id='oW0BG'><ins id='oW0BG'></ins><ul id='oW0BG'></ul><sub id='oW0BG'></sub></form><legend id='oW0BG'></legend><bdo id='oW0BG'><pre id='oW0BG'><center id='oW0BG'></center></pre></bdo></b><th id='oW0BG'></th></span></q></dt></tr></i><div id='oW0BG'><tfoot id='oW0BG'></tfoot><dl id='oW0BG'><fieldset id='oW0BG'></fieldset></dl></div>