AuthAdapter无法覆盖FragmentStatePagerAdapter中的getItem(int)返回类型AuthFragment与Fragment不兼容

时间:2018-06-04 11:58:54

标签: android android-fragments android-adapter

我有一个处理授权的片段。代码如下:

public abstract class AuthFragment extends Fragment {

    protected Callback callback;

    @BindView(R.id.caption)
    protected VerticalTextView caption;

    @BindView(R.id.root)
    protected ViewGroup parent;

    protected boolean lock;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(authLayout(), container, false);
        ButterKnife.bind(this, root);
        KeyboardVisibilityEvent.setEventListener(getActivity(), isOpen -> {
            callback.scale(isOpen);
            if (!isOpen) {
                clearFocus();
            }
        });
        return root;
    }

    public void setCallback(@NonNull Callback callback) {
        this.callback = callback;
    }

    @LayoutRes
    public abstract int authLayout();

    public abstract void fold();

    public abstract void clearFocus();

    @RequiresApi(api = Build.VERSION_CODES.M)
    @OnClick(R.id.root)
    public void unfold() {
        if (!lock) {
            caption.setVerticalText(false);
            caption.requestLayout();
            Rotate transition = new Rotate();
            transition.setStartAngle(-90f);
            transition.setEndAngle(0f);
            transition.addTarget(caption);
            TransitionSet set = new TransitionSet();
            set.setDuration(getResources().getInteger(R.integer.duration));
            ChangeBounds changeBounds = new ChangeBounds();
            set.addTransition(changeBounds);
            set.addTransition(transition);
            TextSizeTransition sizeTransition = new TextSizeTransition();
            sizeTransition.addTarget(caption);
            set.addTransition(sizeTransition);
            set.setOrdering(TransitionSet.ORDERING_TOGETHER);
            caption.post(() -> {
                TransitionManager.beginDelayedTransition(parent, set);
                caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.unfolded_size));
                caption.setTextColor(ContextCompat.getColor(getContext(), R.color.color_label));
                caption.setTranslationX(0);
                ConstraintLayout.LayoutParams params = getParams();
                params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                params.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
                params.verticalBias = 0.78f;
                caption.setLayoutParams(params);
            });
            callback.show(this);
            lock = true;
        }
    }

    protected ConstraintLayout.LayoutParams getParams() {
        return ConstraintLayout.LayoutParams.class.cast(caption.getLayoutParams());
    }

    public interface Callback {
        void show(AuthFragment fragment);

        void scale(boolean hasFocus);
    }

}  

我有一个使用上面片段的callback接口的适配器。代码如下:

public abstract class AuthAdapter extends FragmentStatePagerAdapter implements AuthFragment.Callback {

    private final AnimatedViewPager pager;
    private final SparseArray<AuthFragment> authArray;
    private final List<ImageView> sharedElements;
    private final ImageView authBackground;
    private float factor;

    public AuthAdapter(FragmentManager manager,
                       AnimatedViewPager pager,
                       ImageView authBackground,
                       List<ImageView> sharedElements) {
        super(manager);
        this.authBackground = authBackground;
        this.pager = pager;
        this.authArray = new SparseArray<>(getCount());
        this.sharedElements = sharedElements;
        pager.setDuration(350);
        final float textSize = pager.getResources().getDimension(R.dimen.folded_size);
        final float textPadding = pager.getResources().getDimension(R.dimen.folded_label_padding);
        factor = 1 - (textSize + textPadding) / (pager.getWidth());
    }

    @Override
    public AuthFragment getItem(int position) {
        AuthFragment fragment = authArray.get(position);
        if (fragment == null) {
            fragment = position != 1 ? new LogInFragment() : new SignUpFragment();
            authArray.put(position, fragment);
            fragment.setCallback(this);
        }
        return fragment;
    }

    @Override
    public void show(AuthFragment fragment) {
        final int index = authArray.keyAt(authArray.indexOfValue(fragment));
        pager.setCurrentItem(index, true);
        shiftSharedElements(getPageOffsetX(fragment), index == 1);
        for (int jIndex = 0; jIndex < authArray.size(); jIndex++) {
            if (jIndex != index) {
                authArray.get(jIndex).fold();
            }
        }
    }

    private float getPageOffsetX(AuthFragment fragment) {
        int pageWidth = fragment.getView().getWidth();
        return pageWidth - pageWidth * factor;
    }

    private void shiftSharedElements(float pageOffsetX, boolean forward) {
        final Context context = pager.getContext();
        //since we're clipping the page, we have to adjust the shared elements
        AnimatorSet shiftAnimator = new AnimatorSet();
        for (View view : sharedElements) {
            float translationX = forward ? pageOffsetX : -pageOffsetX;
            float temp = view.getWidth() / 3f;
            translationX -= forward ? temp : -temp;
            ObjectAnimator shift = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, 0, translationX);
            shiftAnimator.playTogether(shift);
        }

        int color = ContextCompat.getColor(context, forward ? R.color.color_logo_sign_up : R.color.color_logo_log_in);
        DrawableCompat.setTint(sharedElements.get(0).getDrawable(), color);
        //scroll the background by x
        int offset = authBackground.getWidth() / 2;
        ObjectAnimator scrollAnimator = ObjectAnimator.ofInt(authBackground, "scrollX", forward ? offset : -offset);
        shiftAnimator.playTogether(scrollAnimator);
        shiftAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        shiftAnimator.setDuration(pager.getResources().getInteger(R.integer.duration) / 2);
        shiftAnimator.start();
    }

    @Override
    public void scale(boolean hasFocus) {

        final float scale = hasFocus ? 1 : 1.4f;
        final float logoScale = hasFocus ? 0.75f : 1f;
        View logo = sharedElements.get(0);

        AnimatorSet scaleAnimation = new AnimatorSet();
        scaleAnimation.playTogether(ObjectAnimator.ofFloat(logo, View.SCALE_X, logoScale));
        scaleAnimation.playTogether(ObjectAnimator.ofFloat(logo, View.SCALE_Y, logoScale));
        scaleAnimation.playTogether(ObjectAnimator.ofFloat(authBackground, View.SCALE_X, scale));
        scaleAnimation.playTogether(ObjectAnimator.ofFloat(authBackground, View.SCALE_Y, scale));
        scaleAnimation.setDuration(200);
        scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        scaleAnimation.start();
    }

    @Override
    public float getPageWidth(int position) {
        return factor;
    }

    @Override
    public int getCount() {
        return 2;
    }
}  

当我尝试运行我的代码时,我得到以下错误:

  

错误:AuthAdapter中的getItem(int)无法覆盖FragmentStatePagerAdapter中的getItem(int)
  返回类型AuthFragment与Fragment
不兼容   error:方法不会覆盖或实现超类型

中的方法

如果我让getItem返回android.support.v4.Fragment,则会出现Incompatible types错误。我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

在将应用程序从Android -O OS移植到Android -P移植时,我遇到了同样的问题     出现此问题的原因是V4,V7旧库用于旧代码。     现在在Android P中已将其迁移到android androidx库。     所以请将源文件中所有必要的导入从V4,V7,V13更改为androidx     进行文件更改:import androidx.legacy_legacy-support-v13 \     如下所示-并注释旧的V4 / V7 / V13包括最新的androidx旧版库,如下所示。Android.mk文件更改如下:-

        LOCAL_STATIC_ANDROID_LIBRARIES += \
        androidx.car_car \
        androidx.legacy_legacy-support-v4 \
        androidx.legacy_legacy-support-v13 \
        android-support-design 
    #   android-support-v4
    #   android-support-v13 \

In source code If you are extending V13 or V4 change it to latest androidx classes :
refere below  https://developer.android.com/topic/libraries/support-library/refactor for changed V4/V7/V13 libraries changed to corresponding androidx....
i did like below in my case for same error.

Before :-


     private class ScreenSlidePagerAdapter extends 
        android.support.v13.app.FragmentStatePagerAdapter {}

After :- added import androidx.legacy.app.FragmentStatePagerAdapter;

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {}

this change worked for me.