BaseFragment

pull/32/head
Invinciblelee 6 years ago
parent 4479b936ba
commit 9eedabc48d
  1. 43
      app/src/main/java/io/legado/app/base/BaseFragment.kt
  2. 20
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 23
      app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt
  4. 19
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt
  5. 22
      app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt

@ -0,0 +1,43 @@
package io.legado.app.base
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import io.legado.app.utils.setIconColor
abstract class BaseFragment(contentLayoutId: Int = 0) : Fragment(contentLayoutId) {
var supportToolbar: Toolbar? = null
private set
val menuInflater: MenuInflater
get() = SupportMenuInflater(requireContext())
fun setSupportToolbar(toolbar: Toolbar) {
supportToolbar = toolbar
supportToolbar?.let {
it.menu.apply {
setIconColor(requireContext())
onCompatCreateOptionsMenu(this)
}
it.setOnMenuItemClickListener { item ->
onCompatOptionsItemSelected(item)
true
}
}
}
open fun onCompatCreateOptionsMenu(menu: Menu) {
}
open fun onCompatOptionsItemSelected(item: MenuItem) {
}
//TODO
}

@ -1,28 +1,20 @@
package io.legado.app.ui.main.bookshelf package io.legado.app.ui.main.bookshelf
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.view.Menu
import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.setIconColor import io.legado.app.base.BaseFragment
import kotlinx.android.synthetic.main.view_titlebar.* import kotlinx.android.synthetic.main.view_titlebar.*
class BookshelfFragment : Fragment(R.layout.fragment_bookshelf), Toolbar.OnMenuItemClickListener { class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) setSupportToolbar(toolbar)
Log.e("TAG", "BookshelfFragment")
toolbar.inflateMenu(R.menu.bookshelf)
toolbar.menu.setIconColor(App.INSTANCE)
toolbar.setOnMenuItemClickListener(this)
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu) {
return false menuInflater.inflate(R.menu.bookshelf, menu)
} }

@ -1,28 +1,25 @@
package io.legado.app.ui.main.booksource package io.legado.app.ui.main.booksource
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.setIconColor import io.legado.app.base.BaseFragment
import kotlinx.android.synthetic.main.view_titlebar.* import kotlinx.android.synthetic.main.view_titlebar.*
class BookSourceFragment : Fragment(R.layout.fragment_book_source), Toolbar.OnMenuItemClickListener { class BookSourceFragment : BaseFragment(R.layout.fragment_book_source) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) setSupportToolbar(toolbar)
Log.e("TAG", "BookSourceFragment")
toolbar.inflateMenu(R.menu.book_source)
toolbar.menu.setIconColor(App.INSTANCE)
toolbar.setOnMenuItemClickListener(this)
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu) {
return false menuInflater.inflate(R.menu.book_source, menu)
}
override fun onCompatOptionsItemSelected(item: MenuItem) {
} }
} }

@ -1,28 +1,21 @@
package io.legado.app.ui.main.findbook package io.legado.app.ui.main.findbook
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.setIconColor import io.legado.app.base.BaseFragment
import kotlinx.android.synthetic.main.view_titlebar.* import kotlinx.android.synthetic.main.view_titlebar.*
class FindBookFragment : Fragment(R.layout.fragment_find_book), Toolbar.OnMenuItemClickListener { class FindBookFragment : BaseFragment(R.layout.fragment_find_book) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) setSupportToolbar(toolbar)
Log.e("TAG", "FindBookFragment")
toolbar.inflateMenu(R.menu.find_book)
toolbar.menu.setIconColor(App.INSTANCE)
toolbar.setOnMenuItemClickListener(this)
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu) {
return false menuInflater.inflate(R.menu.find_book, menu)
} }
} }

@ -2,27 +2,20 @@ package io.legado.app.ui.main.myconfig
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.view.Menu
import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment
import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigActivity
import io.legado.app.ui.config.ConfigViewModel import io.legado.app.ui.config.ConfigViewModel
import io.legado.app.utils.setIconColor
import kotlinx.android.synthetic.main.fragment_my_config.* import kotlinx.android.synthetic.main.fragment_my_config.*
import kotlinx.android.synthetic.main.view_titlebar.* import kotlinx.android.synthetic.main.view_titlebar.*
class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuItemClickListener { class MyConfigFragment : BaseFragment(R.layout.fragment_my_config) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) setSupportToolbar(toolbar)
Log.e("TAG", "MyConfigFragment")
toolbar.inflateMenu(R.menu.my_config)
toolbar.menu.setIconColor(App.INSTANCE)
toolbar.setOnMenuItemClickListener(this)
tv_theme_config.setOnClickListener { tv_theme_config.setOnClickListener {
val intent = Intent(context, ConfigActivity::class.java) val intent = Intent(context, ConfigActivity::class.java)
intent.putExtra("configType", ConfigViewModel.TYPE_THEME_CONFIG) intent.putExtra("configType", ConfigViewModel.TYPE_THEME_CONFIG)
@ -30,8 +23,7 @@ class MyConfigFragment : Fragment(R.layout.fragment_my_config), Toolbar.OnMenuIt
} }
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu) {
return false menuInflater.inflate(R.menu.my_config, menu)
} }
} }
Loading…
Cancel
Save