为什么我不能从FragmentPagerAdapter中分离片段?

时间:2013-03-02 22:33:11

标签: android fragment detach fragmentpageradapter

我想从FragmentPagerAdapter中分离一个片段,但它似乎没有工作。这是我的pageradapter类,我从原始代码中复制了它:

public class PagerAdapter1 extends FragmentPagerAdapter {

    private List<String> fragments;
       private static final String TAG = "FragmentPagerAdapter";
        private static final boolean DEBUG = true;
        private Fragment mCurrentPrimaryItem = null;
        private final FragmentManager mFragmentManager;
        private FragmentTransaction mCurTransaction = null;

    /**
     * @param fm
     * @param fragments2
     */
    public PagerAdapter1(FragmentManager fm, List<String> fragments2) {
        super(fm);
        mFragmentManager=fm;
        this.fragments = fragments2;
    }
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
     */
    @Override
    public Fragment getItem(int position) {
        //return this.fragments.get(position);
        return Fragment.instantiate(ViewPagerActivity.context, fragments.get(position));

    }

    /* (non-Javadoc)
     * @see android.support.v4.view.PagerAdapter#getCount()
     */
    @Override
    public int getCount() {
        return this.fragments.size();
    }



      /**
     * Return the Fragment associated with a specified position.
     */


    @Override
    public void startUpdate(ViewGroup container) {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
       Log.i("asasd", "asdasdasdasdasd");

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }

        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));
          //  Log.i("asdJANIANI", " " + getFragmentTag(container.getId(),0));
            //Log.i("JANI", " " + getItem(0));
        }

        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }
        **destroyItem(container,0,getItem(0));**

        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
                + " v=" + ((Fragment)object).getView());
        mCurTransaction.detach((Fragment)object);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment)object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setMenuVisibility(false);
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            mCurrentPrimaryItem = fragment;
        }

        if (position == 0){
            this.notifyDataSetChanged();
        }

    }

    @Override
    public void finishUpdate(ViewGroup container) {
        if (mCurTransaction != null) {
            mCurTransaction.commitAllowingStateLoss();
            mCurTransaction = null;
            mFragmentManager.executePendingTransactions();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment)object).getView() == view;
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    /**
     * Return a unique identifier for the item at the given position.
     *
     * <p>The default implementation returns the given position.
     * Subclasses should override this method if the positions of items can change.</p>
     *
     * @param position Position within this adapter
     * @return Unique identifier for the item at position
     */
    public long getItemId(int position) {
        return position;
    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }


}

我只改变了一件事,我在instantiateItem()方法中实现了一个destroyItem()方法,因为我想在每次实例化片段时分离片段(位置0)。我没有得到任何错误,它只是没有分离。为什么呢?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

这让我忙了两天。我终于找到了答案:

Update ViewPager dynamically?

基本上,不要使用FragmentPagerAdapter,而是使用FragmentStatePagerAdapter并覆盖getItemPosition()方法。事实证明,前者不允许对其数据源进行适当的更改,也不会反映它们。

这个该死的bug花了我很多时间:(但上面的链接帮助我在几分钟内修复它。

答案 1 :(得分:0)

FragmentPagerAdapter.java的来源,

82  @Override
83 public Object instantiateItem(ViewGroup container, int position) {
84 if (mCurTransaction == null) {
85 mCurTransaction = mFragmentManager.beginTransaction();
86 }
88 // Do we already have this fragment?
89 String name = makeFragmentName(container.getId(), position);
90 Fragment fragment = mFragmentManager.findFragmentByTag(name);
91 if (fragment != null) {
92 if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
93 mCurTransaction.attach(fragment);
94 } else {
95 fragment = getItem(position);
96 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
97 mCurTransaction.add(container.getId(), fragment,
98 makeFragmentName(container.getId(), position));
99 }
100 if (fragment != mCurrentPrimaryItem) {
101 fragment.setMenuVisibility(false);
102 fragment.setUserVisibleHint(false);
103 }
105 return fragment;
106 }
108 @Override
109 public void destroyItem(ViewGroup container, int position, Object object) {
110 if (mCurTransaction == null) {
111 mCurTransaction = mFragmentManager.beginTransaction();
112 }
113 if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
114 + " v=" + ((Fragment)object).getView());
115 mCurTransaction.detach((Fragment)object);
116 }

您可以看到它使用附加/分离来显示/隐藏片段。这就是他们不工作的原因。