I am trying to implement Click functionality on an item inside a RecyclerView
using airbnb.epoxy.
The problem is that i need context in order to navigate to another activity upon click.
What i have done: Following Epoxy's sample app I implemented an interface inside the EpoxyController that contains the function to be called when clicking an item in the recycler view. I then make my main activity implement this interface and method, and instantiate the controller using its constructor inside the main activity and passing it a reference to the activity:
public class MortgageController extends TypedEpoxyController<List<Mortgage>> {
private final MortgageCallBacks callBacks;
public MortgageController(MortgageCallBacks callBacks) {
this.callBacks = callBacks;
}
@Override
protected void buildModels(List<Mortgage> mortgages) {
Observable.from(mortgages)
.subscribe(mortgage -> new MortgageModel_().id(mortgage.id())
.mortgage(mortgage)
.clicks(callBacks::onMortgageClicked)
.addTo(this));
}
public interface MortgageCallBacks {
void onMortgageClicked(Mortgage mortgage);
}
}
main activity's onCreate and onMortgageClick
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controller = new MortgageController(this);
initRecycler();
controller.setData(mortgages);}
@Override
public void onMortgageClicked(Mortgage mortgage) {
DetailActivity.start(this, mortgage);
}
what i want to do
while the above work, i am using Dagger2
in my project and would like to inject the controller into the MainActivity
, injecting it is not the problem rather supplying the activity context, after a little bit of research i found epoxy allow activity injection, so i thought i could inject the main activity to the controller, but i am not sure this is the best way to go, and couldn't find example projects that implement this.
please enlighten me in what is the best way to do this