Tiberiu Paduraru
Tiberiu Paduraru

Reputation: 383

How to set the visibility of a view after recyclerview becomes empty

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

Answers (3)

Hasan Bou Taam
Hasan Bou Taam

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

Công Hải
Công Hải

Reputation: 5241

  1. Create Interface OnItemListener make sure you implements it in your Fragment, and call verifyIfRecyclerViewIsEmpty inside method onEmpty
public interface OnItemListener {

    void onEmpty();
}
  1. In your Adapter create listener
    private OnItemListener mOnItemListener;

    public void setItemListener(IOnItemClickedListener listener) {
        mOnItemListener = listener;
    }
  1. Update method removeItem in your adapter
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();
        if (mOnItemListener != null) {
            mOnItemListener.onEmpty()
        }
    }
    notifyItemRemoved(position);
}

Upvotes: 0

Salem Kabbani
Salem Kabbani

Reputation: 101

Try to use RecyclerViewEmptySupport as mentioned in this solution:

https://stackoverflow.com/a/30415582/5434762

Upvotes: 0

Related Questions