Skip to content

Instantly share code, notes, and snippets.

@sheerazam
Last active April 11, 2016 08:40
Show Gist options
  • Save sheerazam/4cbb2d25f7f36b005bd61b557aa04f83 to your computer and use it in GitHub Desktop.
Save sheerazam/4cbb2d25f7f36b005bd61b557aa04f83 to your computer and use it in GitHub Desktop.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.antonioleiva.mvpexample.app.main.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/>
</FrameLayout>
public interface FindItemsInteractor {
public void findItems(OnFinishedListener listener);
}
public class FindItemsInteractorImpl implements FindItemsInteractor {
@Override public void findItems(final OnFinishedListener listener) {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
listener.onFinished(createArrayList());
}
}, 2000);
}
private List<String> createArrayList() {
return Arrays.asList(
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
"Item 9",
"Item 10"
);
}
}
public class MainActivity extends Activity implements MainView, AdapterView.OnItemClickListener {
private ListView listView;
private ProgressBar progressBar;
private MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
progressBar = (ProgressBar) findViewById(R.id.progress);
presenter = new MainPresenterImpl(this);
}
@Override protected void onResume() {
super.onResume();
presenter.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
listView.setVisibility(View.INVISIBLE);
}
@Override public void hideProgress() {
progressBar.setVisibility(View.INVISIBLE);
listView.setVisibility(View.VISIBLE);
}
@Override public void setItems(List<String> items) {
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
@Override public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
presenter.onItemClicked(position);
}
}
public interface MainPresenter {
public void onResume();
public void onItemClicked(int position);
}
public class MainPresenterImpl implements MainPresenter, OnFinishedListener {
private MainView mainView;
private FindItemsInteractor findItemsInteractor;
public MainPresenterImpl(MainView mainView) {
this.mainView = mainView;
findItemsInteractor = new FindItemsInteractorImpl();
}
@Override public void onResume() {
mainView.showProgress();
findItemsInteractor.findItems(this);
}
@Override public void onItemClicked(int position) {
mainView.showMessage(String.format("Position %d clicked", position + 1));
}
@Override public void onFinished(List<String> items) {
mainView.setItems(items);
mainView.hideProgress();
}
}
public interface MainView {
public void showProgress();
public void hideProgress();
public void setItems(List<String> items);
public void showMessage(String message);
}
public interface OnFinishedListener {
void onFinished(List<String> items);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment