Fixed NPE in EmptyViewHandler

This commit is contained in:
ByteHamster 2019-11-25 16:23:56 +01:00
parent f2771f953f
commit 1dd5c19eea
2 changed files with 35 additions and 11 deletions

View File

@ -89,7 +89,6 @@ public class SubscriptionFragment extends Fragment {
subscriptionGridLayout.setNumColumns(prefs.getInt(PREF_NUM_COLUMNS, 3));
registerForContextMenu(subscriptionGridLayout);
subscriptionAddButton = root.findViewById(R.id.subscriptions_add);
setupEmptyView();
return root;
}
@ -154,6 +153,7 @@ public class SubscriptionFragment extends Fragment {
subscriptionAdapter = new SubscriptionsAdapter((MainActivity) getActivity(), itemAccess);
subscriptionGridLayout.setAdapter(subscriptionAdapter);
subscriptionGridLayout.setOnItemClickListener(subscriptionAdapter);
setupEmptyView();
subscriptionAddButton.setOnClickListener(view -> {
if (getActivity() instanceof MainActivity) {

View File

@ -1,8 +1,10 @@
package de.danoeh.antennapod.view;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import androidx.annotation.AttrRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
@ -17,8 +19,9 @@ import de.danoeh.antennapod.R;
public class EmptyViewHandler {
private boolean layoutAdded = false;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private View list;
private ListAdapter listAdapter;
private RecyclerView.Adapter recyclerAdapter;
private final Context context;
private final View emptyView;
@ -60,7 +63,9 @@ public class EmptyViewHandler {
}
addToParentView(listView);
layoutAdded = true;
this.list = listView;
listView.setEmptyView(emptyView);
updateAdapter(listView.getAdapter());
}
public void attachToRecyclerView(RecyclerView recyclerView) {
@ -69,7 +74,7 @@ public class EmptyViewHandler {
}
addToParentView(recyclerView);
layoutAdded = true;
this.recyclerView = recyclerView;
this.list = recyclerView;
updateAdapter(recyclerView.getAdapter());
}
@ -85,16 +90,27 @@ public class EmptyViewHandler {
}
public void updateAdapter(RecyclerView.Adapter adapter) {
if (this.adapter != null) {
this.adapter.unregisterAdapterDataObserver(adapterObserver);
if (this.recyclerAdapter != null) {
this.recyclerAdapter.unregisterAdapterDataObserver(adapterObserver);
}
this.adapter = adapter;
this.recyclerAdapter = adapter;
if (adapter != null) {
adapter.registerAdapterDataObserver(adapterObserver);
}
updateVisibility();
}
private void updateAdapter(ListAdapter adapter) {
if (this.listAdapter != null) {
this.listAdapter.unregisterDataSetObserver(listAdapterObserver);
}
this.listAdapter = adapter;
if (adapter != null) {
adapter.registerDataSetObserver(listAdapterObserver);
}
updateVisibility();
}
private final SimpleAdapterDataObserver adapterObserver = new SimpleAdapterDataObserver() {
@Override
public void anythingChanged() {
@ -102,14 +118,22 @@ public class EmptyViewHandler {
}
};
private final DataSetObserver listAdapterObserver = new DataSetObserver() {
public void onChanged() {
updateVisibility();
}
};
public void updateVisibility() {
boolean empty;
if (adapter == null) {
empty = true;
if (recyclerAdapter != null) {
empty = recyclerAdapter.getItemCount() == 0;
} else if (listAdapter != null) {
empty = listAdapter.isEmpty();
} else {
empty = adapter.getItemCount() == 0;
empty = true;
}
recyclerView.setVisibility(empty ? View.GONE : View.VISIBLE);
list.setVisibility(empty ? View.GONE : View.VISIBLE);
emptyView.setVisibility(empty ? View.VISIBLE : View.GONE);
}
}