Professional Documents
Culture Documents
Original architecture coupled Activity & View
Tablets and future Wearables
A Fragment represents a behavior or a portion of user interface, has its own lifecycle,
receives its own input events, and can be added or removed while the activity is
running
Native Android or Support Libraries?
Google support-v4 library includes fragment support
The single support library is now a group of libraries: support-v7, appcompat-
v7, recyclerview-v7 etc.
Avoid the native Android OS implementations and use the Support library versions.
Many more releases and easier to update.
FragmentManager
View decoupled by delegating UI to Fragment
Activity uses placeholder views for fragments
Activity uses a FragmentManager to dynamically
add and remove Fragments from Views in a
fragmentManager transaction.
FragmentManager transactions can change fragment
combinations for different screen sizes
Once added, a fragment lives in a ViewGroup inside the
activity's view hierarchy and affected by
the Activity lifecycle
Fragment LifecycleCommonlifecycle methods implemented:
onCreate() initialize components of the fragment to
retain when the fragment is paused or stopped, then
resumed
onCreateView() called for fragment to draw its user
interface and return a view
onPause() first indication user is leaving the fragment;
persist session data
Simple code
with getActivity
method to access the
parent Activity's intent
data directly leads
to coupling
Static method to
bundle any arguments
and return
the fragment leads
to decoupling as any
activity can pass its
data and receive the
fragment
Model‐View‐Presenter(MVP)
Activities/Fragments are part of the View layer
Activity has corresponding Presenter for access to Model
Code example TodoMVP
Model‐View‐ViewModel
Android final architecture, MVx! to MVVM
Android Data Binding Framework ‐ ViewModel retrieve data from model when
requested from the view
Data binding pattern reduces code and Activities/Fragments become very light weight
Unit testing made easier ‐ ViewModel are decoupled from the view
ViewModel have process scope
MVP the Presenter calls the view to display
MVVM the ViewModel exposes stream of events for Views to bind to. No need for all
the MVP interfaces.
Separation of concerns (MVC, MVP, MVVM), MVVM advantage is data binding making
event driven apps easier by delegating to Model and minimizing logic in Views
About RecyclerView
When you display a large number of items in a scrollable list, most of the items aren't visible.
For example, in a long list of words or news headlines, the user only sees a
few items at a time.
Or you may have a dataset that changes as the user interacts with it. If you
create a new View every time the data changes, that's a lot of View items,
even for a small dataset.
From a performance perspective, you want to conserve memory and save
time:
To accomplish both these goals, create more View items than the user can see on the screen
and cache the created View items. Then reuse
the View items with different data as list items scroll in
and out of the display.
The RecyclerView class is a more advanced and flexible
version of ListView. It's a container for displaying large,
scrollable data sets efficiently by maintaining a limited
number of View items.
Use RecyclerView when you need to display a large
amount of scrollable data, or data collections whose
elements change at runtime based on user action or
network events.
RecyclerView components
Data. It doesn't matter where the data comes from. You can create the data locally,
as you do in the practical, get it from a database on the device as you will do in a later
practical, or pull it from the cloud.
A RecyclerView. The scrolling list that contains the list items. An instance
of RecyclerView as defined in the Activity layout file to act as the container for
the View items.
Layout for one item of data. All list items look the same, so you can use the same
layout for all of them. The item layout has to be created separately from
the Activity layout, so that one View item at a time can be created and filled with
data.
A layout manager. The layout manager handles the organization (layout) of user
interface components in a View. Each ViewGroup has a layout manager.
For LinearLayout, the Android system handles the layout for
you. RecyclerView requires an explicit layout manager to manage the arrangement of
list items contained within it. This layout could be vertical, horizontal, or a grid. The
layout manager is an instance of Recyclerview.LayoutManager to organize the layout
of the items in the RecyclerView.
An adapter. Use an extension of RecyclerView.Adapter to connect your data to
the RecyclerView. It prepares the data and how will be displayed in a ViewHolder.
When the data changes, the adapter updates the contents of the respective list item
view in the RecyclerView.
A ViewHolder. Use an extension of RecyclerView.ViewHolder to contain the
information for displaying one View item using the item's layout.
Data
Text
Images
Icons
RecyclerView
Item Layout
The layout for a list item is kept in a separate XML layout file so that the adapter can
create View items and edit their contents independently from the layout of the Activity.
Layout Manager
Animations
Animations for adding and removing items are enabled by default in RecyclerView. To
customize these animations, extend the RecyclerView.ItemAnimator class and use
the RecyclerView.setItemAnimator() method.
Adapter
ViewHolder
Implementing a RecyclerView
Create an XML resource file and specify the layout of one item. The adapter uses this code to
create the ViewHolder.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="@+id/word"
style="@style/word_title" />
</LinearLayout>
The TextView has a @style element. A style is a collection of properties that specifies the
look of a View. You can use styles to share display attributes with multiple View elements. An
easy way to create a style is to extract the style of a View element that you already created.
For example, after styling a TextView:
Extend RecyclerView.Adapter and implement
the onCreateViewHolder() and onBindViewHolder() methods.
Create a new Java class with the following signature:
public class WordListAdapter extends
RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
In the constructor, get an inflater from the current context, and your data.
The ViewHolder class for your item layout is usually defined as an inner class to the adapter.
Extend RecyclerView.ViewHolder to create the ViewHolder. You can add click behavior by
overriding the onClick()method.
class WordViewHolder extends RecyclerView.ViewHolder {
If you want to add click handling, you need to implement View.onClickListener. One way to
do this is to have the ViewHolder implement the View.onClickListener methods.
// Extend the signature of WordViewHolder to implement a click listener.
class WordViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
In its constructor, the ViewHolder has to inflate its layout, associate with its adapter, and, if
applicable, set a click listener.
public WordViewHolder(View itemView, WordListAdapter adapter) {
super(itemView);
wordItemView = itemView.findViewById(R.id.word);
this.mAdapter = adapter;
itemView.setOnClickListener(this);
}
And, if you implementing View.onClickListener, you also have to implement onClick().
@Override
public void onClick(View v) {
wordItemView.setText ("Clicked! "+ wordItemView.getText());
}
If you want to attach click listeners to other elements of the ViewHolder, do that dynamically
in onBindViewHolder() (you will do this in another practical).
1. Declare a RecyclerView.
2. private RecyclerView mRecyclerView;
3. In the Activity onCreate() method, get a handle to the RecyclerView in the layout:
4. mRecyclerView = findViewById(R.id.recyclerview);
5. Create an adapter and supply the data to be displayed.
6. mAdapter = new WordListAdapter(this, mWordList);
7. Connect the adapter with the RecyclerView.
8. mRecyclerView.setAdapter(mAdapter);
9. Give the RecyclerView a default layout manager.
10. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerView is an efficient way to display scrolling list data. It uses the adapter
pattern to connect data with list item views. To implement a RecyclerView, you need
to create an adapter and a ViewHolder. You also need to create the methods that
take the data and add it to the list items.
DAO: Data access object. A mapping of SQL queries to functions. You used to have
to define these queries in a helper class, such as SQLiteOpenHelper. When you use a
DAO, you call the methods, and the components take care of the rest.
Room database: Database layer on top of SQLite database that takes care of
mundane tasks that you used to handle with a helper class, such as SQLiteOpenHelper.
Provides easier local data storage. The Room database uses the DAO to issue
queries to the SQLite database.
Repository: A class that you create for managing multiple data sources, for example
using the WordRepository class.
ViewModel: Provides data to the UI and acts as a communication center between the
Repository and the UI. Hides the backend from the UI. ViewModel instances survive
device configuration changes.
LiveData: A data holder class that follows the observer pattern, which means that it
can be observed. Always holds/caches latest version of data. Notifies its observers
when the data has changed. LiveData is lifecycle aware. UI components observe
relevant data. LiveData automatically manages stopping and resuming observation,
because it's aware of the relevant lifecycle status changes.