I have a default layout that contains a bunch of blank cards in the RecyclerView list, basically a welcome screen for the user to show them how the cards look. The user then launches the input screen for some data and clicks the "Save" button to save the data in CardView. As soon as the user clicks the “Save” button, the layout should change from the default layout with empty CardViews to a new single CardView that contains user data. Later, if the user deletes all his cards, then the view should return to empty Card views by default.
I am trying to set the int Adapter code to onCreateViewHolder because getItemCount () will already have a positive value for the default value (since the RecyclerView list will already contain 4 or 5 empty cards) that conflict later with the same amount getItemCount (), as only user will create 4 or 5 cards. Any ideas on how to set the default layout and then switch to a new layout, which can then revert to the default layout if the list is empty from the user-created CardViews?
Below is my unsuccessful attempt to test for two layouts in the adapter. I realized that this would not work, because the default layout never had an ItemCount with a zero value, since there are already 4 or 5 empty CardViews:
... public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> { private List<ContactInfo> contactList; public ContactAdapter(List<ContactInfo> contactList) { this.contactList = contactList; } @Override public int getItemCount() { return contactList.size(); } @Override public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { if(contactList.size()== 0) { View itemView = LayoutInflater. from(viewGroup.getContext()). inflate(R.layout.defaultcard_layout, viewGroup, false); return new ContactViewHolder(itemView); } else { View itemView = LayoutInflater. from(viewGroup.getContext()). inflate(R.layout.singlecard_layout, viewGroup, false); return new ContactViewHolder(itemView); } }
adapter code and removeItem changed:
... private LayoutInflater mLayoutInflater; private List<Contact> mContacts; private OnItemTapListener mOnItemTapListener; public ListContactsAdapter(Context context, List<Contact> contacts) { Context mContext; mContext = context; mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mContacts = contacts; } public void removeItem(Contact contact, int position) { mContacts.remove(contact); if (mContacts.size()==0) {
source share