Top Banner
Adapter and cache technique Trí Phạm – MultiUni - 2012
21

Adapter and cache technique

Jun 21, 2015

Download

Technology

Nguyen Hoang Vy

Slide for lesson of Wednesday 23/5/2012. Great appreciation for our lecturers. =)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Adapter and cache technique

Adapter and cache technique

Trí Phạm – MultiUni - 2012

Page 2: Adapter and cache technique

Adapter and cache technique

• Adapter in Android

• Cache technique in Android

Page 3: Adapter and cache technique

ADAPTER IN ANDROID

Page 4: Adapter and cache technique

Adapter in Android

• Adapter design pattern

Page 5: Adapter and cache technique

Adapter in Android

• Adapter design pattern (con’t)

Page 6: Adapter and cache technique

Adapter in Android

• Adapter design pattern (con’t)

Page 7: Adapter and cache technique

Adapter in Android

• Adapter design pattern (con’t)

Page 8: Adapter and cache technique

• An Adapter object acts as a bridge between an View and the underlying data for that view.

• One side of the adapter is a data structure like a Java object storing data. the other side of the adapter, there is view that the data structure was transformed into.

• The Adapter is also responsible for making a View for each

item in the data set.

Adapter in Android

Page 9: Adapter and cache technique

Adapter in Android

Page 10: Adapter and cache technique

Adapter in Android

• Android ListView

Page 11: Adapter and cache technique

Adapter in Android

• Android ListView (con’t)

– Naive and dumb way

– The correct way: List13.java

– The fast way: List14.java

Page 12: Adapter and cache technique

Adapter in Android

• Considering Adapter for the List View– Each List item in the List View would be calling its

“getView()” method.

– This method would return a view for the List.

– But creating a new View for each list item would be complicated, and the UI would be slow down.

– Instead of creating new View for each item, Recycler save these view and passes it to the Adapter.

– Adapter uses these view sent by the recycler, when it has to produce any new views.

Page 13: Adapter and cache technique

Adapter in Android

• View Holders– View Holders hold some data that related to an item to

the view.

– View Holders minimizes the amount of work, while writing the same piece of code again and again.

– These are used while creating the views, where we have to use more than one time.

(TextView)findViewById(R.Id.text);

(ImageView)findViewById(R.Id.Icon);

Page 14: Adapter and cache technique

Adapter in Android

• View Holders (con’t)– By using the View Holders, the views are saved into a

class variable and can be called when needed.

holder.text = (TextView)findViewById(R.id.text);

holder.icon = (ImageView)findViewById(R.id.icon);

convertView.setTag(holder);

– A tag can hold any kind of object, that can be set on the view.

Page 15: Adapter and cache technique

Adapter in Android

• Android ListView

Page 16: Adapter and cache technique

CACHE TECHNIQUE IN ANDROID

Page 17: Adapter and cache technique

• Basic cache view technique (for views that have scrolling behavior)

• Cache data technique

Cache technique in Android

Page 18: Adapter and cache technique

Adapter in Android

• Basic cache view technique (for views that have scrolling behavior)– Demo EfficientListView

Page 19: Adapter and cache technique

Adapter in Android

• Cache data technique

– Why using cache:

• Some operations are high CPU load and time consuming. E.g.: load images from network…

-> Never do something time consuming twice

• Mobile applications, especially Android applications needs optimization for smooth UI.

Page 20: Adapter and cache technique

Adapter in Android

• Cache data technique

– Demo ImageDownloader with soft (memory) cache

Page 21: Adapter and cache technique

Q & A