导航抽屉犹豫

时间:2014-03-28 16:51:16

标签: android android-viewpager fragment navigation-drawer

我正在完成一个应用程序,除了一个挥之不去的问题外,一切看起来都很棒。我有一个带有3个按钮的导航抽屉,其中一个按钮打开一个片段,其中有一个ViewPager和FragmentStateAdapter来处理一系列片段,在每个片段之间移动。我注意到当我按下导航抽屉里的按钮去看这个片段时,导航抽屉被挂起来只是一个非常轻微但明显的时刻。在确定问题时,我们将不胜感激,以下是我的一些相关代码片段:

在我的主要活动(主持其他所有内容)中,我发现我的viewpager片段如下:

currentFragment = report;
fragmentManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                    .addToBackStack(null).commit();

然后,这是我用视图寻呼机和适配器构建和初始化片段的方法:

public SliderFragment()
{
    previous = false;

    reportToSubmit = new Report();

    suspect = new ReportSuspect();
    location = new ReportLocation();
    description = new ReportDescription();
    report = new ReportSubmit();
}

    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    adapter = new ScreenSlidePagerAdapter(getChildFragmentManager());   
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    super.onCreateView(inflater, container, savedInstanceState);

    View rootView = inflater.inflate(R.layout.view_pager, container, false);

    String title = getResources().getString(R.string.fraud_report_title);

    getActivity().setTitle(title);

    btnNext = (Button) rootView.findViewById(R.id.button_next);
    btnPrev = (Button) rootView.findViewById(R.id.button_previous);
    btnSubmit = (Button) rootView.findViewById(R.id.button_submit);

    pager = (ViewPager) rootView.findViewById(R.id.pager);

    return rootView;
}

@Override
public void onResume()
{
    super.onResume();
    intializePager();
    intializeControls();
}

private void intializePager()
{       
    pager.setAdapter(adapter);
    // Disable gesture for wizard
    pager.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            return true;
        }
    }); 

}

private void intializeControls()
{

    if (!previous)
    {
        btnPrev.setVisibility(View.GONE);
    }
    else
    {
        btnPrev.setVisibility(View.VISIBLE);
    }

    if (!reportIt)
    {
        btnSubmit.setVisibility(View.GONE);
        btnNext.setVisibility(View.VISIBLE);
    }
    else
    {
        btnSubmit.setVisibility(View.VISIBLE);
        btnNext.setVisibility(View.GONE);
    }

    btnNext.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            btnNextNavigation(btnNext, btnPrev, btnSubmit);
        }

        private void btnNextNavigation(final Button btnNext,
                final Button btnPrev, final Button btnSubmit)
        {
            if (pager.getCurrentItem() < NUM_PAGES)
            {
                pager.setCurrentItem(pager.getCurrentItem() + 1);

                if (pager.getCurrentItem() != 0)
                {
                    btnPrev.setVisibility(View.VISIBLE);
                    previous = true;
                }
                else
                {
                    previous = false;
                }

                if (pager.getCurrentItem() == 3)
                {
                    btnNext.setVisibility(View.GONE);
                    btnSubmit.setVisibility(View.VISIBLE);
                    reportIt = true;
                }

            }
        }
    });

    btnPrev.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            btnPrevNavigation(btnNext, btnPrev, btnSubmit);
        }

        private void btnPrevNavigation(final Button btnNext,
                final Button btnPrev, final Button btnSubmit)
        {
            if (pager.getCurrentItem() < NUM_PAGES)
            {
                pager.setCurrentItem(pager.getCurrentItem() - 1);

                if (pager.getCurrentItem() == 0)
                {
                    btnPrev.setVisibility(View.GONE);
                    previous = false;
                }

                if (pager.getCurrentItem() != 3)
                {
                    btnSubmit.setVisibility(View.GONE);
                    btnNext.setVisibility(View.VISIBLE);
                    reportIt = false;
                }

            }
        }

    });

    btnSubmit.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            showDialog();
        }

        private void showDialog()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getActivity());

            builder.setMessage(R.string.confirmation).setTitle(
                    R.string.confirmation_title);

            builder.setPositiveButton(R.string.yes,
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.dismiss();

                            try
                            {
                                processReport();
                            }
                            catch (Exception e)
                            {
                                Toast.makeText(
                                        getActivity(),
                                        "There was an error processing your request",
                                        Toast.LENGTH_LONG).show();
                            }
                        }
                    });

            builder.setNegativeButton(R.string.no,
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.dismiss();
                        }
                    });

            AlertDialog dialog = builder.create();
            dialog.show();
        }

        private void processReport() throws Exception
        {
            if (suspect.getCbWhoValue())
            {
                reportToSubmit.setWho("Unknown");
            }
            else
            {
                reportToSubmit.setWho(suspect.getEtWhoValue());
            }

            reportToSubmit.setReferenceId(UUID.randomUUID().toString()
                    .toUpperCase(Locale.getDefault()));
            reportToSubmit.setCoordinates(location.getCoordinatesValue());
            reportToSubmit.setAddress(location.getEtAddressValue());
            reportToSubmit.setCity(location.getEtCityValue());
            reportToSubmit.setOrganization(location
                    .getEtOrganizationValue());
            reportToSubmit.setDescription(description
                    .getEtDescriptionValue());
            reportToSubmit.setIsOkayToContact(report
                    .getTbOkToContactValue());
            reportToSubmit.setName(report.getEtNameValue());
            reportToSubmit.setPhone(report.getEtPhoneValue());
            reportToSubmit.setEmail(report.getEtEmailValue());
            reportToSubmit.setAudio(description.getAudioValue());

            new ProcessReportTask().execute(reportToSubmit);
        }
    });
}

我在FragmentStatePagerAdapter中所做的一切都是最低限度的,我有getItem(),它只返回我已经构造的四个片段之一,而getCount()只返回一个常量整数。

1 个答案:

答案 0 :(得分:3)

听起来你需要等到DrawerLayout关闭才能交换Fragment

这是一个简短的例子:

private Fragment mFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // If you're using an `ActionBarDrawerToggle`, you don't need `SimpleDrawerListener`
    mYourDrawerLayout.setDrawerListener(new SimpleDrawerListener() {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (mFragment != null) {
                fragmentManager
                        .beginTransaction()
                        .replace(R.id.content_frame, mFragment)
                        .addToBackStack(null).commit();
            }
        }
    });
}

每当您点击Button时,请初始化Fragment并致电DrawerLayout.closeDrawer

    mYourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mFragment = ...;
            mYourDrawerLayout.closeDrawer(mYourDrawerView);
        }
    });