View Types

View

This is a generic view interface for implementing single-item screens or forms.

Built-in implementation

  • AbstractActivity
  • AbstractFragment
  • AbstractDialogFragment
  • AbstractActionBarActivity
  • AbstractAppCompatActivity

Sample

package com.example.myapplication;

import com.robo.mvp.View;

/**
 * Defines a login form.
 */
public interface LoginView extends View {

    /**
     * Gets input username.
     */
    String getUsername();

    /**
     * Gets input password.
     */
    String getPassword();

    /**
     * Listens to Authenticate event on the view.
     */
    interface OnAuthenticateListener {
        void onAuthenticate();
    }
}

List View

This interface defines a view that displays a list of items.

Built-in implementation

  • AbstractListActivity
  • AbstractListFragment

Sample

The products view below displays a simple list of product names. Every item is a string.

package com.example.myapplication;

import com.robo.mvp.ListView;

/**
 * Defines the view that displays a list of product categories.
 */
public interface ProductCategoriesView extends ListView<String> {
    void showMessage(String message);
}

In the presenter, you can call view.setModel(String[]) to bind data to the view. The view also has OnListItemClickListener, listening to ListItemClick event fired whenever user taps an item on the view.

package com.example.myapplication;

import com.robo.mvp.AbstractPresenter;
import com.robo.mvp.ListView;
import com.robo.mvp.View;

public class ProductCategoriesPresenter extends AbstractPresenter<ProductCategoriesView> {
    @Override
    protected void onViewSet(final ProductCategoriesView view) {
        view.getListeners().set(View.OnReadyListener.class, new View.OnReadyListener() {
            @Override
            public void onReady() {
                view.setModel(new String[] {"Home Appliances", "Decorations", "Electronics",
                        "Accessories"});
            }
        });

        view.getListeners().set(ListView.OnListItemClickListener.class, new ListView.OnListItemClickListener() {
            @Override
            public void onListItemClick(Object item, int position, long id) {
                view.showMessage("You've selected " + item.toString());
            }
        });
    }
}

Expandable List View

This interface defines a view that displays a two-level list of items.

Built-in implementation

  • AbstractExpandableListActivity

Sample

The view below displays a two-level list of items: Categories and Products in each category.

package com.example.myapplication;

import com.robo.mvp.ExpandableListView;

public interface ByCategoryProductsView extends ExpandableListView<String, String> {

    void showMessage(String message);
}

This view accepts a model of ExpandableListView.ViewModel. To bind data to the view, you can call view.setModel(ExpandableListView.ViewModel(String[], String[][])).

The view has OnChildClickListener, listening to ChildClick event fired whenever user taps an item in the child list.

The view also has OnGroupCollapseExpandListener, listening to GroupCollapseExpand event fired whenever user collapses or expands a parent group.

package com.example.myapplication;

import com.robo.mvp.AbstractPresenter;
import com.robo.mvp.ExpandableListView;
import com.robo.mvp.View;

public class ByCategoryProductsPresenter extends AbstractPresenter<ByCategoryProductsView> {

    private static String[] sCategories;
    private static String[][] sProducts;

    static {
        sCategories = new String[]{"Home Appliances", "Decorations"};
        sProducts = new String[][]{{"Vacuum", "Cooker", "Refrigerator"}, {"Flowers", "Curtains"}};
    }

    @Override
    protected void onViewSet(final ByCategoryProductsView view) {
        view.getListeners().set(View.OnReadyListener.class, new View.OnReadyListener() {
            @Override
            public void onReady() {
                view.setModel(new ExpandableListView.ViewModel(sCategories, sProducts));
            }
        });

        view.getListeners().set(ExpandableListView.OnChildClickListener.class, new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(int parentPosition, int childPosition, long id) {
                view.showMessage("You've selected " + sProducts[parentPosition][childPosition]);
                return true;
            }
        });

        view.getListeners().set(ExpandableListView.OnGroupCollapseExpandListener.class, new ExpandableListView.OnGroupCollapseExpandListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                view.showMessage("You've collapsed " + sCategories[groupPosition]);
            }

            @Override
            public void onGroupExpand(int groupPosition) {
                view.showMessage("You've expanded " + sCategories[groupPosition]);
            }
        });
    }
}