0

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);
    }
}
Tiberiu Paduraru
  • 313
  • 3
  • 17
  • What's exactly your problem in your code? – Zain May 26 '20 at 00:29
  • @Zain I'm trying to set the visibility of the views inside the adapter,because there is where i make the deletion,but the problem is that the views are in a fragment and not in the adapter. – Tiberiu Paduraru May 26 '20 at 01:41

3 Answers3

1

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);


}


}
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
0

Try to use RecyclerViewEmptySupport as mentioned in this solution:

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

Salem Kabbani
  • 101
  • 1
  • 7
0
  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);
}
Công Hải
  • 4,961
  • 3
  • 14
  • 20
  • This doesn't seem to work.I followed your steps:i created the interface in the adapter,the fragment implements the interface and inside the method onEmpty() i added the verification.I declared private OnItemListener mOnItemListener and created the method setItemListener() like folowing: public void setItemListener(AdapterView.OnItemClickListener listener) { mOnItemListener = (OnItemListener) listener; } but the method is not used anywhere. – Tiberiu Paduraru May 26 '20 at 02:16
  • 1
    You should set it when you init your adapter inside fragment, call adapter. setItemListener(this) – Công Hải May 26 '20 at 02:28