功能:
我正在开发某种配置向导,它可能有很多步骤。每个步骤都有自己的配置,在这种情况下,isSkippable()
是否用户可以跳过该步骤。当用户点击菜单项以进入下一步,或者确实完成向导时,还有一个回调方法。
问题:我没有活动上下文,当从活动中调用onStepCompleted
时,我的字段也为空。
扩展ActionBarActivity
并且未实现任何接口的活动:
private WizardStepListener mStepListener;
private WizardPagerAdapter mPagerAdapter;
private ViewPager mViewPager;
public static Intent getIntent(final Context context) {
final Intent intent = new Intent(context, WizardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (mStepListener == null) {
return false;
}
switch (item.getItemId()) {
case R.id.action_step_skip:
moveToNextStep();
break;
case R.id.action_step_done:
case R.id.action_step_next:
final boolean stepCompleted = mStepListener.onStepCompleted();
if (stepCompleted && !isLastStep() || !stepCompleted) {
break;
}
final Intent intent = LandingActivity.getIntent(this);
WizardActivity.this.startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
WizardActivity.this.finish();
break;
}
return true;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wizard);
final Resources resources = getResources();
final PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
pagerTabStrip.setDrawFullUnderline(false);
pagerTabStrip.setBackgroundColor(resources.getColor(R.color.tabs_categories_background));
pagerTabStrip.setTextColor(Color.WHITE);
pagerTabStrip.setTabIndicatorColor(Color.WHITE);
mPagerAdapter = new WizardPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.wizard_pager);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
setupView(position);
}
});
setupView(0);
}
private void setupView(final int position) {
final Fragment fragment = mPagerAdapter.getActiveFragment(mViewPager, position);
mStepListener =
fragment instanceof WizardStepListener ? (WizardStepListener) fragment : null;
supportInvalidateOptionsMenu();
}
private boolean isLastStep() { ... }
private boolean moveToNextStep() { ... }
}
片段扩展Fragment
并实现LoaderManager.LoaderCallbacks>,WizardStepListener:
private CategoriesExpandableAdapter mCatAdapter;
private ViewFlipper mViewFlipper;
/**
* @return A new instance of fragment WizardCategories.
*/
public static WizardCategories newInstance() {
return new WizardCategories();
}
@Override
public boolean isSkippable() {
return false;
}
@Override
public boolean onStepCompleted() {
if (!isAdded()) {
return false;
}
if (mCatAdapter == null || !mCatAdapter.hasCheckedCategories()) {
FeedbackToast.getInstance(
getActivity(),
"Debes seleccionar al menos una categoría",
FeedbackToast.NEGATIVE
).show();
return false;
}
...
return true;
}
扩展FragmentPagerAdapter
的PagerAdapter:
private final FragmentManager mFragmentManager;
public WizardPagerAdapter(final FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(final int position) {
final Fragment fragment;
switch (position) {
case 0:
fragment = WizardCategories.newInstance();
break;
default:
fragment = new Fragment();
break;
}
return fragment;
}
public Fragment getActiveFragment(final ViewPager container, final int position) {
final String name = makeFragmentName(container.getId(), position);
final Fragment fragment = mFragmentManager.findFragmentByTag(name);
return fragment != null ? fragment : getItem(position);
}
private static String makeFragmentName(final int viewId, final int index) {
return "android:switcher:" + viewId + ":" + index;
}
我尝试了什么:
FragmentStatePagerAdapter
,但没有运气。getActiveFragment
在另一个可能相关的帖子中查看来自FragmentManager的片段。其他评论:
isAdded()
始终返回false。getActivity()
上调用onStepCompleted
始终返回null。答案 0 :(得分:0)
你应该能够使用getActivity()来获取Activity并将你的片段中的上下文存储在onCreate中,要记住的主要事情是你需要在调用onAttach之后调用它,将片段链接到活动。
我目前在我的应用中使用onCreate()中的Activity的上下文没有问题。
或者,您可以覆盖onAttach并直接获取对Activity的引用并将其存储在那里:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
答案 1 :(得分:0)
如何转动表并让Wizard片段自己保存MenuItems?然后他们可以直接调用诸如((YourActivity)getActivity())。moveToNextPage()之类的活动。
这种方法可以针对一个通用的WizardFragment完成,然后让所有向导页面从中扩展。