From 9eedabc48df0a9b293a84589b5e7eafa654df2a2 Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Wed, 5 Jun 2019 17:07:59 +0800 Subject: [PATCH] BaseFragment --- .../java/io/legado/app/base/BaseFragment.kt | 43 +++++++++++++++++++ .../ui/main/bookshelf/BookshelfFragment.kt | 20 +++------ .../ui/main/booksource/BookSourceFragment.kt | 23 +++++----- .../app/ui/main/findbook/FindBookFragment.kt | 19 +++----- .../app/ui/main/myconfig/MyConfigFragment.kt | 22 +++------- 5 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/io/legado/app/base/BaseFragment.kt diff --git a/app/src/main/java/io/legado/app/base/BaseFragment.kt b/app/src/main/java/io/legado/app/base/BaseFragment.kt new file mode 100644 index 000000000..651bfb830 --- /dev/null +++ b/app/src/main/java/io/legado/app/base/BaseFragment.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 +} diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index fa5963220..a50d9f32f 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -1,28 +1,20 @@ package io.legado.app.ui.main.bookshelf import android.os.Bundle -import android.util.Log -import android.view.MenuItem +import android.view.Menu 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.utils.setIconColor +import io.legado.app.base.BaseFragment 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?) { - super.onViewCreated(view, savedInstanceState) - Log.e("TAG", "BookshelfFragment") - toolbar.inflateMenu(R.menu.bookshelf) - toolbar.menu.setIconColor(App.INSTANCE) - toolbar.setOnMenuItemClickListener(this) + setSupportToolbar(toolbar) } - override fun onMenuItemClick(item: MenuItem?): Boolean { - return false + override fun onCompatCreateOptionsMenu(menu: Menu) { + menuInflater.inflate(R.menu.bookshelf, menu) } diff --git a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt index e20872cd6..616becb0e 100644 --- a/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/booksource/BookSourceFragment.kt @@ -1,28 +1,25 @@ package io.legado.app.ui.main.booksource import android.os.Bundle -import android.util.Log +import android.view.Menu import android.view.MenuItem 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.utils.setIconColor +import io.legado.app.base.BaseFragment 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?) { - super.onViewCreated(view, savedInstanceState) - Log.e("TAG", "BookSourceFragment") - toolbar.inflateMenu(R.menu.book_source) - toolbar.menu.setIconColor(App.INSTANCE) - toolbar.setOnMenuItemClickListener(this) + setSupportToolbar(toolbar) } - override fun onMenuItemClick(item: MenuItem?): Boolean { - return false + override fun onCompatCreateOptionsMenu(menu: Menu) { + menuInflater.inflate(R.menu.book_source, menu) + } + + override fun onCompatOptionsItemSelected(item: MenuItem) { + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt index 8a9da36b5..f3a412af5 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookFragment.kt @@ -1,28 +1,21 @@ package io.legado.app.ui.main.findbook import android.os.Bundle -import android.util.Log +import android.view.Menu import android.view.MenuItem 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.utils.setIconColor +import io.legado.app.base.BaseFragment 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?) { - super.onViewCreated(view, savedInstanceState) - Log.e("TAG", "FindBookFragment") - toolbar.inflateMenu(R.menu.find_book) - toolbar.menu.setIconColor(App.INSTANCE) - toolbar.setOnMenuItemClickListener(this) + setSupportToolbar(toolbar) } - override fun onMenuItemClick(item: MenuItem?): Boolean { - return false + override fun onCompatCreateOptionsMenu(menu: Menu) { + menuInflater.inflate(R.menu.find_book, menu) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt index b7d27e01c..4852bf066 100644 --- a/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/myconfig/MyConfigFragment.kt @@ -2,27 +2,20 @@ package io.legado.app.ui.main.myconfig import android.content.Intent import android.os.Bundle -import android.util.Log -import android.view.MenuItem +import android.view.Menu 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.base.BaseFragment import io.legado.app.ui.config.ConfigActivity 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.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?) { - super.onViewCreated(view, savedInstanceState) - Log.e("TAG", "MyConfigFragment") - toolbar.inflateMenu(R.menu.my_config) - toolbar.menu.setIconColor(App.INSTANCE) - toolbar.setOnMenuItemClickListener(this) + setSupportToolbar(toolbar) + tv_theme_config.setOnClickListener { val intent = Intent(context, ConfigActivity::class.java) 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 { - return false + override fun onCompatCreateOptionsMenu(menu: Menu) { + menuInflater.inflate(R.menu.my_config, menu) } - } \ No newline at end of file