簡體   English   中英

如何在 Android 的片段中顯示和隱藏進度條?

[英]How to display and hide progress bar from fragment in Android?

我有一個片段,我需要在單擊按鈕時顯示進度條,並在成功完成任務后刪除進度條。

這是我的片段類的 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 中添加進度條,但它對我來說不太有效。 這是片段 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()。]

EDIT2:主要活動:

@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>

讓我們通過您的代碼 go 了解 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);

我已經讓它工作了。 訣竅是按照@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);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM