如何在Android中更改片段时加快速度

时间:2019-05-12 05:40:19

标签: android android-fragments kotlin bottomnavigationview

为什么没有人可以帮助我?

在我的应用程序中,我使用了 BottomNavBar NavigationGraph来显示一些fragments
在我的fragments之一中,我有很多视图(fragment布局具有 1069行 xml代码),当从fragment中选择此BottomNavBar时,稍等片刻,告诉我这个fragment
换句话说,告诉我这个fragment 延迟

片段代码:

class HomeDashboardFragment : Fragment(), HomeDashboardContracts.View {

    @NonNull
    private var pageTitle: TextView? = null
    @NonNull
    private var menuIcon: TextView? = null
    private lateinit var token: String
    private lateinit var presenter: HomeDashboardPresenterImpl
    private var giftExpireSplit: List<String> = emptyList()
    private var giftExpireDate: List<String> = emptyList()
    private var timeUtils: TimeUtils? = null
    @NonNull
    private var disposable: Disposable? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home_dashboard, container, false)
    }

    @SuppressLint("WrongConstant")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //Initialize
        presenter = HomeDashboardPresenterImpl(requireContext(), this)
        //Initialize views from activity
        activity?.let { itActivity ->
            pageTitle = itActivity.findViewById(R.id.toolbarMain_title)
            menuIcon = itActivity.findViewById(R.id.toolbarMain_menuIcon)
            //Set title
            pageTitle?.let { itTitle ->
                itTitle.text = getString(R.string.menuHomeDashboard)
            }
            //Open menu
            menuIcon?.let { itMenu ->
                itMenu.setOnClickListener {
                    itActivity.findViewById<AwesomeDrawerLayout>(R.id.homePage_drawerLayout).openDrawer(Gravity.END)
                }
            }
            //Get token
            token = GoodPrefs.getInstance().getString(PrefsKey.USER_JWT_TOKEN.name, "")
            //User registered
            if (GoodPrefs.getInstance().isKeyExists(PrefsKey.USER_JWT_TOKEN.name)) {
                menuIcon?.visibility = View.VISIBLE
            }
            //Set layout
            presenter.checkRegisterUser(token)
            //Load profile data
            if (!isEmptyString(token)) {
                presenter.getProfile(token, USER_NOTIF_ID)
            }
        }
    }

MainActivity代码,用于将fragments设置为BottomNavBarNavigationGraph中:

private fun setupNavigation() {
    val navController = Navigation.findNavController(this, R.id.homePage_fragmentNavHost)
    NavigationUI.setupWithNavController(homePage_bottomNavBar, navController)
}

override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.homePage_fragmentNavHost).navigateUp()

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

您可以添加由单个viewGroup组成的布局虚拟对象。然后在onViewCreated方法中填充实际布局。例如:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
       inflater.inflate(R.layout.layout_dummy, container, false);

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        Handler().post {
            val layout = flContainer
            val child = layoutInflater.inflate(R.layout.fragment_real, null)
            layout.removeAllViews()
            layout.addView(child)

            setupToolbar()
            setupWebView()
        }
    }


layout_dummy.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        android:id="@+id/flContainer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</FrameLayout>

答案 1 :(得分:0)

您的问题几乎可以肯定是由庞大的XML文件引起的。加载所有这些视图需要时间。您有很多嵌套,这会使事情变慢。

您必须优化布局。删除不需要的东西。用ConstraintLayout替换尽可能多的嵌套LinearLayouts。如果打算将其用作长滚动视图,则可以使用RecyclerView。

希望有帮助。