【问题标题】:How to display and hide progress bar from fragment in Android?如何在Android中显示和隐藏片段的进度条?
【发布时间】:2021-06-11 07:57:33
【问题描述】:

我有一个片段,我需要在点击按钮时显示进度条,并在成功完成任务后移除进度条。

这是我的片段类的 onCreateView 方法

private RelativeLayout view;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.fragment_note, container, false);
    view = root;
    
    FloatingActionButton add = root.findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectImage(context);
            displayProgressBar(true);
        }
    });


    hideKeyboard();

    return root;
}

这是我用来显示和隐藏进度条的方法。但是当我试图隐藏进度条时它不起作用。

private void displayProgressBar(boolean visible) {
    RelativeLayout layout = new RelativeLayout(getContext());
    ProgressBar progressBar = new ProgressBar(MainActivity.getActivity(),null,android.R.attr.progressBarStyleLarge);
    progressBar.setIndeterminate(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);


    if (visible) {
        layout.addView(progressBar, params);
        MainActivity.getActivity().setContentView(layout);
        progressBar.setVisibility(View.VISIBLE);
    } else {
        progressBar.setVisibility(View.INVISIBLE);
        MainActivity.getActivity().setContentView(view);
    }

}

我已经尝试在我的片段的 xml 中添加 progressBar,但它对我来说并不是很有效。 这是片段 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/note"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    
    <LinearLayout android:id="@+id/note_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/AppTheme"
        android:orientation="vertical">

        <EditText android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="@dimen/fab_margin_medium"
            android:background="@android:color/transparent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:hint="Double tap to type"
            android:gravity="top"
            android:textCursorDrawable="@drawable/color_cursor"
            android:inputType="textMultiLine"
            android:id="@+id/taskText" />
    </LinearLayout>



    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin_small"
        android:background="@color/colorPrimaryLight"
        android:backgroundTint="@color/colorPrimaryLight"
        android:outlineSpotShadowColor="@color/colorPrimaryLight"
        android:outlineAmbientShadowColor="@color/colorPrimaryLight"
        app:borderWidth="0dp"
        android:src="@drawable/plus" />
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</RelativeLayout>

我是 Android 新手。任何帮助表示赞赏。 提前致谢。

编辑: 我尝试通过以下方式重置片段视图:

MainActivity.getActivity().setContentView(view);

它给了我以下错误: 指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。]

编辑2: 主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    setContentView(R.layout.main_activity);
    setBottomNavigation();
    savedState = savedInstanceState;
}

public void setBottomNavigation() {
    BottomNavigationView navView = findViewById(R.id.nav_view);
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_files)
            .build();
    NavController navController = Navigation.findNavController(MainActivity.this,
            R.id.nav_host_fragment);
    NavigationUI.setupWithNavController(navView, navController);
    navView.setVisibility(View.VISIBLE);
}

和 MainActivity XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 我会选择 Binding Adapter 来实现这一点。见here

标签: android android-layout android-fragments


【解决方案1】:

让我们看看你的 ProgressBar 隐藏和显示机制的代码。

private void displayProgressBar(boolean visible) {
    RelativeLayout layout = new RelativeLayout(getContext());
    ProgressBar progressBar = new ProgressBar(MainActivity.getActivity(),null,android.R.attr.progressBarStyleLarge);
    progressBar.setIndeterminate(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);


    if (visible) {
        layout.addView(progressBar, params);
        MainActivity.getActivity().setContentView(layout);
        progressBar.setVisibility(View.VISIBLE);
    } else {
        progressBar.setVisibility(View.INVISIBLE);
    }

}

你的代码很好,你正在做的是在你的布局中创建一个运行时的新进度条,它很好,它显示了。

问题是当你试图隐藏时,你并没有以任何方式引用旧的ProgressBar,而是创建了另一个ProgressBar。所以要修复它,我建议你保留一个顶级变量,如

 ProgressBar progressBar;

然后分配它并在您的方法中隐藏和显示。

来到 XML,您可以简单地将其添加到您的布局中,如下所示

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:visibility="visible" />

之后,您需要使用findViewById 来获取对它的引用,然后您可以简单地隐藏它或按如下方式显示它

progressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);

【讨论】:

  • 我也需要删除RelativeLayout - “布局”并切换回我的片段视图。这不会回到之前的视图 R.layout.fragment_note。
  • 您应该只将进度条保留在您发布的 XML 中,并使用findViewById 查找然后/显示相应地隐藏它。您不需要以编程方式添加它。
  • 感谢您的及时回复。但是在XML中添加progressBar。我收到此错误 layout/main_activity: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
  • 接受您提出错误的答案。
【解决方案2】:

我已经开始工作了。诀窍是按照@che10 的建议将progressBar 变量声明为全局变量。

我还需要使用 XML 的相对布局,而不是初始化一个新的相对布局。它可能对某人有帮助:

private void displayProgressBar(boolean visible) {
    RelativeLayout layout = view.findViewById(R.id.note);
    
    if (visible) {
        progressBar = new ProgressBar(MainActivity.getActivity(),null,android.R.attr.progressBarStyleLarge);
        progressBar.setIndeterminate(true);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(progressBar, params);
        progressBar.setVisibility(View.VISIBLE);
    } else {
        layout.removeView(progressBar);
        progressBar.setVisibility(View.GONE);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多