Skip to content

RecyclerView 数据切换density变动,会出现适配异常,item一会大一会小 #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
MarcusKu opened this issue Apr 19, 2019 · 12 comments
Closed
5 tasks done

Comments

@MarcusKu
Copy link

Environment

  • Autosize Version:
  • Target Android Version:
  • Device Model:
  • Device Resolution:
  • Design Size On AndroidManifest:

Bug Description:

Log:



Screenshot

Related Code:

  

Others:

@MarcusKu
Copy link
Author

爱神

@JessYanCoding
Copy link
Owner

#59

@liyujiang-gzu
Copy link

用这个方案估计能完美解决了:

    private static final float FONT_NOT_SCALE = 1f;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.fontScale != FONT_NOT_SCALE) {
            getResources();
        }
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        //规避可能的空指针异常
        if (res == null) {
            return null;
        }
        //强制字体大小不随系统改变而改变
        if (res.getConfiguration().fontScale != FONT_NOT_SCALE) {
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                createConfigurationContext(newConfig);
            } else {
                res.updateConfiguration(newConfig, res.getDisplayMetrics());
            }
        }
        //用于解决本来适配正常的布局突然出现适配失效、适配异常等问题
        AutoSizeCompat.autoConvertDensity(res, getSizeInDp(), isBaseOnWidth());
        return res;
    }

@JessYanCoding
Copy link
Owner

@liyujiang-gzu 建议 newConfig.setToDefaults(); 这个 API 少用,这个不光会把 fontScale 置为 1,还会将系统内的很多参数重置,从而影响其他框架,建议使用框架的 API,AutoSizeConfig.getInstance().setExcludeFontScale(true)

@liyujiang-gzu
Copy link

重写getResources()是可以解决RecyclerView.Adapter之类本来适配正常的布局突然失效问题,但是引起了另外一个问题:

两个Activity使用的设计稿尺寸不一样,但是调用了同一个自定义Dialog,这个Dialog在show之前调用AutoSizeAutoSizeCompat的`autoConvert***``方法无法改变适配。

@JessYanCoding
Copy link
Owner

JessYanCoding commented Apr 29, 2019

这个问题,我在 这篇文章 中关于三方库适配时,提到过,就是三方库 View 的设计图尺寸和本页面的设计图尺寸不一致就会导致三方库适配的异常,你需要做的就是,取消这个三方库 View 的适配,或者整个项目使用副单位,放弃对所有三方库 View 的适配

也可以在 show 之前,修改设计图尺寸,然后在 show 后,修改回来,但我估计 getResources() 调用很频繁,难以控制

@liyujiang-gzu
Copy link

这个问题,我在 这篇文章 中关于三方库适配时,提到过,就是三方库 View 的设计图尺寸和本页面的设计图尺寸不一致就会导致三方库适配的异常,你需要做的就是,取消这个三方库 View 的适配,或者整个项目使用副单位,放弃对所有三方库 View 的适配

也可以在 show 之前,修改设计图尺寸,然后在 show 后,修改回来,但我估计 getResources() 调用很频繁,难以控制

谢谢解答。

RecyclerViewItem里含RecyclerView的情景,点击两个RecyclerViewItem弹出Dialog,经试验,在Dialog#show 之前,通过AutoSize#autoConvert***修改尺寸不一定能生效,AutoSize#autoConvert***之后加了View#postDelayed延时一下,在run里进行Dialog#show才能保证适配生效

@JessYanCoding
Copy link
Owner

JessYanCoding commented May 6, 2019

@liyujiang-gzu 你这个方法不知道是不是在所有设备和环境中都可以生效,实在不行可以选择放弃适配

不过 AndroidAutoSize,还支持一个很赞的功能,可以让项目中给主单位和副单位各设置一个设计图尺寸,并且同一个页面可以同时使用主单位和副单位,并允许这两个单位的设计图尺寸不同,貌似这个功能应用到你这个需求就可以实现,你可以试试。

AutoSizeConfig.getInstance().getUnitsManager()
                .setDesignSize();

@chenfan1227
Copy link

我的小米k20 pro 手机上 在fragment中 使用RecyclerView 刚进来的时候 正常 从别的界面回来的时候就开始一个item占整个屏幕了。整个列表全部变形 我已经在onResume 已经调用过了 完全没用 不知道为啥? 求解释下

@chenfan1227
Copy link

也在 onBindViewHolder设置了
AutoSizeConfig.getInstance().isExcludeFontScale = true
AutoSize.autoConvertDensity(context as Activity?,750f, true)
还是没用

@burnzl
Copy link

burnzl commented Dec 22, 2021 via email

@JessYanCoding
Copy link
Owner

#59 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants