PresenterAdapter

Adapters with MVP pattern in a clean way

License

License

GroupId

GroupId

com.github.vicpinm
ArtifactId

ArtifactId

presenteradapter
Last Version

Last Version

1.6.5
Release Date

Release Date

Type

Type

aar
Description

Description

PresenterAdapter
Adapters with MVP pattern in a clean way
Project URL

Project URL

https://github.com/vicpinm/PresenterAdapter
Source Code Management

Source Code Management

https://github.com/vicpinm/PresenterAdapter

Download presenteradapter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.vicpinm/presenteradapter/ -->
<dependency>
    <groupId>com.github.vicpinm</groupId>
    <artifactId>presenteradapter</artifactId>
    <version>1.6.5</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.vicpinm/presenteradapter/
implementation 'com.github.vicpinm:presenteradapter:1.6.5'
// https://jarcasting.com/artifacts/com.github.vicpinm/presenteradapter/
implementation ("com.github.vicpinm:presenteradapter:1.6.5")
'com.github.vicpinm:presenteradapter:aar:1.6.5'
<dependency org="com.github.vicpinm" name="presenteradapter" rev="1.6.5">
  <artifact name="presenteradapter" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.vicpinm', module='presenteradapter', version='1.6.5')
)
libraryDependencies += "com.github.vicpinm" % "presenteradapter" % "1.6.5"
[com.github.vicpinm/presenteradapter "1.6.5"]

Dependencies

compile (2)

Group / Artifact Type Version
com.android.support » appcompat-v7 jar 23.4.0
com.android.support » recyclerview-v7 jar 23.4.0

Project Modules

There are no modules declared in this project.

PresenterAdapter

A lighweight Android library to implement adapters for your RecyclerViews in a clean way, using the MVP pattern.

Features

  • Avoid writing adapter classes, you only has to focus in view clases and their presenters, following the MVP pattern.
  • View representation and view logic decoupled from adapter framework classes.
  • Easy creation of different kinds of views for the same RecyclerView.
  • Avoid create new presenters for each row, presenter instances are recycled in the same way that adapter does with ViewHolder clases.
  • Lifecycle callbacks in presenter clases. You can control view creation and destroy for each RecyclerView position. Presenters are notified when they are destroyed to perform clear and unsubscribe operations if needed.
  • Custom presenter creation. You are responsible for creating presenter instance the same way yo usually do in your Activities or Fragments, which allows you to use tools like Dagger to inject your dependencies (see description below for details).

Download

Grab via Gradle:

repositories {
    mavenCentral()
}

compile 'com.github.vicpinm:presenteradapter:1.5.7'

Usage

Single view type adapter

For adapters with a unique kind of view, there is no need to create any adapter class. SimplePresenterAdapter is provided for this kind of lists.

Adapter creation sample for a list of countries.

Extracted from the sample, CountryView.java is the class which implements the view layer for each adapter position, the same way Activities or Fragment does. Data setter is optional and can be setted in any moment later.

    PresenterAdapter<Country> adapter = SimplePresenterAdapter.with(CountryView.class)
    .setLayout(R.layout.adapter_country)
    .setData(data);

View class

Your view class inherits from ViewHolder class. This class is responsible for implementing the view layer in MPV pattern, equivalent to Activities or Fragments, and creating a presenter instance.

public class CountryView extends ViewHolder<Country> implements CountryPresenter.View {

private CountryPresenter mPresenter;

@BindView(R.id.countryName)
TextView mCountryName;

public CountryView(View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
}

@Override
public void createPresenter() {
    mPresenter = new CountryPresenter();
}

@Override
public ViewHolderPresenter getPresenter() {
    return mPresenter;
}

@Override
public void setCountryName(String text) {
    mCountryName.setText(text);
}

}

Presenter class

Class responsible for implementing the presenter layer in MPV pattern, equivalent to any other presenter. It inherits from ViewHolderPresenter<Data, PresenterView> class. This class is generic and you need to indicate two types, your adapter data type ( in the sample) and your presenter view interface. You have to override onCreate method to setup your view. Also, you can override onDestroy method if you need to implement any destroy logic for your view. Inside your presenter class, you have access to getData() method, in order to get the current data instance for the view, obtained from the adapter data collection. Also, inside your presenter class, you have access to getView() method, in order to interact with your view class.

public class CountryPresenter extends ViewHolderPresenter<Country, CountryPresenter.View> {

    @Override
    public void onCreate() {
        setCountryName();
    }

    public void setCountryName(){
        getView().setCountryName(getData().getName());
    }

    public interface View {
         void setCountryName(String s);
    }
}

Multiple view type adapter

It is very easy to implement multiple view types in for your RecyclerView. Instead of use SimpleAdapterPresenter, you have to implement your own adapter, in order to implement your representation logic. Your adapter class must extends from PresenterAdapter class. PresenterAdapter class has only one abstract method you have to implement, getViewInfo(int position) method. This method returns an instance of ViewInfo class, which holds an association between your view class and your layour resource for a given position.

Example of different types of views based on item position, using the same view class and differents layouts:
public class MultipleAdapter extends PresenterAdapter<Country> {

@Override public ViewInfo getViewInfo(int position) {
    if(position % 2 == 0)
        return ViewInfo.with(CountryView.class).setLayout(R.layout.adapter_country_even);
    else
        return ViewInfo.with(CountryView.class).setLayout(R.layout.adapter_country_odd);
}

}

Example of different types of views based on item properties, using diferent view clases and layouts:

public class MultipleAdapter extends PresenterAdapter {

@Override public ViewInfo getViewInfo(int position) {
    if((getItem(position).isFavourite())
        return ViewInfo.with(FavouriteItemView.class).setLayout(R.layout.adapter_favourite_item);
    else
        return ViewInfo.with(NormalItemView.class).setLayout(R.layout.adapter_normal_item);
}

Event listeners

Click and long click listeners methods are provided to be notified when users interacts with your views. Also, you can set a custom object listener to be manually invoked from your view class when you want. See sample for details.

Proguard

-keepclassmembers public class * extends com.vicpin.presenteradapter.ViewHolder {
    public <init>(...);
}

Author

Víctor Manuel Pineda Murcia | http://vicpinm.github.io/PresenterAdapter/

Versions

Version
1.6.5
1.6.3
1.6.2
1.6.1
1.6.0
1.5.8
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4
1.3
1.2
1.1
1.0.2
1.0