Skip to content

Update Android性能优化.md #73

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Part1/Android/Android性能优化.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ Tips:如果我们要在<include>标签中覆写layout属性,必须要将layout

举例:比如在LinearLayout里边使用一个<include>布局。<include>里边又有一个LinearLayout,那么其实就存在了多余的布局嵌套,使用merge可以解决这个问题。

###利用视图复用机制

listView的item比较大,高度不固定时会出现一个问题:

当前几个item高度比较高,2个item就占满了整个屏幕,listView由于内部的视图复用机制,会生成2个item的实例,继续往后滑动后面的item比较短,1个页面可以容纳10个,这时往后滑动,listView就会继续实例化8个item,导致初次滑动顿卡,但是初次滑动完成后,后面就不会再实例化了也就很流畅了。
解决措施:
实例化一个item就很卡,那说明这个item的layout文件写的有问题,是不是里面的View层次太多或者是View的数量众多。如果View层次较深,可以对linearlayout多用Relativelayout替换,减少嵌套层次;如果是View数量太多,看看怎么减少view的数量。还有getView方法里的逻辑是不是太复杂,把里面能够精简的东西尽量简化,去掉不必要的循环逻辑,加快getView的执行速度。
       如果是业务方面的话,那就要靠自己思考怎么更好优化业务逻辑了。如果的item是因为数据有不同的类型展示不同的数据,不是某种类型就隐藏它对应的view,那么建议lz使用getViewTypeCount, ViewType把item拆开。


###仅在需要时才加载布局

某个布局当中的元素不是一起显示出来的,普通情况下只显示部分常用的元素,而那些不常用的元素只有在用户进行特定操作时才会显示出来。
Expand Down