properly init the viewpager with wanted note

This commit is contained in:
tibbi 2016-11-26 20:40:29 +01:00
parent 3070bba0eb
commit 6e1a530da1
1 changed files with 15 additions and 2 deletions

View File

@ -45,11 +45,19 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
fun initViewPager() { fun initViewPager() {
mNotes = mDb.getNotes().sortedBy(Note::title) mNotes = mDb.getNotes().sortedBy(Note::title)
mCurrentNote = mNotes[0] mCurrentNote = mNotes[0]
var itemIndex = 0
for (i in 0..mNotes.count() - 1) {
if (mNotes[i].id == config.currentNoteId) {
mCurrentNote = mNotes[i]
itemIndex = i
break
}
}
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes) mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes)
view_pager.apply { view_pager.apply {
adapter = mAdapter adapter = mAdapter
currentItem = 0 currentItem = itemIndex
addOnPageChangeListener(this@MainActivity) addOnPageChangeListener(this@MainActivity)
} }
} }
@ -129,14 +137,19 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun updateSelectedNote(id: Int) { private fun updateSelectedNote(id: Int) {
config.currentNoteId = id config.currentNoteId = id
for (i in 0..mNotes.count() - 1) {
view_pager.currentItem = i
mCurrentNote = mNotes[i]
}
} }
fun displayNewNoteDialog() { fun displayNewNoteDialog() {
NewNoteDialog(this, mDb) { NewNoteDialog(this, mDb) {
val newNote = Note(0, it, "", TYPE_NOTE) val newNote = Note(0, it, "", TYPE_NOTE)
val id = mDb.insertNote(newNote) val id = mDb.insertNote(newNote)
updateSelectedNote(id)
mNotes = mDb.getNotes() mNotes = mDb.getNotes()
updateSelectedNote(id)
invalidateOptionsMenu() invalidateOptionsMenu()
initViewPager() initViewPager()
} }