diff --git a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt index 8f49e6cd7..c063e69db 100644 --- a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt @@ -27,7 +27,7 @@ import java.util.* class ReadRecordActivity : BaseActivity() { - lateinit var adapter: RecordAdapter + private val adapter by lazy { RecordAdapter(this) } private var sortMode = 0 override val binding by viewBinding(ActivityReadRecordBinding::inflate) @@ -58,7 +58,6 @@ class ReadRecordActivity : BaseActivity() { private fun initView() = binding.run { readRecord.tvBookName.setText(R.string.all_read_time) - adapter = RecordAdapter(this@ReadRecordActivity) recyclerView.adapter = adapter readRecord.tvRemove.setOnClickListener { alert(R.string.delete, R.string.sure_del) { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index c6a834a39..dce1044e9 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -58,7 +58,7 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList private val binding by viewBinding(DialogRecyclerViewBinding::bind) private val viewModel by viewModels() - private lateinit var adapter: SourcesAdapter + private val adapter by lazy { SourcesAdapter(requireContext()) } override fun onStart() { super.onStart() @@ -89,7 +89,6 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList binding.toolBar.setTitle(R.string.import_book_source) binding.rotateLoading.show() initMenu() - adapter = SourcesAdapter(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.adapter = adapter binding.tvCancel.visible() diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt index aec7a3762..70e7f3e53 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt @@ -51,7 +51,7 @@ class ImportReplaceRuleDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickLis private val binding by viewBinding(DialogRecyclerViewBinding::bind) private val viewModel by viewModels() - lateinit var adapter: SourcesAdapter + private val adapter by lazy { SourcesAdapter(requireContext()) } override fun onStart() { super.onStart() @@ -82,7 +82,6 @@ class ImportReplaceRuleDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickLis binding.toolBar.setTitle(R.string.import_replace_rule) binding.rotateLoading.show() initMenu() - adapter = SourcesAdapter(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.adapter = adapter binding.tvCancel.visible() diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt index b6d41b20a..d4be8117b 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -55,7 +55,7 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe private val binding by viewBinding(DialogRecyclerViewBinding::bind) private val viewModel by viewModels() - lateinit var adapter: SourcesAdapter + private val adapter by lazy { SourcesAdapter(requireContext()) } override fun onStart() { super.onStart() @@ -86,7 +86,6 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe binding.toolBar.setTitle(R.string.import_rss_source) binding.rotateLoading.show() initMenu() - adapter = SourcesAdapter(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.adapter = adapter binding.tvCancel.visible() diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index 83d7584af..08d947c71 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -45,7 +45,7 @@ class ArrangeBookActivity : VMBaseActivity = arrayListOf() private val groupRequestCode = 22 private val addToGroupRequestCode = 34 - private lateinit var adapter: ArrangeBookAdapter + private val adapter by lazy { ArrangeBookAdapter(this, this) } private var booksFlowJob: Job? = null private var menu: Menu? = null private var groupId: Long = -1 @@ -90,7 +90,6 @@ class ArrangeBookActivity : VMBaseActivity() private val exportBookPathKey = "exportBookPath" private val exportTypes = arrayListOf("txt", "epub") - lateinit var adapter: CacheAdapter + private val adapter by lazy { CacheAdapter(this, this) } private var booksFlowJob: Job? = null private var menu: Menu? = null private var exportPosition = -1 @@ -147,7 +147,6 @@ class CacheActivity : VMBaseActivity() private fun initRecyclerView() { binding.recyclerView.layoutManager = LinearLayoutManager(this) - adapter = CacheAdapter(this, this) binding.recyclerView.adapter = adapter } diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt index 2d3084173..4d03a5753 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt @@ -40,7 +40,7 @@ class ChangeCoverDialog : BaseDialogFragment(), private val binding by viewBinding(DialogChangeCoverBinding::bind) private var callBack: CallBack? = null private val viewModel: ChangeCoverViewModel by viewModels() - lateinit var adapter: CoverAdapter + private val adapter by lazy { CoverAdapter(requireContext(), this) } override fun onStart() { super.onStart() @@ -73,7 +73,6 @@ class ChangeCoverDialog : BaseDialogFragment(), private fun initView() { binding.recyclerView.layoutManager = GridLayoutManager(requireContext(), 3) - adapter = CoverAdapter(requireContext(), this) binding.recyclerView.adapter = adapter viewModel.loadDbSearchBook() } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index c79096391..2c2345145 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -51,7 +51,7 @@ class ChangeSourceDialog : BaseDialogFragment(), private val groups = linkedSetOf() private var callBack: CallBack? = null private val viewModel: ChangeSourceViewModel by viewModels() - lateinit var adapter: ChangeSourceAdapter + private val adapter by lazy { ChangeSourceAdapter(requireContext(), viewModel, this) } override fun onStart() { super.onStart() @@ -97,7 +97,6 @@ class ChangeSourceDialog : BaseDialogFragment(), } private fun initRecyclerView() { - adapter = ChangeSourceAdapter(requireContext(), viewModel, this) binding.recyclerView.layoutManager = LinearLayoutManager(context) binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) binding.recyclerView.adapter = adapter diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt index 76d2b22f8..085f4cecd 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt @@ -20,8 +20,8 @@ class ExploreShowActivity : VMBaseActivity() - private lateinit var adapter: ExploreShowAdapter - private lateinit var loadMoreView: LoadMoreView + private val adapter by lazy { ExploreShowAdapter(this, this) } + private val loadMoreView by lazy { LoadMoreView(this) } private var isLoading = true override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -35,10 +35,8 @@ class ExploreShowActivity : VMBaseActivity() - private lateinit var adapter: ImportBookAdapter + private val adapter by lazy { ImportBookAdapter(this, this) } private var sdPath = FileUtils.getSdCardPath() private var path = sdPath private val selectFolder = registerForActivityResult(HandleFileContract()) { uri -> @@ -111,7 +111,6 @@ class ImportBookActivity : VMBaseActivity diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 4df4eb0ac..774855cb8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -39,7 +39,7 @@ class BgTextConfigDialog : BaseDialogFragment() { private val binding by viewBinding(DialogReadBgTextBinding::bind) private val configFileName = "readConfig.zip" - private lateinit var adapter: BgAdapter + private val adapter by lazy { BgAdapter(requireContext(), secondaryTextColor) } private var primaryTextColor = 0 private var secondaryTextColor = 0 private val importFormNet = "网络导入" @@ -111,7 +111,6 @@ class BgTextConfigDialog : BaseDialogFragment() { ivExport.setColorFilter(primaryTextColor) ivDelete.setColorFilter(primaryTextColor) tvBgImage.setTextColor(primaryTextColor) - adapter = BgAdapter(requireContext(), secondaryTextColor) recyclerView.adapter = adapter adapter.addHeaderView { ItemBgImageBinding.inflate(layoutInflater, it, false).apply { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index e587879c3..5769f9ed7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -35,10 +35,11 @@ import splitties.init.appCtx class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + private val binding by viewBinding(DialogRecyclerViewBinding::bind) - private val ttsUrlKey = "ttsUrlKey" - lateinit var adapter: Adapter private val viewModel: SpeakEngineViewModel by viewModels() + private val ttsUrlKey = "ttsUrlKey" + private val adapter by lazy { Adapter(requireContext()) } private var engineId = appCtx.getPrefLong(PreferKey.speakEngine) private val importDocResult = registerForActivityResult(HandleFileContract()) { it?.let { @@ -84,7 +85,6 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener toolBar.setTitle(R.string.speak_engine) ATH.applyEdgeEffectColor(recyclerView) recyclerView.layoutManager = LinearLayoutManager(requireContext()) - adapter = Adapter(requireContext()) recyclerView.adapter = adapter tvFooterLeft.setText(R.string.system_tts) tvFooterLeft.visible() diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index 5895e2863..ae8167cc6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -36,12 +36,13 @@ import kotlinx.coroutines.launch import java.util.* class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + private val importTocRuleKey = "tocRuleUrl" - private lateinit var adapter: TocRegexAdapter - var selectedName: String? = null - private var durRegex: String? = null private val viewModel: TocRegexViewModel by viewModels() private val binding by viewBinding(DialogTocRegexBinding::bind) + private val adapter by lazy { TocRegexAdapter(requireContext()) } + var selectedName: String? = null + private var durRegex: String? = null override fun onStart() { super.onStart() @@ -69,7 +70,6 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { } private fun initView() = binding.run { - adapter = TocRegexAdapter(requireContext()) recyclerView.addItemDecoration(VerticalDivider(requireContext())) recyclerView.adapter = adapter val itemTouchCallback = ItemTouchCallback(adapter) diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 3572a7a79..04da90cc4 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -42,11 +42,13 @@ class SearchActivity : VMBaseActivity() - lateinit var adapter: SearchAdapter - private lateinit var bookAdapter: BookAdapter - private lateinit var historyKeyAdapter: HistoryKeyAdapter - private lateinit var loadMoreView: LoadMoreView - private lateinit var searchView: SearchView + private val adapter by lazy { SearchAdapter(this, this) } + private val bookAdapter by lazy { BookAdapter(this, this) } + private val historyKeyAdapter by lazy { HistoryKeyAdapter(this, this) } + private val loadMoreView by lazy { LoadMoreView(this) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private var historyFlowJob: Job? = null private var booksFlowJob: Job? = null private var menu: Menu? = null @@ -55,7 +57,6 @@ class SearchActivity : VMBaseActivity() - lateinit var adapter: SearchContentAdapter - private lateinit var mLayoutManager: UpLinearLayoutManager - private lateinit var searchView: SearchView + private val adapter by lazy { SearchContentAdapter(this, this) } + private val mLayoutManager by lazy { UpLinearLayoutManager(this) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private var searchResultCounts = 0 private var durChapterIndex = 0 private var searchResultList: MutableList = mutableListOf() override fun onActivityCreated(savedInstanceState: Bundle?) { - searchView = binding.titleBar.findViewById(R.id.search_view) val bbg = bottomBackground val btc = getPrimaryTextColor(ColorUtils.isColorLight(bbg)) binding.llSearchBaseInfo.setBackgroundColor(bbg) @@ -81,8 +82,6 @@ class SearchContentActivity : } private fun initRecyclerView() { - adapter = SearchContentAdapter(this, this) - mLayoutManager = UpLinearLayoutManager(this) binding.recyclerView.layoutManager = mLayoutManager binding.recyclerView.addItemDecoration(VerticalDivider(this)) binding.recyclerView.adapter = adapter diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt index e926b11e9..5f320ba46 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt @@ -24,7 +24,7 @@ class BookSourceDebugActivity : VMBaseActivity() - private lateinit var adapter: BookSourceDebugAdapter + private val adapter by lazy { BookSourceDebugAdapter(this) } private val searchView: SearchView by lazy { binding.titleBar.findViewById(R.id.search_view) } @@ -52,7 +52,6 @@ class BookSourceDebugActivity : VMBaseActivity() private val importRecordKey = "bookSourceRecordKey" - private lateinit var adapter: BookSourceAdapter - private lateinit var searchView: SearchView + private val adapter by lazy { BookSourceAdapter(this, this) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private var sourceFlowJob: Job? = null private val groups = linkedSetOf() private var groupMenu: SubMenu? = null @@ -86,7 +88,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.fragment_bookmark TocViewModel.BookmarkCallBack { override val viewModel by activityViewModels() private val binding by viewBinding(FragmentBookmarkBinding::bind) - private lateinit var adapter: BookmarkAdapter + private val adapter by lazy { BookmarkAdapter(requireContext(), this) } private var bookmarkFlowJob: Job? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { @@ -37,7 +37,6 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark private fun initRecyclerView() { ATH.applyEdgeEffectColor(binding.recyclerView) - adapter = BookmarkAdapter(requireContext(), this) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) binding.recyclerView.adapter = adapter diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index fdf1a2183..34d386dc1 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -34,7 +34,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt TocViewModel.ChapterListCallBack { override val viewModel by activityViewModels() private val binding by viewBinding(FragmentChapterListBinding::bind) - lateinit var adapter: ChapterListAdapter + private val adapter by lazy { ChapterListAdapter(requireContext(), this) } private var durChapterIndex = 0 private lateinit var mLayoutManager: UpLinearLayoutManager private var tocFlowJob: Job? = null @@ -56,7 +56,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt } private fun initRecyclerView() { - adapter = ChapterListAdapter(requireContext(), this) mLayoutManager = UpLinearLayoutManager(requireContext()) binding.recyclerView.layoutManager = mLayoutManager binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt b/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt index b4275a7ae..c6e7b19cd 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeListDialog.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.config +import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.MenuItem @@ -22,7 +23,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding class ThemeListDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { private val binding by viewBinding(DialogRecyclerViewBinding::bind) - private lateinit var adapter: Adapter + private val adapter by lazy { Adapter(requireContext()) } override fun onStart() { super.onStart() @@ -47,7 +48,6 @@ class ThemeListDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { } private fun initView() = binding.run { - adapter = Adapter() recyclerView.layoutManager = LinearLayoutManager(requireContext()) recyclerView.addItemDecoration(VerticalDivider(requireContext())) recyclerView.adapter = adapter @@ -93,8 +93,8 @@ class ThemeListDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { requireContext().share(json, "主题分享") } - inner class Adapter : - RecyclerAdapter(requireContext()) { + inner class Adapter(context: Context) : + RecyclerAdapter(context) { override fun getViewBinding(parent: ViewGroup): ItemThemeConfigBinding { return ItemThemeConfigBinding.inflate(inflater, parent, false) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt index ea279792a..96b2f85d7 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt @@ -37,8 +37,10 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), SearchView.OnQueryTextListener { private val binding by viewBinding(FragmentBookshelfBinding::bind) - private lateinit var adapter: FragmentStatePagerAdapter - private lateinit var tabLayout: TabLayout + private val adapter by lazy { TabFragmentPageAdapter(childFragmentManager) } + private val tabLayout: TabLayout by lazy { + binding.titleBar.findViewById(R.id.tab_layout) + } private val bookGroups = mutableListOf() private val fragmentMap = hashMapOf() @@ -51,7 +53,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), } override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - tabLayout = binding.titleBar.findViewById(R.id.tab_layout) setSupportToolbar(binding.titleBar.toolbar) initView() initBookGroupData() @@ -67,7 +68,6 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf), tabLayout.setSelectedTabIndicatorColor(requireContext().accentColor) tabLayout.setupWithViewPager(binding.viewPagerBookshelf) binding.viewPagerBookshelf.offscreenPageLimit = 1 - adapter = TabFragmentPageAdapter(childFragmentManager) binding.viewPagerBookshelf.adapter = adapter } diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index 6d797a30d..7e4cd3841 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -38,15 +38,16 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo ExploreAdapter.CallBack { override val viewModel by viewModels() private val binding by viewBinding(FragmentExploreBinding::bind) - private lateinit var adapter: ExploreAdapter - private lateinit var linearLayoutManager: LinearLayoutManager - private lateinit var searchView: SearchView + private val adapter by lazy { ExploreAdapter(requireContext(), lifecycleScope, this) } + private val linearLayoutManager by lazy { LinearLayoutManager(context) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private val groups = linkedSetOf() private var exploreFlowJob: Job? = null private var groupsMenu: SubMenu? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - searchView = binding.titleBar.findViewById(R.id.search_view) setSupportToolbar(binding.titleBar.toolbar) initSearchView() initRecyclerView() @@ -86,9 +87,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo private fun initRecyclerView() { ATH.applyEdgeEffectColor(binding.rvFind) - linearLayoutManager = LinearLayoutManager(context) binding.rvFind.layoutManager = linearLayoutManager - adapter = ExploreAdapter(requireContext(), lifecycleScope, this) binding.rvFind.adapter = adapter adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index b140c4638..6db0827f3 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -40,14 +40,15 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), RssAdapter.CallBack { private val binding by viewBinding(FragmentRssBinding::bind) override val viewModel by viewModels() - private lateinit var adapter: RssAdapter - private lateinit var searchView: SearchView + private val adapter by lazy { RssAdapter(requireContext(), this) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private var rssFlowJob: Job? = null private val groups = linkedSetOf() private var groupsMenu: SubMenu? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - searchView = binding.titleBar.findViewById(R.id.search_view) setSupportToolbar(binding.titleBar.toolbar) initSearchView() initRecyclerView() @@ -106,7 +107,6 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), private fun initRecyclerView() { ATH.applyEdgeEffectColor(binding.recyclerView) - adapter = RssAdapter(requireContext(), this) binding.recyclerView.adapter = adapter adapter.addHeaderView { ItemRssBinding.inflate(layoutInflater, it, false).apply { diff --git a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt index 7f1c76638..78e628d19 100644 --- a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt @@ -30,9 +30,10 @@ import kotlinx.coroutines.launch class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { + private val viewModel: ReplaceRuleViewModel by activityViewModels() - private lateinit var adapter: GroupAdapter private val binding by viewBinding(DialogRecyclerViewBinding::bind) + private val adapter by lazy { GroupAdapter(requireContext()) } override fun onStart() { super.onStart() @@ -60,7 +61,6 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener toolBar.inflateMenu(R.menu.group_manage) toolBar.menu.applyTint(requireContext()) toolBar.setOnMenuItemClickListener(this@GroupManageDialog) - adapter = GroupAdapter(requireContext()) recyclerView.layoutManager = LinearLayoutManager(requireContext()) recyclerView.addItemDecoration(VerticalDivider(requireContext())) recyclerView.adapter = adapter diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index ab786bbf7..33c74842a 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -50,8 +50,10 @@ class ReplaceRuleActivity : VMBaseActivity() private val importRecordKey = "replaceRuleRecordKey" - private lateinit var adapter: ReplaceRuleAdapter - private lateinit var searchView: SearchView + private val adapter by lazy { ReplaceRuleAdapter(this, this) } + private val searchView: SearchView by lazy { + binding.titleBar.findViewById(R.id.search_view) + } private var groups = hashSetOf() private var groupMenu: SubMenu? = null private var replaceRuleFlowJob: Job? = null @@ -90,7 +92,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.fragme private val binding by viewBinding(FragmentRssArticlesBinding::bind) private val activityViewModel by activityViewModels() override val viewModel by viewModels() - lateinit var adapter: BaseRssArticlesAdapter<*> - private lateinit var loadMoreView: LoadMoreView + private val adapter: BaseRssArticlesAdapter<*> by lazy { + when (activityViewModel.rssSource?.articleStyle) { + 1 -> RssArticlesAdapter1(requireContext(), this@RssArticlesFragment) + 2 -> RssArticlesAdapter2(requireContext(), this@RssArticlesFragment) + else -> RssArticlesAdapter(requireContext(), this@RssArticlesFragment) + } + } + private val loadMoreView: LoadMoreView by lazy { + LoadMoreView(requireContext()) + } private var articlesFlowJob: Job? = null override val isGridLayout: Boolean get() = activityViewModel.isGridLayout @@ -64,15 +72,8 @@ class RssArticlesFragment : VMBaseFragment(R.layout.fragme } else { recyclerView.addItemDecoration(VerticalDivider(requireContext())) LinearLayoutManager(requireContext()) - - } - adapter = when (activityViewModel.rssSource?.articleStyle) { - 1 -> RssArticlesAdapter1(requireContext(), this@RssArticlesFragment) - 2 -> RssArticlesAdapter2(requireContext(), this@RssArticlesFragment) - else -> RssArticlesAdapter(requireContext(), this@RssArticlesFragment) } recyclerView.adapter = adapter - loadMoreView = LoadMoreView(requireContext()) adapter.addFooterView { ViewLoadMoreBinding.bind(loadMoreView) } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt index 02b823c91..f92e98f1f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt @@ -29,7 +29,7 @@ class RssSortActivity : VMBaseActivity() - private lateinit var adapter: TabFragmentPageAdapter + private val adapter by lazy { TabFragmentPageAdapter() } private val sortList = mutableListOf>() private val fragmentMap = hashMapOf() private val editSourceResult = registerForActivityResult( @@ -43,7 +43,6 @@ class RssSortActivity : VMBaseActivity(), RssFavoritesAdapter.CallBack { override val binding by viewBinding(ActivityRssFavoritesBinding::inflate) - private lateinit var adapter: RssFavoritesAdapter + private val adapter by lazy { RssFavoritesAdapter(this, this) } override fun onActivityCreated(savedInstanceState: Bundle?) { initView() @@ -29,7 +29,6 @@ class RssFavoritesActivity : BaseActivity(), binding.recyclerView.let { it.layoutManager = LinearLayoutManager(this) it.addItemDecoration(VerticalDivider(this)) - adapter = RssFavoritesAdapter(this, this) it.adapter = adapter } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt index d755c70fa..9e2559612 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt @@ -22,7 +22,7 @@ class RssSourceDebugActivity : VMBaseActivity() - private lateinit var adapter: RssSourceDebugAdapter + private val adapter by lazy { RssSourceDebugAdapter(this) } override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() @@ -57,7 +57,6 @@ class RssSourceDebugActivity : VMBaseActivity() private val importRecordKey = "rssSourceRecordKey" - private lateinit var adapter: RssSourceAdapter + private val adapter by lazy { RssSourceAdapter(this, this) } private var sourceFlowJob: Job? = null private var groups = hashSetOf() private var groupMenu: SubMenu? = null @@ -142,7 +142,6 @@ class RssSourceActivity : VMBaseActivity(), RuleSubAdapter.Callback { override val binding by viewBinding(ActivityRuleSubBinding::inflate) - private lateinit var adapter: RuleSubAdapter + private val adapter by lazy { RuleSubAdapter(this, this) } override fun onActivityCreated(savedInstanceState: Bundle?) { initView() @@ -53,7 +53,6 @@ class RuleSubActivity : BaseActivity(), } private fun initView() { - adapter = RuleSubAdapter(this, this) binding.recyclerView.adapter = adapter val itemTouchCallback = ItemTouchCallback(adapter) itemTouchCallback.isCanDrag = true diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/TextListDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/TextListDialog.kt index 6dd173274..690cc6413 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/TextListDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/TextListDialog.kt @@ -30,7 +30,7 @@ class TextListDialog : BaseDialogFragment() { } private val binding by viewBinding(DialogRecyclerViewBinding::bind) - lateinit var adapter: TextAdapter + private val adapter by lazy { TextAdapter(requireContext()) } private var values: ArrayList? = null override fun onStart() { @@ -53,7 +53,6 @@ class TextListDialog : BaseDialogFragment() { values = it.getStringArrayList("values") } recyclerView.layoutManager = LinearLayoutManager(requireContext()) - adapter = TextAdapter(requireContext()) recyclerView.adapter = adapter adapter.setItems(values) } diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index cdc5f1f9d..98e65fc1d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -37,8 +37,8 @@ class FontSelectDialog : BaseDialogFragment(), private val fontFolder by lazy { FileUtils.createFolderIfNotExist(appCtx.filesDir, "Fonts") } - private var adapter: FontAdapter? = null private val binding by viewBinding(DialogFontSelectBinding::bind) + private val adapter by lazy { FontAdapter(requireContext(), this) } private val selectFontDir = registerForActivityResult(HandleFileContract()) { uri -> uri ?: return@registerForActivityResult if (uri.toString().isContentScheme()) { @@ -83,7 +83,6 @@ class FontSelectDialog : BaseDialogFragment(), binding.toolBar.inflateMenu(R.menu.font_select) binding.toolBar.menu.applyTint(requireContext()) binding.toolBar.setOnMenuItemClickListener(this) - adapter = FontAdapter(requireContext(), this) binding.recyclerView.layoutManager = LinearLayoutManager(context) binding.recyclerView.adapter = adapter @@ -165,7 +164,7 @@ class FontSelectDialog : BaseDialogFragment(), } mergeFontItems(fontItems, getLocalFonts()) }.onSuccess { - adapter?.setItems(it) + adapter.setItems(it) }.onError { toastOnUi("getFontFiles:${it.localizedMessage}") } @@ -200,7 +199,7 @@ class FontSelectDialog : BaseDialogFragment(), } mergeFontItems(fontItems, getLocalFonts()) }.onSuccess { - adapter?.setItems(it) + adapter.setItems(it) }.onError { toastOnUi("getFontFiles:${it.localizedMessage}") }