I'm trying to dynamically set the visibility of a view which is in the same fragment as the recyclerview to invisible when the recyclerview becomes empty.The problem is that i'm making the deletion of an item from the recyclerview inside the adapter while the visibility must be set inside the fragment.I also implemented "swipe to delete" recyclerview items, but that was inside the fragment with the rv and with the views that need to become invisible and it works fine.
Deletion from Adapter:
holder.shoppingCartDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int holderPosition = holder.getAdapterPosition();
removeItem(holderPosition, v.getContext());
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(currentItem);
}
});
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
// SET VISIBILITY
}
notifyItemRemoved(position);
}
Deletion from fragment-the visibility is changed after i delete the last item:
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(shoppingCartList.get(position));
shoppingCartList.remove(position);
shopAdapter.notifyDataSetChanged();
verifyIfRecyclerViewIsEmpty(shopAdapter, recyclerView);
}
}).attachToRecyclerView(recyclerView);
Where verifyIftheRecyclerViewIsEmpty(shopAdapter, recyclerView) is the method for the visibility:
private void verifyIfRecyclerViewIsEmpty(RecyclerView.Adapter adapter, RecyclerView recyclerView) {
if (adapter.getItemCount() == 0) {
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
} else {
constraintLayout.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.VISIBLE);
textView.setVisibility(View.GONE);
btnAdd.setVisibility(View.GONE);
}
}