Let the adapter handle the decorator

This should remove the hackiness of decorator being added several times.
This commit is contained in:
mvp76 2017-03-01 14:34:31 +01:00 committed by Peter Serwylo
parent 48b21626e4
commit fd22ee01e0
2 changed files with 16 additions and 18 deletions

View File

@ -28,8 +28,6 @@ class WhatsNewViewBinder implements LoaderManager.LoaderCallbacks<Cursor> {
private final WhatsNewAdapter whatsNewAdapter; private final WhatsNewAdapter whatsNewAdapter;
private final AppCompatActivity activity; private final AppCompatActivity activity;
private static RecyclerView.ItemDecoration appListDecorator;
WhatsNewViewBinder(final AppCompatActivity activity, FrameLayout parent) { WhatsNewViewBinder(final AppCompatActivity activity, FrameLayout parent) {
this.activity = activity; this.activity = activity;
@ -45,20 +43,6 @@ class WhatsNewViewBinder implements LoaderManager.LoaderCallbacks<Cursor> {
appList.setLayoutManager(layoutManager); appList.setLayoutManager(layoutManager);
appList.setAdapter(whatsNewAdapter); appList.setAdapter(whatsNewAdapter);
// This is a bit hacky, but for some reason even though we are inflating the main_tab_whats_new
// layout above, the app_list RecyclerView seems to remember that it has decorations from before.
// If we blindly call addItemDecoration here without first removing the existing one, it will
// double up on all of the paddings the second time we view it. The third time it will triple up
// on the paddings, etc. In addition, the API doesn't allow us to "clearAllDecorators()". Instead
// we need to hold onto the reference to the one we added in order to remove it.
if (appListDecorator == null) {
appListDecorator = new WhatsNewAdapter.ItemDecorator(activity);
} else {
appList.removeItemDecoration(appListDecorator);
}
appList.addItemDecoration(appListDecorator);
final SwipeRefreshLayout swipeToRefresh = (SwipeRefreshLayout) whatsNewView.findViewById(R.id.swipe_to_refresh); final SwipeRefreshLayout swipeToRefresh = (SwipeRefreshLayout) whatsNewView.findViewById(R.id.swipe_to_refresh);
swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override @Override

View File

@ -18,9 +18,23 @@ public class WhatsNewAdapter extends RecyclerView.Adapter<AppCardController> {
private Cursor cursor; private Cursor cursor;
private final Activity activity; private final Activity activity;
private final RecyclerView.ItemDecoration appListDecorator;
public WhatsNewAdapter(Activity activity) { public WhatsNewAdapter(Activity activity) {
this.activity = activity; this.activity = activity;
appListDecorator = new WhatsNewAdapter.ItemDecorator(activity);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
recyclerView.addItemDecoration(appListDecorator);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
recyclerView.removeItemDecoration(appListDecorator);
super.onDetachedFromRecyclerView(recyclerView);
} }
@Override @Override
@ -92,10 +106,10 @@ public class WhatsNewAdapter extends RecyclerView.Adapter<AppCardController> {
* @see org.fdroid.fdroid.R.dimen#whats_new__padding__app_card__horizontal * @see org.fdroid.fdroid.R.dimen#whats_new__padding__app_card__horizontal
* @see org.fdroid.fdroid.R.dimen#whats_new__padding__app_card__vertical * @see org.fdroid.fdroid.R.dimen#whats_new__padding__app_card__vertical
*/ */
public static class ItemDecorator extends RecyclerView.ItemDecoration { private class ItemDecorator extends RecyclerView.ItemDecoration {
private final Context context; private final Context context;
public ItemDecorator(Context context) { ItemDecorator(Context context) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
} }