0

我想从我的 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;
    }


}

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

先感谢您!

4

2 回答 2

2

这让我连续忙了两天。我终于在这里找到了答案:

动态更新 ViewPager?

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

这个该死的错误花费了我大量的时间:(但上面的链接帮助我在几分钟内修复了它。

于 2013-05-13T15:44:30.937 回答
0

在 的来源中FragmentPagerAdapter.java

82   @ Override 
83 public Object instantiateItem( ViewGroup container, int position) {
84 if (mCurTransaction == null ) {
85 mCurTransaction = mFragmentManager. 开始交易();
86 }
88 // 我们是否已经有了这个片段?
89 String name = makeFragmentName (container.getId ( ), position);
90 片段片段 = mFragmentManager。findFragmentByTag(名称);
91 如果(片段!= null ) {
92 if (DEBUG) 日志。v (TAG, "附加项目#" + 位置 + ": f=" + 片段);
93 mCurTransaction。附加(片段);
94 } else {
95 片段 = getItem (位置);
96 如果(调试)日志。v (TAG, "添加项目#" + 位置 + ": f=" + 片段);
97 mCurTransaction。add (container.getId (), fragment, 98 makeFragmentName (container.getId ( ), position ));

99 }
100 if (fragment != mCurrentPrimaryItem) {
101 片段。setMenuVisibility ();
102 片段。setUserVisibleHint ();
103 }
105 返回片段;
106 }
108 @ Override
109 public void destroyItem( ViewGroup container, int position, Object object) {
110 if (mCurTransaction == null ) {
111 mCurTransaction = mFragmentManager。开始交易();
112 }
113 if (DEBUG) 日志。v (TAG, "分离项目#" + position + ": f=" + object
114 + " v=" + (( Fragment )object) .getView ());
115 mCurTransaction。分离((片段)对象);
116 }

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

于 2013-10-11T08:34:56.183 回答