0

我试图有一个导航抽屉,让我选择不同类型的片段。我实现了一个 Runnable() 线程,因此当片段发生变化时过渡会更加平滑。但是,在我实现之后,它不会在开始时加载默认片段,这意味着它将显示为空白,我必须单击导航抽屉才能在 srart 上显示一些内容。

我希望它能够在应用程序开始时显示菜单上的第一项(菜单 0 )

这是一些代码片段

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(final int position) {
     mPendingRunnable = new Runnable() {
            @Override
            public void run() {
    // update the main content by replacing fragments
                changeFragment(position);
            }
        };

    // update selected item and title, then close the drawer
    // update the main content by replacing fragments

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPAGETitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

private void changeFragment(final int position)
{
    Fragment fragment = new PageFragment(MainActivity.this);
    Bundle args = new Bundle();
    args.putInt(PageFragment.ARG_PAGE_NUMBER, position);
    fragment.setArguments(args);
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame,     
 fragment).commit();
    fragmentChanged =true;
}

public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        //     
 onPrepareOptionsMenu()

            if (mPendingRunnable != null) {
                Handler mHandler = new Handler();
                mHandler.post(mPendingRunnable);
                mPendingRunnable = null;
            }
            progress = ProgressDialog.show(MainActivity.this,
                    "Downloading Data",
                    "Please wait while Data are downloaded");
            progress.setCancelable(false);
            progress.isIndeterminate();
            progress.show();
        }
4

1 回答 1

0

您必须在活动的 onCreate 方法中加载您的第一个片段。当用户从导航抽屉菜单中选择一个选项时,您将现有的片段布局替换为新的。

于 2013-11-05T06:15:54.347 回答