问题描述
我完成了一个应用程序,一切除了一个挥之不去的问题看起来很大。我有一个导航抽屉与3个按钮,并且这些按钮中的一个打开了具有ViewPager和FragmentStateAdapter对付一系列片段,他们每个人之间移动的片段。我注意到,当我preSS在我的抽屉里导航按钮去这个片段,导航抽屉被挂起来只是一个很轻微,但明显的,那一刻。任何帮助将是AP preciated的确定问题,下面是我的一些相关的code片段:
I'm completing an app, and everything looks great with the exception of one lingering issue. I have a navigation drawer with 3 buttons, and one of those buttons opens up a fragment that has a ViewPager and FragmentStateAdapter to deal with a series of fragments, moving between each of them. I notice when I press the button in my nav drawer to go to this fragment, the navigation drawer gets hung up for just a very slight, but noticeable, moment. Any help would be appreciated in determine the problem, here are some relevant code snippets of mine:
在我的主要活动(它承载一切),我发现与我的viewpager像这样的片段:
In my Main Activity (which hosts everything else) I find the fragment with my viewpager like so:
currentFragment = report;
fragmentManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.addToBackStack(null).commit();
那么,这里是我如何构建和我的观点寻呼机和适配器初始化片段:
Then, here is how I construct and initialize the fragment with my view pager and adapter:
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()时,它只是返回我已经构建了4个片段中的一个,和getCount将(),它简单的返回一个常量的整数。
All that I do in my FragmentStatePagerAdapter is the bare minimum, I have getItem(), which just returns one of the four fragments I have already constructed, and a getCount() which simple returns a constant integer.
推荐答案
听起来像是你需要等待,直到 DrawerLayout
调换片段之前关闭
。
Sounds like you need to wait until the DrawerLayout
closes before swapping the Fragment
.
下面是一个简单的例子:
Here's a short example:
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();
}
}
});
}
每当你点击你的按钮
,初始化片段
并调用 DrawerLayout.closeDrawer
。
mYourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mFragment = ...;
mYourDrawerLayout.closeDrawer(mYourDrawerView);
}
});
这篇关于导航抽屉犹豫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!