Android AuthAdapter无法重写FragmentStatePagerAdapter中的getItem(int),返回类型AuthFragment与Fragment不兼容
我有一个关于授权的片段。代码如下所示:Android AuthAdapter无法重写FragmentStatePagerAdapter中的getItem(int),返回类型AuthFragment与Fragment不兼容,android,android-fragments,android-adapter,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
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);
}
}
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;
}
}
我有一个适配器,它使用上述片段的回调
接口。代码如下所示:
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);
}
}
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扩展了FragmentStatePagerAdapter,实现了AuthFragment.Callback{
私人最终动画视图寻呼机;
私有最终Sparsarray authArray;
私人最终股东名单;
私有最终图像视图背景;
私人浮动系数;
公共AuthAdapter(碎片管理器,
动画视图寻呼机,
ImageView authBackground,
列表共享(删除){
高级经理;
this.authBackground=authBackground;
this.pager=寻呼机;
this.authArray=new SparseArray(getCount());
this.sharedElements=sharedElements;
寻呼机设置持续时间(350);
final float textSize=pager.getResources().getDimension(R.dimen.folded_size);
最终浮点文本填充=pager.getResources().getDimension(R.dimen.folded\u label\u padding);
系数=1-(textSize+textPadding)/(pager.getWidth());
}
@凌驾
公共AuthFragment getItem(int位置){
AuthFragment fragment=authArray.get(位置);
if(片段==null){
片段=位置!=1?新登录片段():新注册片段();
放置(位置,片段);
setCallback(this);
}
返回片段;
}
@凌驾
公共无效显示(AuthFragment片段){
final int index=authArray.keyAt(authArray.indexOfValue(fragment));
pager.setCurrentItem(索引,true);
移位共享元素(getPageOffsetX(片段),索引==1);
对于(int jIndex=0;jIndex返回类型AuthFragment与Fragment不兼容
错误:方法不重写或实现超类型中的方法
如果我让
getItem
返回android.support.v4.Fragment
,那么我会得到不兼容类型
错误。我怎样才能解决这个问题 在将应用程序从Android-O操作系统移植到Android-p移植时,我遇到了同样的问题
这个问题是因为V4、V7旧库用于旧代码。
现在在androidp中,它已经迁移到androidandroidx库。
因此,请将源文件中所有必要的导入从V4、V7、V13更改为androidx
更改文件:导入androidx.legacy\u 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.
为什么投票否决???确保所有类中所有与
片段
相关的导入都来自android.support.v4.app
,包括FragmentStatePagerAdapter
。