ANDROID MVP FOR BEGINNERS — BEST FOLDER STRUCTURE

public interface BaseView {
void start();
}
public interface BasePresenter <T extends BasePresenter> {
void setPresenter(T presenter);
}
public final class User {
@Nullable
private final String mName;
public User(@Nullable String name) {
mName = name;
}
}
public class UsersActivity extends AppCompatActivity { private UsersPresenter mUsersPresenter; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsersFragment usersFragment =
(UsersFragment) getSupportFragmentManager().findFragmentById(R.id.contentFrame);
if (usersFragment == null) {
// Create the fragment
usersFragment = UsersFragment.newInstance();
ActivityUtils.addFragmentToActivity(
getSupportFragmentManager(), usersFragment, R.id.contentFrame);
}
// Create the presenter
mUsersPresenter = new UsersPresenter(
usersFragment
);
}
}
public interface UsersContract { interface View extends BaseView<Presenter> { } interface Presenter extends BasePresenter { }
}
public class UsersPresenter implements UsersContract.Presenter { private final UsersContract.View mUsersView;
@Override
public void start() {
loadUsers();
}
public void loadUsers() {
}
public UsersPresenter(@NonNull UsersContract.View usersView) {
mUsersView = checkNotNull(usersView, “usersview cannot be null!”);
mUsersView.setPresenter(this);
}
}
public class UsersFragment extends Fragment implements UsersContract.View { private UsersContract.Presenter mPresenter;
private UsersAdapter mListAdapter;
@Override
public void setPresenter(UsersContract.Presenter presenter) {
mPresenter = checkNotNull(presenter);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<User> na = new ArrayList<User>();
na.add(new User(“saman”));
na.add(new User(“test name”));
mListAdapter = new UsersAdapter(na, mUserListener);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.tasks_frag, container, false);
ListView listView = (ListView) root.findViewById(R.id.tasks_list);
listView.setAdapter(mListAdapter);
return root;
}
public static UsersFragment newInstance() {
return new UsersFragment();
}
UserItemListener mUserListener = new UserItemListener() {
@Override
public void onUserClick(User clickedUser) {
}
};
private static class UsersAdapter extends BaseAdapter { private List<User> mUsers;
private UserItemListener mUserListener;
public UsersAdapter(List<User> tasks, UserItemListener userListener) {
setList(tasks);
mUserListener = userListener;
}
public void replaceData(List<User> tasks) {
setList(tasks);
notifyDataSetChanged();
}
private void setList(List<User> tasks) {
mUsers = checkNotNull(tasks);
}
@Override
public int getCount() {
return mUsers.size();
}
@Override
public User getItem(int i) {
return mUsers.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View rowView = view;
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
rowView = inflater.inflate(R.layout.user_item, viewGroup, false);
}
final User user = getItem(i); TextView titleTV = (TextView) rowView.findViewById(R.id.title);
titleTV.setText(user.getName());
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mUserListener.onUserClick(user);
}
});
return rowView;
}
}
public interface UserItemListener { void onUserClick(User clickedUser);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store