Achaz / Achaz/imsdroid

Performance improvement suggestion

Open
#457 0 comments 0 reactions 0 assignees View on GitHub
auto-migrated Priority-Medium Type-Defect
Dominant language
Java
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

```
Dear developers,

I am a fan of imsdroid, and recently I am writing a static code analysis tool
to conduct performance analysis for Android apps. I found several violations of
"view holder" patterns in imsdroid's code. These violations could affect the
ListView scrolling performance.

Currently in imsdroid, in some list adapters the getView() method works likes
this

public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.listItem, null);
}
((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]);

return convertView;
}

When the users scroll a list view, this implementation avoids inflations for
each view (by using the recycled convertView), which saves CPU cycles and RAM.
However, the method still invokes findViewById() every time when it is called.
Android documentation says that findViewById is an expensive call, it
recursively traverses a view tree to find a view matching the give ID. Google
developers actually suggested a better way to implement getView(). It works
like this:

We define a ViewHolder class with the field: TextView text . Then the getView()
can be implemented like this:

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
//we have no recycled views to use, then build new one
convertView = mInflater.inflate(R.layout.listItem, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.getViewById(R.id.text);
convertView.setTag(holder)
} else {
//use the recycled view to improve performance
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[position]);

return convertView;
}

This avoids calling findViewById frequently and will improve performance when
the list contains many items or on low end devices.

We found the violations of view holder pattern in these classes:

org/doubango/imsdroid/Screens/ScreenAVQueue.java
org/doubango/imsdroid/Screens/ScreenChat.java
org/doubango/imsdroid/Screens/ScreenChatQueue.java
org/doubango/imsdroid/Screens/ScreenFileTransferQueue.java
org/doubango/imsdroid/Screens/ScreenHome.java
org/doubango/imsdroid/Screens/ScreenPresence.java
org/doubango/imsdroid/Screens/ScreenSettings.java
org/doubango/imsdroid/Screens/ScreenTabContacts.java
org/doubango/imsdroid/Screens/ScreenTabHistory.java
org/doubango/imsdroid/Screens/ScreenTabMessages.java

you may find more useful information in thees references:
view holder pattern:
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
http://www.youtube.com/watch?v=wDBM6wVEO70

In the last Google IO video, we find that the current implementation of
getView() in imsdroid is a right way, but not a fast way. The video actually
provides three ways: a slow way, a right way and a fast way.

Looking forward to your reply and hope I can help improve imsdroid :)
```

Original issue reported on code.google.com by `yepang...@gmail.com` on 1 Jul 2013 at 11:31

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by opening the listed ScreenAVQueue.java, ScreenChat.java, ScreenChatQueue.java, ScreenFileTransferQueue.java, ScreenHome.java, ScreenPresence.java, ScreenSettings.java, ScreenTabContacts.java, ScreenTabHistory.java, and ScreenTabMessages.java files and locating their getView() methods. Compare each adapter with the referenced Android ListView view-holder guidance. Done means the listed violations are addressed while list contents and scrolling remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.