0

I have been trying to find information about creating advanced RecyclerView adapters.

I need two versions of adapters for my application.

  1. The first one is similar to the Google Keep checklist. When you are adding a new item to the list, it adds a new cell at the end of the list.

ex: create a product list or wishlist.

I can already create an adapter with a multiple ViewTypes, but I can't find a solution to my problem (adding a new cell when adding a new item to the list)

  1. second one is I need to have a list with multiple types of layouts. ex: Notes, Birthdays, Important, etc. with I separators in between them. And when I delete all Birthdays from the list. How can I delete Birthday separator?
msfvenom
  • 17
  • 6

1 Answers1

0

To answer your question:

Adding new cell at the bottom of the list when adding an new item

Setup listener on a view on item layout and add new item to the end of the list

ex: For Google Keep example implement code to add new item to the end of the list inside edittext view TextChangeListener's onTextChanged method on the item layout.

inner class TaskViewHolder(mView: View) : RecyclerView.ViewHolder(mView) {

        val etTask: EditText = view.findViewById(R.id.et_task)

        init {
            etTask.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(p0: Editable?) {
                }

                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    // add new item to the end of the list here

                }

            })
        }
    }

Removing the separator when all the items in a group was deleted

You could check to see if the list items in the group is empty and set the visibility of the separator to GONE

ex:

// here birthdays is ArrayList<Birthday>
if(birthdays.isEmpty()){
     separator.visibility = View.GONE
}
user158
  • 12,852
  • 7
  • 62
  • 94