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

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

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

      1. 在TabLayout(材料设计)android中动态添加和删除标签

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

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

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

                  <tfoot id='gzrDD'></tfoot>
                  本文介绍了在TabLayout(材料设计)android中动态添加和删除标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 TabLayout,里面有 ViewPager.我需要在标签布局(材料设计)中动态添加和删除标签.我可以动态添加选项卡,但是在删除选项卡时,选项卡正在正确删除.但是viewpager最后一项被删除了.所以选项卡没有显示特定的片段.仅供参考,我使用了 FragmentPagerAdapter.

                  I have a TabLayout and inside that I have ViewPager. I need to dynamically add and remove tab in tablayout(material design). I can able to add the tabs dynamically but while removing the tab, tab is removing correctly. But viewpager last item is deleted. So tab is not showing specific fragment. FYI i have used FragmentPagerAdapter.

                  我已经按照这个教程来实现这个

                  I have followed this tutorial to implement this

                  https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library

                  public class TabFragment extends Fragment {
                  
                      public static TabLayout tabLayout;
                      public static ViewPager viewPager;
                      public static int int_items = 2;
                      private MyNewsFragment mMyNewsFragment;
                      private ArrayList<TabFragmentModelClass> mFragmentArrayList;
                      private TabLayoutAdapter mTabLayoutAdapter;
                  
                      @Nullable
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                  
                          mMyNewsFragment = new MyNewsFragment();
                          /**
                           *Inflate tab_layout and setup Views.i;;a
                           */
                          View x = inflater.inflate(R.layout.tab_layout, null);
                          tabLayout = (TabLayout) x.findViewById(R.id.tabs);
                          tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);  // scorllable tab
                          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);  // tab name fill parent
                          viewPager = (ViewPager) x.findViewById(R.id.viewpager);
                          mFragmentArrayList = new ArrayList<>();
                          mFragmentArrayList.add(new TabFragmentModelClass(new MyNewsSelectionFragment(), "", ""));
                          mFragmentArrayList.add(new TabFragmentModelClass(mMyNewsFragment, "", getResources().getString(R.string.mynews)));
                          mFragmentArrayList.add(new TabFragmentModelClass(new BreakingNewsFragment(), "", getResources().getString(R.string.breakingnews)));
                  
                          // Selected news from shared preference
                          ArrayList<MyNewsSharedPreferenceModelClass> tempSharedPreferenceModelClass = new MyNewsSharedPreferenceClass().loadFavorites(getActivity());
                          for (int i = 0; i < tempSharedPreferenceModelClass.size(); i++) {
                              mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(tempSharedPreferenceModelClass.get(i).getmCatID()), tempSharedPreferenceModelClass.get(i).getmCatID(),
                                      tempSharedPreferenceModelClass.get(i).getmCatName()));
                          }
                  
                  
                   }
                  
                  
                          /**
                           *Set an Apater for the View Pager
                           */
                          mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
                          viewPager.setAdapter(mTabLayoutAdapter);
                          viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
                          tabLayout.setupWithViewPager(viewPager);
                  
                  
                          viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                              @Override
                              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                              }
                  
                              @Override
                              public void onPageSelected(int position) {
                                  tabLayout.getTabAt(position);
                              }
                  
                              @Override
                              public void onPageScrollStateChanged(int state) {
                                  if (state == 1) {
                                      updateMyNewsFragment();
                                  }
                              }
                          });
                  
                          /**
                           * Now , this is a workaround ,
                           * The setupWithViewPager dose't works without the runnable .
                           * Maybe a Support Library Bug .
                           */
                  
                          tabLayout.post(new Runnable() {
                              @Override
                              public void run() {
                  
                                  tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
                  
                                  tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                      @Override
                                      public void onTabSelected(TabLayout.Tab tab) {
                                          viewPager.setCurrentItem(tab.getPosition());
                                          switch (tab.getPosition()) {
                                              case 1:
                  
                                                  System.out.println("000000000000000000000 ");
                                                  updateMyNewsFragment();
                                                  break;
                                          }
                                      }
                  
                                      @Override
                                      public void onTabUnselected(TabLayout.Tab tab) {
                                      }
                  
                  
                                      @Override
                                      public void onTabReselected(TabLayout.Tab tab) {
                                      }
                                  });
                  
                              }
                          });
                          return x;
                      }
                  
                      /**
                       * update the mynews selection
                       * this method trigger from the MainNewsActivity
                       */
                      public void updateMyNewsFragment() {
                          if (ApplicationController.getInstance().getBoolPref(CommonVariableInterFace.isNewsSelected)) {
                              mMyNewsFragment.sendMyNewsRequest();
                              ApplicationController.getInstance().setBoolPref(CommonVariableInterFace.isNewsSelected, false);
                          }
                      }
                  
                      /**
                       * update the tab values
                       * this method trigger from the MainNewsActivity
                       * if value is zero need to add in tab
                       * if value is one need to remove in tab
                       */
                      public void updateTabvalue(String catId, String catName, int value) {
                  
                              if (value == 0) { // add the value in tab
                  
                                  mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(catId), catId,
                                          catName));
                              }  else {      // Removing the tab
                                  for (int i = 0; i < mFragmentArrayList.size(); i++) {
                                      if (mFragmentArrayList.get(i).getmCatID().equalsIgnoreCase(catId)) {
                  
                                          Log.i("-----same id ", catId);
                                          mFragmentArrayList.remove(i);
                                          mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
                                          viewPager.setAdapter(mTabLayoutAdapter);
                                      }
                                  }
                              }
                  
                              mTabLayoutAdapter.notifyDataSetChanged();
                              viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
                              tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
                               tabLayout.setupWithViewPager(viewPager);
                  
                  
                              tabLayout.post(new Runnable() {
                                  @Override
                                  public void run() {
                  
                                      tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
                  
                                      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                          @Override
                                          public void onTabSelected(TabLayout.Tab tab) {
                                              viewPager.setCurrentItem(tab.getPosition());
                                              switch (tab.getPosition()) {
                                                  case 1:
                                                      updateMyNewsFragment();
                                                      break;
                                              }
                                          }
                  
                                          @Override
                                          public void onTabUnselected(TabLayout.Tab tab) {
                                          }
                  
                  
                                          @Override
                                          public void onTabReselected(TabLayout.Tab tab) {
                                          }
                                      });
                  
                                  }
                              });
                      }
                  
                      public void openSpecificTab() {
                  
                  //        tabLayout.settab
                  //        tabLayout.se
                      }
                  }
                  

                  适配器代码:

                  public class TabLayoutAdapter extends FragmentPagerAdapter {
                  
                      private long baseId = 0;
                      ArrayList<TabFragmentModelClass> fragmentAdapterArrayList;
                  
                      public TabLayoutAdapter(FragmentManager fm, ArrayList<TabFragmentModelClass> fragmentArrayList) {
                          super(fm);
                          fragmentAdapterArrayList = fragmentArrayList;
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                  
                          if (position == 0)
                              return fragmentAdapterArrayList.get(position).getmFragment();
                          if (position == 1)
                              return fragmentAdapterArrayList.get(position).getmFragment();
                          if (position == 2)
                              return fragmentAdapterArrayList.get(position).getmFragment();
                  
                          return new CategoreyNewsFragment().newInstance(fragmentAdapterArrayList.get(position).getmCatID());
                      }
                  
                      @Override
                      public int getCount() {
                          return fragmentAdapterArrayList.size();
                      }
                  
                  
                      //this is called when notifyDataSetChanged() is called
                      @Override
                      public int getItemPosition(Object object) {
                          // refresh all fragments when data set changed
                          return TabLayoutAdapter.POSITION_NONE;
                      }
                  
                  
                      @Override
                      public long getItemId(int position) {
                          // give an ID different from position when position has been changed
                          return baseId + position;
                      }
                  
                      /**
                       * Notify that the position of a fragment has been changed.
                       * Create a new ID for each position to force recreation of the fragment
                       *
                       * @param n number of items which have been changed
                       */
                      public void notifyChangeInPosition(int n) {
                          // shift the ID returned by getItemId outside the range of all previous fragments
                          baseId += getCount() + n;
                      }
                  
                      /**
                       * //     * This method returns the title of the tab according to the position.
                       * //
                       */
                  
                      @Override
                      public CharSequence getPageTitle(int position) {
                          return fragmentAdapterArrayList.get(position).getmCatName();
                      }
                  }
                  

                  推荐答案

                  Remove tab from TabLayout

                  Remove tab from TabLayout

                  ...
                  public void removeTab(int position) {
                      if (mTabLayout.getTabCount() >= 1 && position<mTabLayout.getTabCount()) {
                            mTabLayout.removeTabAt(position);
                            mPagerAdapter.removeTabPage(position);
                      }
                  }
                  ...
                  

                  removeTabPage 方法添加到您的 PagerAdapter

                  Add a removeTabPage method to your PagerAdapter

                  ...
                  public void removeTabPage(int position) {
                      if (!tabItems.isEmpty() && position<tabItems.size()) {
                            tabItems.remove(position);
                            notifyDataSetChanged();
                      }
                  }
                  ...
                  

                  添加一个标签

                  ...
                  private void addTab(String title) {
                          mTabLayout.addTab(mTabLayout.newTab().setText(title));
                          mPagerAdapter.addTabPage(title);
                  }
                  ...
                  

                  addTabPage 方法添加到您的 PagerAdapter

                  Add a addTabPage method to your PagerAdapter

                  ...
                  public void addTabPage(String title) {
                        tabItems.add(title);
                        notifyDataSetChanged();
                  }
                  ...
                  

                  查看此示例代码以获取完整示例:...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java

                  Check out this sample code for a full example: ...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java

                  这篇关于在TabLayout(材料设计)android中动态添加和删除标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我在 viewpager 选项卡中的片段不刷新 下一篇:如何知道 viewpager 是向左还是向右滚动?

                  相关文章

                • <tfoot id='ouzSJ'></tfoot>

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

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

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