I am trying to implement MVP in my android project. But I am getting difficulty when i implement MVP with viewPagerAdapter
. To implement MVP, we used to create fragment/view and presenter instance in Activity
class.Fragment/view and presenter both will communicate with each other using contract. In this way, we decoupled view with presenter and cover all business logic with Unit test. But in case when we have tablayout
inside activity
. We have initiated fragments inside fragmentPagerAdapter
. I am not sure, how to provide tab-fragment/view reference to its corresponding presenter.If I initialize presenter inside fragmentPagerAdapter
that would bleach SRP(Single responsibility principal). If we create presenter instance inside Fragment
that would generate coupled code and then we don't need contact to establish communication between fragment and presenter. Please provide architectural solution, so that i can achieve same MVP with view pager along with adhering all clean code principal.
For reference, please find below my adapter code, where i am initializing fragments for tab:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return getConfigurationsTabFragment();
case 1:
return UpdateTabFragment.newInstance();
case 2:
return ServiceTabFragment.newInstance();
default:
return null;
}
}
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.
Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android. app.
The MVP pattern allows separating the presentation layer from the logic so that everything about how the UI works is agnostic from how we represent it on screen. Ideally, the MVP pattern would achieve that the same logic might have completely different and interchangeable views.
private class MyAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragement(Fragment fragment){
mFragmentList.add(fragment);
}
}
use it in activity:
MyAdapter adapter = new MyAdapter(getChildFragmentManager());
Fragment fragment1 = EmptyFragment.newInstance("this is page 1");
Fragment fragment2 = EmptyFragment.newInstance("this is page 2");
presenter1.setxxx(fragment1);
presenter2.setxxx(fragment2);
adapter.addFragement(fragment1);
adapter.addFragement(fragment2);
viewPager.setAdapter(adapter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With