简体   繁体   English

如何使进度条始终位于滚动视图的中心屏幕和滑动刷新布局?

[英]how to make progress bar always in the center screen of scroll view and swipe refresh layout?

I have views hierarchy like this:我有这样的视图层次结构:

在此处输入图片说明

and here is my xml:这是我的 xml:

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/constraintLayout_profile">


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" android:id="@+id/swipeRefreshLayout_userControl">


        <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@android:color/white"
                android:id="@+id/scrollView_user_control"
                app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/imageView_profile_image_user_information"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:src="@drawable/user"
                        app:civ_border_width="0.5dp"
                        app:civ_border_color="#FF000000" android:layout_marginEnd="8dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
                        android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"/>
                <TextView
                        android:text="User Fullname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView_user_fullname_user_information" android:layout_marginTop="16dp"
                        app:layout_constraintTop_toBottomOf="@+id/imageView_profile_image_user_information"
                        app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
                        app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
                        android:textSize="18sp"/>
                <TextView
                        android:text="user@email.com"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView_user_email_user_information" app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="8dp"
                        app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
                        app:layout_constraintTop_toBottomOf="@+id/textView_user_fullname_user_information"
                        android:layout_marginTop="8dp"/>
                <TextView
                        android:text="AKAN DIHADIRI"
                        android:layout_width="0dp"
                        android:layout_height="35dp"
                        android:id="@+id/textView_will_come_user_information" app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                        app:layout_constraintTop_toBottomOf="@+id/textView_user_email_user_information"
                        android:gravity="center|left"
                        android:paddingStart="16dp" android:background="#D2D1D6"/>

                <ImageView
                        android:src="@drawable/ic_settings"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:id="@+id/imageView_gear_setting_user_information"
                        app:layout_constraintTop_toTopOf="@+id/imageView_profile_image_user_information"
                        app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="24dp"/>

                <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:scrollbars="vertical"
                        app:layout_constraintTop_toBottomOf="@+id/textView_will_come_user_information"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        tools:listitem="@layout/item_general_event"
                        android:id="@+id/recyclerView_attended_event_user_control"
                        app:layout_constraintHorizontal_bias="1.0"
                        android:nestedScrollingEnabled="false">
                </androidx.recyclerview.widget.RecyclerView>

                <ImageView
                        android:id="@+id/imageView_verified_icon"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginTop="4dp"
                        android:layout_marginEnd="4dp"
                        app:layout_constraintEnd_toEndOf="@+id/imageView_profile_image_user_information"
                        app:layout_constraintTop_toTopOf="@+id/imageView_profile_image_user_information"
                        app:srcCompat="@drawable/verifiedicon" />


            </androidx.constraintlayout.widget.ConstraintLayout>


        </androidx.core.widget.NestedScrollView>

        <ProgressBar
                android:id="@+id/progressBar_user_control"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible" />


    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>





</androidx.constraintlayout.widget.ConstraintLayout>

as you can see, there is recycler view inside my scroll view, and also there is a swipe refresh layout wrap it all.如您所见,我的滚动视图中有回收器视图,还有一个滑动刷新布局将其全部包裹起来。

I want my progress bar will always be in the center of the screen.我希望我的进度条始终位于屏幕中央。 I have tried to read from here: How to make a ProgressBar stay in the center of screen despite scrolling .我试图从这里阅读: How to make a ProgressBar留在屏幕中心尽管滚动 it is said that据说

You need the ScrollView to be at same level as ProgressDialog in your Layout您需要 ScrollView 与布局中的 ProgressDialog 处于同一级别

I have tried to implement that, like the image above, but I don't know why my progress bar will not appear in the screen.我试图实现它,如上图,但我不知道为什么我的进度条不会出现在屏幕上。

if I put the progress bar inside the scroll view like this:如果我将进度条放在滚动视图中,如下所示:

在此处输入图片说明

the progress bar will appear, but not in the center of the screen.进度条会出现,但不会出现在屏幕中央。 it depends of the number of items inside my recycler view.这取决于我的回收站视图中的项目数量。 so how to solve this ?那么如何解决这个问题呢?

Place your ProgressBar at the bottom of outer ConstraintLayout .ProgressBar放在外部ConstraintLayout的底部。 Check below:检查以下:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraintLayout_profile">


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" android:id="@+id/swipeRefreshLayout_userControl">


        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/white"
            android:id="@+id/scrollView_user_control"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/imageView_profile_image_user_information"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:src="@drawable/user"
                    app:civ_border_width="0.5dp"
                    app:civ_border_color="#FF000000" android:layout_marginEnd="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"/>
                <TextView
                    android:text="User Fullname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView_user_fullname_user_information" android:layout_marginTop="16dp"
                    app:layout_constraintTop_toBottomOf="@+id/imageView_profile_image_user_information"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
                    app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
                    android:textSize="18sp"/>
                <TextView
                    android:text="user@email.com"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView_user_email_user_information" app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginEnd="8dp"
                    app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
                    app:layout_constraintTop_toBottomOf="@+id/textView_user_fullname_user_information"
                    android:layout_marginTop="8dp"/>
                <TextView
                    android:text="AKAN DIHADIRI"
                    android:layout_width="0dp"
                    android:layout_height="35dp"
                    android:id="@+id/textView_will_come_user_information" app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
                    app:layout_constraintTop_toBottomOf="@+id/textView_user_email_user_information"
                    android:gravity="center|left"
                    android:paddingStart="16dp" android:background="#D2D1D6"/>

                <ImageView
                    android:src="@drawable/ic_settings"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:id="@+id/imageView_gear_setting_user_information"
                    app:layout_constraintTop_toTopOf="@+id/imageView_profile_image_user_information"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="24dp"/>

                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    app:layout_constraintTop_toBottomOf="@+id/textView_will_come_user_information"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    tools:listitem="@layout/item_general_event"
                    android:id="@+id/recyclerView_attended_event_user_control"
                    app:layout_constraintHorizontal_bias="1.0"
                    android:nestedScrollingEnabled="false">
                </androidx.recyclerview.widget.RecyclerView>

                <ImageView
                    android:id="@+id/imageView_verified_icon"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginEnd="4dp"
                    app:layout_constraintEnd_toEndOf="@+id/imageView_profile_image_user_information"
                    app:layout_constraintTop_toTopOf="@+id/imageView_profile_image_user_information"
                    app:srcCompat="@drawable/verifiedicon" />


            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <ProgressBar
        android:id="@+id/progressBar_user_control"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM