Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BaseActivity/BaseFragment中的ProgressBar管理_Android_Kotlin_Android Architecture Components_Android Progressbar - Fatal编程技术网

Android BaseActivity/BaseFragment中的ProgressBar管理

Android BaseActivity/BaseFragment中的ProgressBar管理,android,kotlin,android-architecture-components,android-progressbar,Android,Kotlin,Android Architecture Components,Android Progressbar,如果你在Android应用程序中也遇到类似的问题,请阅读下面的答案 需要从单个位置而不是每个活动/片段管理进度条 要从BaseAcivity/BaseFragment中删除ProgressDialog 在Android应用程序中有许多处理ProgreesBar的选项。我想分享我的方式。希望它能帮助一些人,节省一些时间 首先,我们在BaseActivity中添加一个根进度条,并在BaseFragment中使用相同的进度条 要在BaseActivity中添加进度条,请重写setContentVie

如果你在Android应用程序中也遇到类似的问题,请阅读下面的答案

  • 需要从单个位置而不是每个活动/片段管理进度条
  • 要从BaseAcivity/BaseFragment中删除ProgressDialog

在Android应用程序中有许多处理ProgreesBar的选项。我想分享我的方式。希望它能帮助一些人,节省一些时间

首先,我们在BaseActivity中添加一个根进度条,并在BaseFragment中使用相同的进度条

要在BaseActivity中添加进度条,请重写setContentView(layoutResID)方法

BaseActivity.kt

lateinit var progressListener: ProgressListener
private lateinit var progressBar: ProgressBar

override fun setContentView(layoutResID: Int) {
    val coordinatorLayout: CoordinatorLayout = layoutInflater.inflate(R.layout.activity_base, null) as CoordinatorLayout
    val activityContainer: FrameLayout = coordinatorLayout.findViewById(R.id.layout_container)
    progressBar = coordinatorLayout.findViewById(R.id.progressBar) as ProgressBar
    binder = DataBindingUtil.inflate(layoutInflater, layoutResId(), activityContainer, true)//to inflate a layout using Data binding 
    binder.lifecycleOwner = this
    super.setContentView(coordinatorLayout)
}

override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        progressListener = ProgressListener(getProgressBar())
    }

/**
 *  open it for overload it in other child activity
 *  So in case
 *  If you want to use BaseProgress just ignore it, Don't need to override this fun
 *  If you want to your progressbar change, Just define in your xml and override the progress bar instance to base
 *  If you don't want to use Base as well any custom progress just override this and return null
 *  */
open fun getProgressBar(): ProgressBar? {
        return progressBar
    }
class ProgressListener(private val progressBar: ProgressBar?) {
    fun showLoading(isVisible: Boolean) {
        progressBar?.visibility = if (isVisible) View.VISIBLE else View.GONE
    }
}
ProgressListener.kt

lateinit var progressListener: ProgressListener
private lateinit var progressBar: ProgressBar

override fun setContentView(layoutResID: Int) {
    val coordinatorLayout: CoordinatorLayout = layoutInflater.inflate(R.layout.activity_base, null) as CoordinatorLayout
    val activityContainer: FrameLayout = coordinatorLayout.findViewById(R.id.layout_container)
    progressBar = coordinatorLayout.findViewById(R.id.progressBar) as ProgressBar
    binder = DataBindingUtil.inflate(layoutInflater, layoutResId(), activityContainer, true)//to inflate a layout using Data binding 
    binder.lifecycleOwner = this
    super.setContentView(coordinatorLayout)
}

override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        progressListener = ProgressListener(getProgressBar())
    }

/**
 *  open it for overload it in other child activity
 *  So in case
 *  If you want to use BaseProgress just ignore it, Don't need to override this fun
 *  If you want to your progressbar change, Just define in your xml and override the progress bar instance to base
 *  If you don't want to use Base as well any custom progress just override this and return null
 *  */
open fun getProgressBar(): ProgressBar? {
        return progressBar
    }
class ProgressListener(private val progressBar: ProgressBar?) {
    fun showLoading(isVisible: Boolean) {
        progressBar?.visibility = if (isVisible) View.VISIBLE else View.GONE
    }
}
activity_base.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/layout_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

现在对于BaseFragment,只需使用BaseActivity的实例访问BaseActivity的进度条,并重写BaseFragment的open fun

注意:如果您面临一些问题,或者我无法正确解释解决方案,请在下面进行评论。

快乐编码:)