0

我有一个带有FragmentStatePagerAdapter的ViewPager ,它通过CursorAdapter显示来自ContentProvider的项目。因此,当自动创建/删除页面的内容提供者中的数据发生更改时,会有一个回调。

当我通过ContentResolver.insert()从另一个片段创建新项目时,我想在ViewPager中显示创建的页面。

为了显示一个页面,我调用ViewPager.setCurrentItem()。但似乎直接在插入调用之后ViewPager尚未更新。我该在何时何地称呼它?


编辑:

这是我的主要活动:

我有一个用于创建项目的片段(在位置 0)和来自内容提供者的几个项目。从创建片段中,我在内容解析器上调用 insert。

public class MainActivity extends FragmentActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {

    SectionsPagerAdapter mSectionsPagerAdapter;

    SimpleCursorAdapter mCommitmentAdapter;

    private static final byte LOADER_ID = 0;
    private static final String TAG = MainActivity.class.getName();

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Adapter for commitment names and ids
        mCommitmentAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_dropdown_item_1line, null,
                new String[] { Commitment.Columns.NAME },
                new int[] { android.R.id.text1 }, 0);

        // Adapter for the saved commitments
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager(), mCommitmentAdapter);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public final Loader<Cursor> onCreateLoader(int id, final Bundle args) {
        ....
    }

    @Override
    public final void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mCommitmentAdapter.swapCursor(cursor);
    }

    @Override
    public final void onLoaderReset(Loader<Cursor> loader) {
        mCommitmentAdapter.swapCursor(null);
    }

    public final void showPageWithCommitmentId(long id) {
            // this is called after contentresolver.insert
            // but it does not work
        mViewPager.setCurrentItem(mSectionsPagerAdapter.getItemPosition(id));
    }

    @Override
    protected void onDestroy() {
        if (mSectionsPagerAdapter != null) {
            mSectionsPagerAdapter.destroy();
        }

        getLoaderManager().destroyLoader(LOADER_ID);

        super.onDestroy();
    }

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        final CursorAdapter wrappedAdapter;
        final DataSetObserver subObserver;

        public SectionsPagerAdapter(FragmentManager fm,
                SimpleCursorAdapter adapter) {
            super(fm);
            this.wrappedAdapter = adapter;

            subObserver = new DataSetObserver() {
                @Override
                public void onChanged() {
                    notifyDataSetChanged();
                }

                @Override
                public void onInvalidated() {
                    // Probably destroying the loader
                }
            };

            if (wrappedAdapter != null)
                wrappedAdapter.registerDataSetObserver(subObserver);
        }

        public void destroy() {
            if (wrappedAdapter != null) {
                wrappedAdapter.unregisterDataSetObserver(subObserver);
            }
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                Fragment fragment = CreateCommitmentSectionFragment
                        .getInstance(position);
                return fragment;
            } else {
                // create new ShowCommitSectionFragment
                Fragment fragment = ShowCommitmentSectionFragment.getInstance(
                        getItemId(position), getPageTitle(position).toString());
                return fragment;
            }
        }

        public long getItemId(int position) {
            long id = 0;
            if (wrappedAdapter != null && position > 0) {
                Cursor c = (Cursor) wrappedAdapter.getItem(position - 1);
                if (c != null && !c.isAfterLast() && !c.isBeforeFirst()) {
                    id = c.getLong(0);
                }
            }
            return id;
        }

        @Override
        public int getCount() {
            if (wrappedAdapter != null)
                return 1 + wrappedAdapter.getCount();
            else
                return 1;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position >= getCount())
                return null;

            if (position == 0) {
                return MainActivity.this.getString(R.string.main_create_create);
            }

            Cursor c = (Cursor) wrappedAdapter.getItem(position - 1);
            if (c != null && !c.isAfterLast() && !c.isBeforeFirst()) {
                return c.getString(1);
            }

            return null;
        }

        @Override
        public int getItemPosition(Object object) {
            Fragment f = (Fragment) object;
            long listId = 0;
            if (f instanceof ShowCommitmentSectionFragment) {
                listId = f.getArguments().getLong(
                        ShowCommitmentSectionFragment.ARG_COMMITMENT_ID);
            }
            return getItemPosition(listId);
        }

        /**
         * Get the position of the given commitment id.
         * 
         * @param commitmentId
         * @return
         */
        public int getItemPosition(final long commitmentId) {
            int length = getCount();
            int result = POSITION_NONE;
            int position;
            for (position = 0; position < length; position++) {
                if (commitmentId == getItemId(position)) {
                    result = position;
                    break;
                }
            }

            return result;
        }

    }

}
4

0 回答 0