When I click add note, the note gets to the top but I had to scroll up to check the note added. It didn't go automatically as it should do. How can I implement this? And there is an error which is being shown up when I tried to add note, i.e. index out of bound. But I solved it by setting itemAnimator to null.
Notes.kt
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.cashmates.daos.NoteDao
import com.example.cashmates.models.Note
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import com.google.firebase.firestore.Query
import kotlinx.android.synthetic.main.activity_notes.*
class Notes : AppCompatActivity() {
private lateinit var noteDao: NoteDao
private lateinit var adapter: NoteAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notes)
fab.setOnClickListener {
val intent3 = Intent(this, CreateNoteActivity::class.java)
startActivity(intent3)
}
setUpRecyclerView()
}
private fun setUpRecyclerView() {
noteDao = NoteDao()
val notesCollections = noteDao.noteCollections
val query = notesCollections.orderBy("time", Query.Direction.DESCENDING)
val recyclerViewOptions = FirestoreRecyclerOptions.Builder<Note>().setQuery(query, Note::class.java).build()
adapter= NoteAdapter(recyclerViewOptions)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.itemAnimator = null
}
override fun onStart() {
super.onStart()
adapter.startListening()
}
override fun onStop() {
super.onStop()
adapter.stopListening()
}
}
NoteAdapter.kt
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.cashmates.models.Note
import com.firebase.ui.firestore.FirestoreRecyclerAdapter
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import java.text.SimpleDateFormat
class NoteAdapter(options: FirestoreRecyclerOptions<Note>) : FirestoreRecyclerAdapter<Note, NoteAdapter.NoteViewHolder>(
options
) {
class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val noteText: TextView = itemView.findViewById(R.id.notes)
val date: TextView = itemView.findViewById(R.id.dates)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
return NoteViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_note,parent,false))
}
@SuppressLint("SimpleDateFormat")
override fun onBindViewHolder(holder: NoteViewHolder, position: Int, model: Note) {
holder.noteText.text = model.detail
val simpleDateFormat = SimpleDateFormat("dd/MM/yyyy")
val dateString = simpleDateFormat.format(model.time)
holder.date.text = dateString.toString()
}
}
I am not able to automatically come to the top of the linear layout, I had to scroll the linear layout to go to the top.