Reputation: 383
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);
}
}
Upvotes: 0
Views: 2629
Reputation: 4035
Create an interface
in new file:
public interface onShoppingItemRemovedListener{
void onRecyclerViewEmpty();
}
In your adapter
do this:
class YourAdapter extends .........{
//add this
private onShoppingItemRemovedListener listener;
.....
.....
//in the constructor
public YourAdapter (onShoppingItemRemovedListener listener,..............){
this.listener = listener;
......
......
......
}
//when you delete item do this
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();
//at this point the recyclerview is empty, so notify the fragment
listener.onRecyclerViewEmpty();
}
}
}
Finally in the fragment
implement the interface
and pass to the adapter
:
class YourFragement extends ........ implements onShoppingItemRemovedListener{
//now when you initialize the adapter pass the listener like this
adapter = new YourAdapter(this,...........);
.....
.....
//override this
@Override
public void onRecyclerViewEmpty(){
//this is triggered when the recycler view is completely empty
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
}
}
Upvotes: 1
Reputation: 5241
OnItemListener
make sure you implements it in your Fragment, and call verifyIfRecyclerViewIsEmpty
inside method onEmpty
public interface OnItemListener {
void onEmpty();
}
private OnItemListener mOnItemListener;
public void setItemListener(IOnItemClickedListener listener) {
mOnItemListener = listener;
}
removeItem
in your adapterprivate 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();
if (mOnItemListener != null) {
mOnItemListener.onEmpty()
}
}
notifyItemRemoved(position);
}
Upvotes: 0
Reputation: 101
Try to use RecyclerViewEmptySupport as mentioned in this solution:
https://stackoverflow.com/a/30415582/5434762
Upvotes: 0