片段中的罗马努里克的WizardPager

时间:2013-08-13 21:59:37

标签: android android-fragments static-methods android-fragmentactivity navigation-drawer

我有一个FragmentActivity,里面有不同的片段,可以通过NavigationDrawer切换。

Roman Nurik的原始代码是用FragmentActivity编写的,但我需要在Fragment ...

这是我Activity(已缩短):

public class MainActivity extends FragmentActivity implements
    PageFragmentCallbacks, ReviewFragment.Callbacks, ModelCallbacks {

// those are the methods I took from the sample
@Override
public void onPageTreeChanged() {
    mCurrentPageSequence = mWizardModel.getCurrentPageSequence();
    recalculateCutOffPage();
    mStepPagerStrip.setPageCount(mCurrentPageSequence.size() + 1); // + 1 =
                                                                    // review
                                                                    // step
    mPagerAdapter.notifyDataSetChanged();
    updateBottomBar();
}

private static void updateBottomBar() {
    int position = mPager.getCurrentItem();
    if (position == mCurrentPageSequence.size()) {
        mNextButton.setText(R.string.finish);
        mNextButton.setBackgroundResource(R.drawable.finish_background);
        mNextButton.setTextAppearance(myContext,
                R.style.TextAppearanceFinish);
    } else {
        mNextButton.setText(mEditingAfterReview ? R.string.review
                : R.string.next);
        mNextButton
                .setBackgroundResource(R.drawable.selectable_item_background);
        TypedValue v = new TypedValue();
        myContext.getTheme().resolveAttribute(
                android.R.attr.textAppearanceMedium, v, true);
        mNextButton.setTextAppearance(myContext, v.resourceId);
        mNextButton.setEnabled(position != mPagerAdapter.getCutOffPage());
    }

    mPrevButton
            .setVisibility(position <= 0 ? View.INVISIBLE : View.VISIBLE);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mWizardModel.unregisterListener(this);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBundle("model", mWizardModel.save());
}

@Override
public AbstractWizardModel onGetModel() {
    return mWizardModel;
}

@Override
public void onEditScreenAfterReview(String key) {
    for (int i = mCurrentPageSequence.size() - 1; i >= 0; i--) {
        if (mCurrentPageSequence.get(i).getKey().equals(key)) {
            mConsumePageSelectedEvent = true;
            mEditingAfterReview = true;
            mPager.setCurrentItem(i);
            updateBottomBar();
            break;
        }
    }
}

@Override
public void onPageDataChanged(Page page) {
    if (page.isRequired()) {
        if (recalculateCutOffPage()) {
            mPagerAdapter.notifyDataSetChanged();
            updateBottomBar();
        }
    }
}

@Override
public Page onGetPage(String key) {
    return mWizardModel.findByKey(key);
}

private static boolean recalculateCutOffPage() {
    // Cut off the pager adapter at first required page that isn't completed
    int cutOffPage = mCurrentPageSequence.size() + 1;
    for (int i = 0; i < mCurrentPageSequence.size(); i++) {
        Page page = mCurrentPageSequence.get(i);
        if (page.isRequired() && !page.isCompleted()) {
            cutOffPage = i;
            break;
        }
    }

    if (mPagerAdapter.getCutOffPage() != cutOffPage) {
        mPagerAdapter.setCutOffPage(cutOffPage);
        return true;
    }

    return false;
}
}

这是我的Fragment代码:

public static class MyFragment extends Fragment {

    public MyFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy3,
                container, false);

        if (savedInstanceState != null) {
            mWizardModel.load(savedInstanceState.getBundle("model"));
        }

        mWizardModel.registerListener(this);

        mPagerAdapter = new MyPagerAdapter(fragmentManager1);
        mPager = (ViewPager) rootView.findViewById(R.id.pager);
        mPager.setAdapter(mPagerAdapter);
        mStepPagerStrip = (StepPagerStrip) rootView
                .findViewById(R.id.strip);
        mStepPagerStrip
                .setOnPageSelectedListener(new StepPagerStrip.OnPageSelectedListener() {
                    @Override
                    public void onPageStripSelected(int position) {
                        position = Math.min(mPagerAdapter.getCount() - 1,
                                position);
                        if (mPager.getCurrentItem() != position) {
                            mPager.setCurrentItem(position);
                        }
                    }
                });

        mNextButton = (Button) rootView.findViewById(R.id.next_button);
        mPrevButton = (Button) rootView.findViewById(R.id.prev_button);

        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mStepPagerStrip.setCurrentPage(position);

                if (mConsumePageSelectedEvent) {
                    mConsumePageSelectedEvent = false;
                    return;
                }

                mEditingAfterReview = false;
                updateBottomBar();
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
                    DialogFragment dg = new DialogFragment() {
                        @Override
                        public Dialog onCreateDialog(
                                Bundle savedInstanceState) {
                            return new AlertDialog.Builder(getActivity())
                                    .setMessage(
                                            R.string.submit_confirm_message)
                                    .setPositiveButton(
                                            R.string.submit_confirm_button,
                                            null)
                                    .setNegativeButton(
                                            android.R.string.cancel, null)
                                    .create();
                        }
                    };
                    dg.show(fragmentManager1, "place_order_dialog");
                } else {
                    if (mEditingAfterReview) {
                        mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
                    } else {
                        mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                    }
                }
            }
        });

        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            }
        });

        onPageTreeChanged();
        updateBottomBar();

        return rootView;
    }

    public class MyPagerAdapter extends FragmentStatePagerAdapter {
        private int mCutOffPage;
        private Fragment mPrimaryItem;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if (i >= mCurrentPageSequence.size()) {
                return new ReviewFragment();
            }

            return mCurrentPageSequence.get(i).createFragment();
        }

        @Override
        public int getItemPosition(Object object) {
            if (object == mPrimaryItem) {
                return POSITION_UNCHANGED;
            }

            return POSITION_NONE;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position,
                Object object) {
            super.setPrimaryItem(container, position, object);
            mPrimaryItem = (Fragment) object;
        }

        @Override
        public int getCount() {
            return 6;
            // return Math.min(mCutOffPage + 1,
            // mCurrentPageSequence.size() + 1);
        }

        public void setCutOffPage(int cutOffPage) {
            if (cutOffPage < 0) {
                cutOffPage = Integer.MAX_VALUE;
            }
            mCutOffPage = cutOffPage;
        }

        public int getCutOffPage() {
            return mCutOffPage;
        }
    }
}

我收到以下错误: AbstractWizardModel类型中的方法registerListener(ModelCallbacks)不适用于参数(MainActivity.MyFragment)

和: 无法从MainActivity类型

中对非静态方法onPageTreeChanged()进行静态引用

我该怎么做才能解决这些错误?

2 个答案:

答案 0 :(得分:0)

尝试更改:

registerListener(this)

registerListener(getActivity())

另一个错误,正如您可以从消息中读取的那样,您的片段是静态的,并且您正在调用MainActivity中的onPageTreeChanged(),它不是静态的。您可以通过这种方式保持对MainActivity的引用:

首先在Fragment中声明一个ModelCallbacks类型的变量:

ModelCallbacks mModelCallback;

然后,覆盖Fragment的onAttach方法,并设置引用:

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

try {
    mModelCallback = (ModelCallbacks)activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
            + " must implement ModelCallbacks");
    }
}

最后,不要直接从片段中调用onPageTreeChanged(),而是更改以下行:

mModelCallback.onPageTreeChanged();

这应该可以解决你的错误。

答案 1 :(得分:0)

这很可能是响应太迟了。但万一其他人成为类似问题的受害者:

您的片段似乎没有实现必要的回调。这解释了它不适用于registerListener方法的情况。 您应该实现这些PageFragmentCallbacks, ReviewFragment.Callbacks, ModelCallbacks回调。特别是,registerListener(ModelCallbacks)要求您提供的任何参数(在本例中为您的片段)实现ModelCallbacks接口