FrameWork内核解析之布局加载与资源系统(三)(1)

detachAndScrapAttachedViews(recycler);

actualHeight = 0;

for (int i = 0 ;i < getItemCount() ; i++){

View scrap = recycler.getViewForPosition(i);

addView(scrap);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,actualHeight,width,actualHeight+height);

actualHeight+=height;

}

}

@Override

public boolean canScrollVertically() {

return true;

}

@Override

public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {

//界面向下滚动的时候,dy为正,向上滚动的时候dy为负

//向下滚动的时候,最下面的值不能超过总值,

//向上滚动的时候,最上面的值不能小于0

int willScrollTo = totalScrollY + dy;

Log.d(TAG,"scrollVerticallyBy " + dy + " totalScrollY " + totalScrollY);

if (willScrollTo >= actualHeight-getHeight()){

offsetChildrenVertical(-1*(actualHeight - getHeight() - totalScrollY));

totalScrollY = actualHeight- getHeight();

return 0;

}

if (willScrollTo <= 0){

offsetChildrenVertical(totalScrollY);

totalScrollY = 0;

return 0;

}

offsetChildrenVertical(dy*-1);

totalScrollY +=dy;

return dy;

}

}

加入回收功能

其实就是基于上边的简陋版本进行扩展

onLayoutChildren的时候不添加全部view,只添加可视范围内的View

滑动的时候要更复杂一点

如果向下滚动,先往RecyclerView下面添加即将展示的View

如果往上滚动,就往RecyclerView上面添加即将展示的View

添加完View后就调用offsetChildrenVertical进行滚动

完了后检查是否有子View离开了可视界面,如果不可见了,就是用removeAndRecycleView来移除掉

onLayoutChildren

与之前不同的就是最后几句,如果超过RecyclerView的高度了,就不Add了

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

Log.d(TAG,"onLayoutChildren ");

if (getItemCount() == 0){

detachAndScrapAttachedViews(recycler);

return;

}

//state.isPreLayout()是支持动画的

if (getItemCount() == 0 && state.isPreLayout()){

return;

}

//将当前Recycler中的view全部移除并放到报废缓存里,之后优先重用缓存里的view

detachAndScrapAttachedViews(recycler);

int actualHeight = 0;

for (int i = 0 ;i < getItemCount() ; i++){

View scrap = recycler.getViewForPosition(i);

addView(scrap);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,actualHeight,width,actualHeight+height);

actualHeight+=height;

//超出界面的就不画了,也不add了

if (actualHeight > getHeight()){

break;

}

}

}

scrollVerticallyBy

之前说了,分为填充,滚动,回收

@Override

public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {

Log.d(“feifeifei”,"getChildCount() " + getChildCount() + " recycler.getScrapList().size() " + recycler.getScrapList().size());

//界面向下滚动的时候,dy为正,向上滚动的时候dy为负

//向下滚动的时候,最下面的值不能超过总值,

//向上滚动的时候,最上面的值不能小于0

//填充

fill(dy,recycler,state);

//滚动

offsetChildrenVertical(dy*-1);

//回收已经离开界面的

recycleOut(dy,recycler,state);

return dy;

}

填充

例如向下滚动

通过getChildAt获取最后一个View

再通过getPosition获取这个View的Adapter中的位置,最后一个了,就不要继续填充了,因为没有了,如果有下一个,就继续

这里还没有滑动,但是即将滑动的距离dy传进来了,如果最后一个View滑动dy后小于RecyclerView的高度了说明最后一个View已经全部出现在界面上了,之后就是空白了,需要添加新的子View

那就做获取,测量,添加操作

向上滚动是一样的逻辑

private void fill(int dy, RecyclerView.Recycler recycler, RecyclerView.State state){

//向下滚动

if (dy > 0){

//先在底部填充

View lastView = getChildAt(getChildCount() -1);

int lastPos = getPosition(lastView);

if (lastPos == getChildCount()-1){

return;

}

Log.d(“feifeifei”,“lastView top” + lastView.getTop() + " bottom " + lastView.getBottom());

if (lastView.getBottom() - dy < getHeight()){

View scrap = recycler.getViewForPosition(lastPos+1);

addView(scrap);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,lastView.getBottom(),width,lastView.getBottom()+height);

}

}else {

//向上滚动

//现在顶部填充

View firstView = getChildAt(0);

Log.d(“feifeifei”,“firstView top” + firstView.getTop() + " bottom " + firstView.getBottom());

int layoutPostion = getPosition(firstView);

if (layoutPostion == 0){

return;

}

if (firstView.getTop() >= 0 ){

View scrap = recycler.getViewForPosition(layoutPostion -1);

addView(scrap,0);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,firstView.getTop() - height,width,firstView.getTop());

}

}

}

滚动

滚动就是直接用offsetChildrenVertical(dy*-1);但是需要和简陋版一样,需要搞定边界问题

例如: 到达上边界后,滑动的距离不是dy,而是第一个View还剩下多少距离可以滑动,代码如下

int canScroll = dy;

if (dy>0){

View lastView = getChildAt(getChildCount() -1);

int lastPos = getPosition(lastView);

if (lastPos >= getItemCount()-1){

if (lastView.getBottom() - dy < getHeight()){

canScroll = lastView.getBottom() - getHeight();

offsetChildrenVertical(canScroll*-1);

return 0;

}

}

}else {

View firView = getChildAt(0);

int firstPos = getPosition(firView);

if (firstPos <= 0){

if (firView.getTop() - dy >= 0){

canScroll = firView.getTop();

offsetChildrenVertical(canScroll*-1);

return 0;

}

}

}

回收

通过getChildCount() 获取当前所有的子View

例如向上滚动,name就回收最下面的,最下面的View的top滑动后超出了RecyclerView的高度,说明这个View全部在界面外了,可以回收了,使用removeAndRecycleView移除并回收

向下滚动就判断顶部的Bottom是否小于0

private void recycleOut(int dy, RecyclerView.Recycler recycler, RecyclerView.State state){

for (int i = 0 ; i <getChildCount() ;i++){

View view = getChildAt(i);

Log.d(“feifeifei”,"recycleOut position "+ i + " top " + view.getTop() + " bottom " + view.getBottom());

if (dy >0){

if (view.getBottom()-dy <0){

Log.d(“feifeifei”,"recycleOut " + i);

removeAndRecycleView(view,recycler);

}

}else {

if (view.getTop()-dy > getHeight()){

Log.d(“feifeifei”,"recycleOut " + i);

removeAndRecycleView(view,recycler);

}

}

}

}

这样带回收的LayoutManager也完成了,全部代码如下

/**

  • 填充,滑动,回收

*/

public class CustomLayoutManager2 extends RecyclerView.LayoutManager {

private final String TAG = CustomLayoutManager2.class.getSimpleName();

@Override

public RecyclerView.LayoutParams generateDefaultLayoutParams() {

return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

}

// 1 在RecyclerView初始化时,会被调用两次。

// 2 在调用adapter.notifyDataSetChanged()时,会被调用。

// 3 在调用setAdapter替换Adapter时,会被调用。

// 4 在RecyclerView执行动画时,它也会被调用。

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

Log.d(TAG,"onLayoutChildren ");

if (getItemCount() == 0){

detachAndScrapAttachedViews(recycler);

return;

}

//state.isPreLayout()是支持动画的

if (getItemCount() == 0 && state.isPreLayout()){

return;

}

//将当前Recycler中的view全部移除并放到报废缓存里,之后优先重用缓存里的view

detachAndScrapAttachedViews(recycler);

int actualHeight = 0;

for (int i = 0 ;i < getItemCount() ; i++){

View scrap = recycler.getViewForPosition(i);

addView(scrap);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,actualHeight,width,actualHeight+height);

actualHeight+=height;

//超出界面的就不画了,也不add了

if (actualHeight > getHeight()){

break;

}

}

}

@Override

public boolean canScrollVertically() {

return true;

}

@Override

public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {

Log.d(“feifeifei”,"getChildCount() " + getChildCount() + " recycler.getScrapList().size() " + recycler.getScrapList().size());

//界面向下滚动的时候,dy为正,向上滚动的时候dy为负

//向下滚动的时候,最下面的值不能超过总值,

//向上滚动的时候,最上面的值不能小于0

int canScroll = dy;

if (dy>0){

View lastView = getChildAt(getChildCount() -1);

int lastPos = getPosition(lastView);

if (lastPos >= getItemCount()-1){

if (lastView.getBottom() - dy < getHeight()){

canScroll = lastView.getBottom() - getHeight();

offsetChildrenVertical(canScroll*-1);

return 0;

}

}

}else {

View firView = getChildAt(0);

int firstPos = getPosition(firView);

if (firstPos <= 0){

if (firView.getTop() - dy >= 0){

canScroll = firView.getTop();

offsetChildrenVertical(canScroll*-1);

return 0;

}

}

}

//底部填充

fill(dy,recycler,state);

//滚动

offsetChildrenVertical(dy*-1);

//回收已经离开界面的

recycleOut(dy,recycler,state);

return dy;

}

private void fill(int dy, RecyclerView.Recycler recycler, RecyclerView.State state){

//向下滚动

if (dy > 0){

//先在底部填充

View lastView = getChildAt(getChildCount() -1);

int lastPos = getPosition(lastView);

if (lastPos == getChildCount()-1){

return;

}

Log.d(“feifeifei”,“lastView top” + lastView.getTop() + " bottom " + lastView.getBottom());

if (lastView.getBottom() - dy < getHeight()){

View scrap = recycler.getViewForPosition(lastPos+1);

addView(scrap);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,lastView.getBottom(),width,lastView.getBottom()+height);

// bottomItemPos++;

}

}else {

//向上滚动

//现在顶部填充

View firstView = getChildAt(0);

Log.d(“feifeifei”,“firstView top” + firstView.getTop() + " bottom " + firstView.getBottom());

int layoutPostion = getPosition(firstView);

if (layoutPostion == 0){

return;

}

if (firstView.getTop() >= 0 ){

View scrap = recycler.getViewForPosition(layoutPostion -1);

addView(scrap,0);

measureChildWithMargins(scrap,0,0);

int width = getDecoratedMeasuredWidth(scrap);

int height = getDecoratedMeasuredHeight(scrap);

layoutDecorated(scrap,0,firstView.getTop() - height,width,firstView.getTop());

}

}

}

private void recycleOut(int dy, RecyclerView.Recycler recycler, RecyclerView.State state){

for (int i = 0 ; i <getChildCount() ;i++){

View view = getChildAt(i);

// Log.d(“feifeifei”,"recycleOut position "+ i + " getDecoratedTop " + getDecoratedTop(view) + " getDecoratedBottom " + getDecoratedBottom(view));

Log.d(“feifeifei”,"recycleOut position "+ i + " top " + view.getTop() + " bottom " + view.getBottom());

if (dy >0){

if (view.getBottom()-dy <0){

Log.d(“feifeifei”,"recycleOut " + i);

removeAndRecycleView(view,recycler);

}

}else {

if (view.getTop()-dy > getHeight()){

Log.d(“feifeifei”,"recycleOut " + i);

removeAndRecycleView(view,recycler);

}

}

}

}

}

然后通过adapter打印日志,onCreateViewHolder只打印了7次(我的界面上显示的7个item),然后滚动界面的时候,onBindViewHolder依次打印.看来回收还是成功的.这样一个简单版的带回收的LinearLayoutManager就好了

二、Resources 和 AssetManager

=============================================================================================

在 Android 开发中我们使用 Resources 来获取 res 目录下的各种与设备相关的资源。而使用 AssetManager 来获取 assets 目录下的资源。

一般来说 Resources 对象通过 Context 获得。

appContext 中的 resources 是在创建之后通过如下代码设置。

context.setResources(packageInfo.getResources());

而 LoadedApk 中则是通过如下代码创建:

mResources = ResourcesManager.getInstance().getResources(null, mResDir,

splitPaths, mOverlayDirs, mApplicationInfo.sharedLibraryFiles,

Display.DEFAULT_DISPLAY, null, getCompatibilityInfo(),

getClassLoader());

其中 mResDir 对应 ApplicationInfo.sourceDir 字段

/**

  • Full path to the base APK for this application.

*/

public String sourceDir;

ResourcesManager.getInstance().getResources 的主要逻辑如下:

final ResourcesKey key = new ResourcesKey(

resDir,

splitResDirs,

overlayDirs,

libDirs,

displayId,

overrideConfig != null ? new Configuration(overrideConfig) : null, // Copy

compatInfo);

classLoader = classLoader != null ? classLoader : ClassLoader.getSystemClassLoader();

return getOrCreateResources(activityToken, key, classLoader);

getOrCreateResources 主要逻辑如下:

// 1) 先创建 ResourcesImpl

ResourcesImpl resourcesImpl = createResourcesImpl(key);

// 2) 再创建 Resources

resources = getOrCreateResourcesLocked(classLoader, resourcesImpl, key.mCompatInfo);

createResourcewImpl 主要逻辑如下:

final AssetManager assets = createAssetManager(key);

if (assets == null) {

return null;

}

final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId, daj);

final Configuration config = generateConfig(key, dm);

final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, daj);

return impl;

其中 createAssetManager(key) 的主要逻辑如下:

// 1) 创建 assets 对象实例。

AssetManager assets = new AssetManager();

// 2) 添加各资源目录。

assets.addAssetPath(key.mResDir)

assets.addAssetPath(splitResDir)

assets.addOverlayPath(idmapPath);

assets.addAssetPathAsSharedLibrary(libDir)

最终 Resources 的创建逻辑如下:

// getOrCreateResourcesLocked

Resources resources = compatInfo.needsCompatResources() ? new CompatResources(classLoader)
new Resources(classLoader);

resources.setImpl(impl);

####以 Resouces.getString 来查看资源的读取流程

1)调用 getText

public String getString(@StringRes int id) throws NotFoundException {

return getText(id).toString();

}

2)通过 resourcesImpl 调用 AssetsManager 的 getResourceText

@NonNull public CharSequence getText(@StringRes int id) throws NotFoundException {

CharSequence res = mResourcesImpl.getAssets().getResourceText(id);

if (res != null) {

return res;

}

throw new NotFoundException(“String resource ID #0x”

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持)

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT大厂面试题(有解析)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

[外链图片转存中…(img-Nzt0pcU9-1712639133197)]

[外链图片转存中…(img-rZZEgeTC-1712639133198)]

[外链图片转存中…(img-GmyTSpGA-1712639133198)]

[外链图片转存中…(img-bMUJxfZd-1712639133198)]

[外链图片转存中…(img-VEp7SbGk-1712639133199)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持)

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

[外链图片转存中…(img-e75gTJv2-1712639133199)]

[外链图片转存中…(img-7zg6muT8-1712639133199)]

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

[外链图片转存中…(img-OWBop9DB-1712639133200)]

  • Android BAT大厂面试题(有解析)

[外链图片转存中…(img-c3HkdzWj-1712639133200)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值