View - nhaarman/Triad GitHub Wiki
The View
class presents the data to the user, and passes user interaction to the Presenter
. It implements the Container
interface to act as a bridge between the Presenter
and the View
.
View creation
The View
is instantiated by the Android system using the layout inflater. The layout resource id is defined in the Screen
class, and the layout file should have the View
class as its root:
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/view_my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/view_my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view_my_textview"
android:layout_centerHorizontal="true" />
</com.example.MyView>
The View Class
The View
class itself must implement the MyContainer
interface, and have at least the necessary constructors:
public class MyView extends RelativeLayoutContainer<MyPresenter, ActivityComponent> implements MyContainer {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/* ... */
}
Child Views
Child View
s such as the TextView
in the above example can be injected automatically using Butter Knife:
public class MyView extends RelativeLayoutContainer<MyPresenter, ActivityComponent> implements MyContainer {
/* ... */
@InjectView(R.id.view_my_textview)
protected TextView mTextView;
@OnClick(R.id.view_my_button)
public void onButtonClicked() {
getPresenter().onIncrementButtonClicked();
}
}
Abstract Containers
The RelativeLayoutContainer
class manages the Presenter
handling and View
injection. It is recommended to extend this class for your View
s. More of these container classes such as LinearLayout
will follow soon.