More concise management of the main recycler view.

No longer bind the views as they become visible. This resulted in
the same view being bound multiple times, but that was unneccesary.
Given that there is only five types of view, and each view type only
ever gets used once, the binding can happen when the view holder is
created, rather than each time it is shown. This fixed a few bugs to
do with views being inflated multiple times.
This commit is contained in:
Peter Serwylo 2016-12-19 12:41:29 +11:00
parent 952024768a
commit 6ad1f0faef
2 changed files with 33 additions and 18 deletions

View File

@ -41,6 +41,30 @@ class MainViewAdapter extends RecyclerView.Adapter<MainViewController> {
@Override
public MainViewController onCreateViewHolder(ViewGroup parent, int viewType) {
MainViewController holder = createEmptyView();
switch (viewType) {
case R.id.whats_new:
holder.bindWhatsNewView();
break;
case R.id.categories:
holder.bindCategoriesView();
break;
case R.id.nearby:
holder.bindSwapView();
break;
case R.id.my_apps:
holder.bindMyApps();
break;
case R.id.settings:
holder.bindSettingsView();
break;
default:
throw new IllegalStateException("Unknown view type " + viewType);
}
return holder;
}
private MainViewController createEmptyView() {
FrameLayout frame = new FrameLayout(activity);
frame.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return new MainViewController(activity, frame);
@ -48,20 +72,10 @@ class MainViewAdapter extends RecyclerView.Adapter<MainViewController> {
@Override
public void onBindViewHolder(MainViewController holder, int position) {
long menuId = getItemId(position);
if (menuId == R.id.whats_new) {
holder.bindWhatsNewView();
} else if (menuId == R.id.categories) {
holder.bindCategoriesView();
} else if (menuId == R.id.nearby) {
holder.bindSwapView();
} else if (menuId == R.id.my_apps) {
holder.bindMyApps();
} else if (menuId == R.id.settings) {
holder.bindSettingsView();
} else {
holder.clearViews();
}
// The binding happens in onCreateViewHolder. This is because we never have more than one of
// each type of view in this main activity. Therefore, there is no benefit to re-binding new
// data each time we navigate back to an item, as the recycler view will just use the one we
// created earlier.
}
@Override
@ -69,6 +83,11 @@ class MainViewAdapter extends RecyclerView.Adapter<MainViewController> {
return positionToId.size();
}
@Override
public int getItemViewType(int position) {
return positionToId.get(position);
}
// The RecyclerViewPager and the BottomNavigationView both use menu item IDs to identify pages.
@Override
public long getItemId(int position) {

View File

@ -30,10 +30,6 @@ class MainViewController extends RecyclerView.ViewHolder {
this.frame = frame;
}
public void clearViews() {
frame.removeAllViews();
}
/**
* @see WhatsNewViewBinder
*/