displaying large contentberaldi/macc_18/05bis.pdf · displaying long content: listactivity • it...
TRANSCRIPT
DISPLAYING LARGE CONTENTRoberto Beraldi
Displaying long content• The screen size of a device is limited, so many times to
show all the content at once is unfeasible• This is a common issue and android provides special
classes and layouts to deal with this problem• There are different solutions (older�new):• ListActivity• ListView• GridView• CardView• RecyclerView
Displaying long content: ListActivity• It is an subclass of Activity• There is no layout to inflate• Allows to display an array of items, that are ‘clickable’• An ‘ArrayAdapter’ is required to transform an item (data) into a view
• A predefined layout is used to create the view • By default, the array adapter creates a view by calling toString() on each data object
in the provided collection, and places the result in a TextView.
• Warning: Action bar is not displayed
ListActivity (example)
Displaying long content: listView andGridView• Uses a special layout (named listView or gridView)• Similar to ListActivity, but can extend AppCompatActivity
ListActivity, ListView, GridView compared
Displaying long content: recyclerView• Is ‘faster’ and more flexible• Uses a recyclerView layout• It automatically ‘recycles’ views as they are no longer
visible (more efficient than others)• It requires a LayoutManager to manage views
• (for example the scrolling can be done horizontally)
• It requires an Adapter to create a single View and update data of the View• Allows ‘animations’ (e.g., it pushes the toolbar out of the
screen)
Displaying long content: recyclerView
Recycler view and (collapsing) toolbar
Example
CardView• CardView extends the FrameLayout class and show
information inside cards• CardView widgets can have shadows and rounded
corners.
Spinner• Like a menu, it shows a set of options• It requires an adapter
List an device rotation• As after a device rotation the activity is re-created, it could
be required to store the ‘state’ of the list (i.e., which is the first item visible)
• Methods exit to get/set the first item to be shown
Fetching data from the network• If data source is remotely located on a network, it is worth
to use an additional thread to fetch data asynchronously• The adapter is notified when data are available• For long query, a page based solution must be used
HTTPClient
AsyncTask…
Adapter ListView
notifyDataChange
LocalData
Source
stor
eda
ta
getView
ViewdataHTTP/JSON