<tfoot id='JvkQJ'></tfoot>
  • <legend id='JvkQJ'><style id='JvkQJ'><dir id='JvkQJ'><q id='JvkQJ'></q></dir></style></legend>

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

      <small id='JvkQJ'></small><noframes id='JvkQJ'>

      1. 带有 ViewPager 的 TabLayout 在片段 Android 中不起作用

        时间:2023-10-04

        <small id='hodlz'></small><noframes id='hodlz'>

            <tbody id='hodlz'></tbody>
            • <bdo id='hodlz'></bdo><ul id='hodlz'></ul>
            • <legend id='hodlz'><style id='hodlz'><dir id='hodlz'><q id='hodlz'></q></dir></style></legend>

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

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

                  问题描述

                  您好,我在片段中有一个 TabLayout 和 ViewPager.但是我不能在标签之间滑动到子片段(在活动中正常工作但在片段中不工作).

                  Hi I have a TabLayout and ViewPager inside a fragment. But I can not slide beetween tab to come to child fragment (Working normally when in Activity but not work in fragment).

                  这是我在片段内的 Tablayout 代码:

                  Here is my code for Tablayout inside fragment:

                  @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                               Bundle savedInstanceState) {
                  
                          View inflateView = inflater.inflate(R.layout.fragment_assign_beacon_to_event, container, false);
                  
                          //Viewpager and TabLayout
                          viewPager = (ViewPager)inflateView.findViewById(R.id.viewPager);
                          viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));
                  
                          tabLayout = (TabLayout)inflateView.findViewById(R.id.tabLayout);
                  
                  
                          // Inflate the layout for this fragment
                          return inflateView;
                      }
                  
                  @Override
                      public void onActivityCreated(Bundle savedInstanceState) {
                          super.onActivityCreated(savedInstanceState);
                  
                      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());
                              }
                          });
                  
                  Any here is my Custom Adapter class
                  
                  private class CustomAdapter extends FragmentPagerAdapter {
                  
                          private String fragments[] = {"Assign Event", "Manage Event"};
                  
                          public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
                              super(supportFragmentManager);
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              switch (position) {
                                  case 0:
                  //                    return new AssignEventToBeacon();
                                      return new Test1();
                                  case 1:
                                      return new ManageEventAssigned();
                                  default:
                                      return null;
                              }
                          }
                  
                          @Override
                          public int getCount() {
                              return fragments.length;
                          }
                  
                          @Override
                          public CharSequence getPageTitle(int position) {
                              return fragments[position];
                          }
                  
                      }
                  

                  我怀疑这行有问题:

                  viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));
                  

                  非常感谢任何帮助,谢谢

                  Anyhelp is much appreciate, thanks

                  推荐答案

                  我做了一个类似的项目,这是我的片段代码:

                  I have done a similar project and here is my code for the fragment :

                  @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                               Bundle savedInstanceState) {
                          // Inflate the layout for this fragment
                          View view = inflater.inflate(R.layout.outer_fragment, container, false);
                          setHasOptionsMenu(true);
                  
                          ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
                          if (viewPager != null) {
                              setupViewPager(viewPager);
                          }
                  
                          Log.v("Layout","Tabs");
                          TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
                          tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
                          tabLayout.setTabTextColors(Color.parseColor("#707070"), Color.parseColor("#FFFFFF"));
                          assert viewPager != null;
                          tabLayout.setupWithViewPager(viewPager);
                  
                          return view;
                      }
                  
                  private void setupViewPager(ViewPager viewPager) {
                          Adapter adapter = new Adapter(getChildFragmentManager());
                          adapter.addFragment(new Tab1Fragment(), "PHOTOS");
                          adapter.addFragment(new Tab2Fragment(), "HI-FIVES");
                          viewPager.setAdapter(adapter);
                      }
                  
                  
                  static class Adapter extends FragmentPagerAdapter {
                          private final List<Fragment> mFragments = new ArrayList<>();
                          private final List<String> mFragmentTitles = new ArrayList<>();
                  
                          public Adapter(FragmentManager fm) {
                              super(fm);
                          }
                  
                          public void addFragment(Fragment fragment, String title) {
                              mFragments.add(fragment);
                              mFragmentTitles.add(title);
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              return mFragments.get(position);
                          }
                  
                          @Override
                          public int getCount() {
                              return mFragments.size();
                          }
                  
                          @Override
                          public CharSequence getPageTitle(int position) {
                              return mFragmentTitles.get(position);
                          }
                      }
                  

                  这篇关于带有 ViewPager 的 TabLayout 在片段 Android 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MuPDF for Android:片段选项而不是活动 下一篇:android回收器视图中的水平和垂直滚动

                  相关文章

                  1. <legend id='Erfyu'><style id='Erfyu'><dir id='Erfyu'><q id='Erfyu'></q></dir></style></legend>

                      • <bdo id='Erfyu'></bdo><ul id='Erfyu'></ul>

                    1. <tfoot id='Erfyu'></tfoot>

                      <small id='Erfyu'></small><noframes id='Erfyu'>

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