创建一个可以被多个活动使用的组件
Creating a component that can be utilised by multiple activites
我正在尝试创建一个可以在我的应用程序中的多个活动中使用的进度条。进度条包含一些逻辑,比如什么时候重置,进度条递增的速度等
我不想创建和编码逻辑来在每个 activity 中实现进度条,我想在单独的 class 中创建它,但我不确定如何执行此操作。我遇到了允许您重新使用布局的 include 标记,但我不确定如何实现进度条附带的逻辑。有人 advice/suggestion 知道解决这个问题的最佳方法吗?
正如我在评论中所写,您可以使用显示/隐藏进度条的方法定义自定义 Activity,然后让所有其他活动扩展它。
我为您提供了一个带有自定义 ProgressDialog 的示例:
public class BaseActivity extends AppCompatActivity {
private MyProgressDialog mLoader;
protected void showLoader() {
if (mLoader == null) {
mLoader = new MyProgressDialog(this);
}
if (!mLoader.isShowing()) {
mLoader.show();
}
}
protected void hideLoader() {
if (mLoader != null && mLoader.isShowing()) {
mLoader.dismiss();
}
}
}
public class MyActivity extends BaseActivity {
// here you can use showLoader and hideLoader wherever you need
}
当然你可以使用任何你想要的而不是标准的 ProgressDialog。例如,您可以定义自定义视图。重要的是,无论您将如何在 BaseActivity 中实现加载程序,修改都将适用于您的所有活动。
这是一个带有简单自定义布局的进度对话框示例。
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context, R.style.AppTheme_Translucent);
}
public MyProgressDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_progress);
setCancelable(false);
}
@Override
public void setCancelable(boolean flag) {
super.setCancelable(flag);
}
}
这里是dialog_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:keepScreenOn="true" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bg_progress_dialog" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="12dp"
android:indeterminateTint="#00acff"
android:indeterminateTintMode="src_in" />
</RelativeLayout>
</RelativeLayout>
这里是bg_progress_dialog.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/black" />
<corners android:radius="10dp" />
</shape>
这里是样式styles.xml
:
<style name="AppTheme.Translucent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
我正在尝试创建一个可以在我的应用程序中的多个活动中使用的进度条。进度条包含一些逻辑,比如什么时候重置,进度条递增的速度等
我不想创建和编码逻辑来在每个 activity 中实现进度条,我想在单独的 class 中创建它,但我不确定如何执行此操作。我遇到了允许您重新使用布局的 include 标记,但我不确定如何实现进度条附带的逻辑。有人 advice/suggestion 知道解决这个问题的最佳方法吗?
正如我在评论中所写,您可以使用显示/隐藏进度条的方法定义自定义 Activity,然后让所有其他活动扩展它。
我为您提供了一个带有自定义 ProgressDialog 的示例:
public class BaseActivity extends AppCompatActivity {
private MyProgressDialog mLoader;
protected void showLoader() {
if (mLoader == null) {
mLoader = new MyProgressDialog(this);
}
if (!mLoader.isShowing()) {
mLoader.show();
}
}
protected void hideLoader() {
if (mLoader != null && mLoader.isShowing()) {
mLoader.dismiss();
}
}
}
public class MyActivity extends BaseActivity {
// here you can use showLoader and hideLoader wherever you need
}
当然你可以使用任何你想要的而不是标准的 ProgressDialog。例如,您可以定义自定义视图。重要的是,无论您将如何在 BaseActivity 中实现加载程序,修改都将适用于您的所有活动。
这是一个带有简单自定义布局的进度对话框示例。
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context, R.style.AppTheme_Translucent);
}
public MyProgressDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_progress);
setCancelable(false);
}
@Override
public void setCancelable(boolean flag) {
super.setCancelable(flag);
}
}
这里是dialog_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:keepScreenOn="true" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bg_progress_dialog" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="12dp"
android:indeterminateTint="#00acff"
android:indeterminateTintMode="src_in" />
</RelativeLayout>
</RelativeLayout>
这里是bg_progress_dialog.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/black" />
<corners android:radius="10dp" />
</shape>
这里是样式styles.xml
:
<style name="AppTheme.Translucent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>