<bdo id='ExsN3'></bdo><ul id='ExsN3'></ul>

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

        <tfoot id='ExsN3'></tfoot><legend id='ExsN3'><style id='ExsN3'><dir id='ExsN3'><q id='ExsN3'></q></dir></style></legend>
      1. <i id='ExsN3'><tr id='ExsN3'><dt id='ExsN3'><q id='ExsN3'><span id='ExsN3'><b id='ExsN3'><form id='ExsN3'><ins id='ExsN3'></ins><ul id='ExsN3'></ul><sub id='ExsN3'></sub></form><legend id='ExsN3'></legend><bdo id='ExsN3'><pre id='ExsN3'><center id='ExsN3'></center></pre></bdo></b><th id='ExsN3'></th></span></q></dt></tr></i><div id='ExsN3'><tfoot id='ExsN3'></tfoot><dl id='ExsN3'><fieldset id='ExsN3'></fieldset></dl></div>
      2. 从 Android 中的 ViewPager 中删除片段页面

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

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

              <tbody id='AfZCg'></tbody>
          1. <small id='AfZCg'></small><noframes id='AfZCg'>

            • <tfoot id='AfZCg'></tfoot>
                  本文介绍了从 Android 中的 ViewPager 中删除片段页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 ViewPager 中动态添加和删除片段,添加作品没有任何问题,但删除无法按预期工作.

                  I'm trying to dynamically add and remove Fragments from a ViewPager, adding works without any problems, but removing doesn't work as expected.

                  每次我想删除当前项时,最后一项都会被删除.

                  Everytime I want to remove the current item, the last one gets removed.

                  我还尝试使用 FragmentStatePagerAdapter 或在适配器的 getItemPosition 方法中返回 POSITION_NONE.

                  I also tried to use an FragmentStatePagerAdapter or return POSITION_NONE in the adapter's getItemPosition method.

                  我做错了什么?

                  这是一个基本的例子:

                  MainActivity.java

                  public class MainActivity extends FragmentActivity implements TextProvider {
                  
                      private Button mAdd;
                      private Button mRemove;
                      private ViewPager mPager;
                  
                      private MyPagerAdapter mAdapter;
                  
                      private ArrayList<String> mEntries = new ArrayList<String>();
                  
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.main);
                  
                          mEntries.add("pos 1");
                          mEntries.add("pos 2");
                          mEntries.add("pos 3");
                          mEntries.add("pos 4");
                          mEntries.add("pos 5");
                  
                          mAdd = (Button) findViewById(R.id.add);
                          mRemove = (Button) findViewById(R.id.remove);
                          mPager = (ViewPager) findViewById(R.id.pager);
                  
                          mAdd.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  addNewItem();
                              }
                          });
                  
                          mRemove.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  removeCurrentItem();
                              }
                          });
                  
                          mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this);
                  
                          mPager.setAdapter(mAdapter);
                  
                      }
                  
                      private void addNewItem() {
                          mEntries.add("new item");
                          mAdapter.notifyDataSetChanged();
                      }
                  
                      private void removeCurrentItem() {
                          int position = mPager.getCurrentItem();
                          mEntries.remove(position);
                          mAdapter.notifyDataSetChanged();
                      }
                  
                      @Override
                      public String getTextForPosition(int position) {
                          return mEntries.get(position);
                      }
                      @Override
                      public int getCount() {
                          return mEntries.size();
                      }
                  
                  
                      private class MyPagerAdapter extends FragmentPagerAdapter {
                  
                          private TextProvider mProvider;
                  
                          public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
                              super(fm);
                              this.mProvider = provider;
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              return MyFragment.newInstance(mProvider.getTextForPosition(position));
                          }
                  
                          @Override
                          public int getCount() {
                              return mProvider.getCount();
                          }
                  
                      }
                  
                  }
                  

                  TextProvider.java

                  public interface TextProvider {
                      public String getTextForPosition(int position);
                      public int getCount();
                  }
                  

                  MyFragment.java

                  public class MyFragment extends Fragment {
                  
                      private String mText;
                  
                      public static MyFragment newInstance(String text) {
                          MyFragment f = new MyFragment(text);
                          return f;
                      }
                  
                      public MyFragment() {
                      }
                  
                      public MyFragment(String text) {
                          this.mText = text;
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                  
                          View root = inflater.inflate(R.layout.fragment, container, false);
                  
                          ((TextView) root.findViewById(R.id.position)).setText(mText);
                  
                          return root;
                      }
                  
                  }
                  

                  activity_main.xml

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical" >
                  
                      <Button
                          android:id="@+id/add"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="add new item" />
                  
                      <Button
                          android:id="@+id/remove"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="remove current item" />
                  
                      <android.support.v4.view.ViewPager
                          android:id="@+id/pager"
                          android:layout_width="match_parent"
                          android:layout_height="0dip"
                          android:layout_weight="1" />
                  
                  </LinearLayout>
                  

                  fragment.xml

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical" >
                  
                      <TextView
                          android:id="@+id/position"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:gravity="center"
                          android:textSize="35sp" />
                  
                  </LinearLayout>
                  

                  推荐答案

                  Louth 的解决方案不足以让我的工作正常,因为现有的片段没有被破坏.受 this answer 的启发,我发现解决方案是覆盖 getItemId(int position)FragmentPagerAdapter 的方法,用于在 Fragment 的预期位置发生变化时提供新的唯一 ID.

                  The solution by Louth was not enough to get things working for me, as the existing fragments were not getting destroyed. Motivated by this answer, I found that the solution is to override the getItemId(int position) method of FragmentPagerAdapter to give a new unique ID whenever there has been a change in the expected position of a Fragment.

                  private class MyPagerAdapter extends FragmentPagerAdapter {
                  
                      private TextProvider mProvider;
                      private long baseId = 0;
                  
                      public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
                          super(fm);
                          this.mProvider = provider;
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          return MyFragment.newInstance(mProvider.getTextForPosition(position));
                      }
                  
                      @Override
                      public int getCount() {
                          return mProvider.getCount();
                      }
                  
                  
                      //this is called when notifyDataSetChanged() is called
                      @Override
                      public int getItemPosition(Object object) {
                          // refresh all fragments when data set changed
                          return PagerAdapter.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;
                      }
                  }
                  

                  现在,例如,如果您删除单个选项卡或对订单进行一些更改,您应该在调用 notifyDataSetChanged() 之前调用 notifyChangeInPosition(1),这将确保将重新创建所有片段.

                  Now, for example if you delete a single tab or make some change to the order, you should call notifyChangeInPosition(1) before calling notifyDataSetChanged(), which will ensure that all the Fragments will be recreated.

                  覆盖 getItemPosition():

                  当调用notifyDataSetChanged() 时,适配器调用它所附加的ViewPagernotifyChanged() 方法.ViewPager 然后检查适配器的 getItemPosition() 为每个项目返回的值,删除那些返回 POSITION_NONE 的项目(参见 源代码) 然后重新填充.

                  When notifyDataSetChanged() is called, the adapter calls the notifyChanged() method of the ViewPager which it is attached to. The ViewPager then checks the value returned by the adapter's getItemPosition() for each item, removing those items which return POSITION_NONE (see the source code) and then repopulating.

                  覆盖 getItemId():

                  这是防止适配器在重新填充 ViewPager 时重新加载旧片段所必需的.您可以通过查看 FragmentPagerAdapter 中 instantiateItem() 的源代码.

                  This is necessary to prevent the adapter from reloading the old fragment when the ViewPager is repopulating. You can easily understand why this works by looking at the source code for instantiateItem() in FragmentPagerAdapter.

                      final long itemId = getItemId(position);
                  
                      // Do we already have this fragment?
                      String name = makeFragmentName(container.getId(), itemId);
                      Fragment fragment = mFragmentManager.findFragmentByTag(name);
                      if (fragment != null) {
                          if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
                          mCurTransaction.attach(fragment);
                      } else {
                          fragment = getItem(position);
                          if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
                          mCurTransaction.add(container.getId(), fragment,
                                  makeFragmentName(container.getId(), itemId));
                      }
                  

                  如你所见,getItem() 方法只有在片段管理器没有找到具有相同 Id 的现有片段时才会被调用.对我来说,即使在调用 notifyDataSetChanged() 之后,旧片段仍然附加,这似乎是一个错误,但 ViewPager 的文档确实明确指出:

                  As you can see, the getItem() method is only called if the fragment manager finds no existing fragments with the same Id. To me it seems like a bug that the old fragments are still attached even after notifyDataSetChanged() is called, but the documentation for ViewPager does clearly state that:

                  请注意,该课程目前处于早期设计和开发阶段.该 API 可能会在以后的兼容性库更新中发生变化,需要在针对较新版本编译应用程序时更改应用程序的源代码.

                  Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.

                  因此,希望此处给出的解决方法在未来版本的支持库中不再是必需的.

                  So hopefully the workaround given here will not be necessary in a future version of the support library.

                  这篇关于从 Android 中的 ViewPager 中删除片段页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ViewPager.setOffscreenPageLimit(0) 无法按预期工作 下一篇:动态添加和删除视图到 viewpager

                  相关文章

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

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

                2. <legend id='ad4qe'><style id='ad4qe'><dir id='ad4qe'><q id='ad4qe'></q></dir></style></legend>

                    <tfoot id='ad4qe'></tfoot>
                    • <bdo id='ad4qe'></bdo><ul id='ad4qe'></ul>