【问题标题】:Set progress bar on top of RecyclerView and remove after data is loaded在 RecyclerView 顶部设置进度条并在数据加载后移除
【发布时间】:2015-03-02 21:31:34
【问题描述】:

我希望在 RecyclerView 的顶部显示一个加载图标,并在数据加载完成后消失

看起来像:

谁能帮帮我?

我的代码在 RecyclerView 上方显示了一个 TextView,上面写着“正在加载...”并在加载数据后消失,但 RecyclerView 仍然可见。

我的 XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/loaderTV"
    android:text="Loading..."
    android:layout_above="@+id/eventListRV"/>

<android.support.v7.widget.RecyclerView
    android:scrollbars="vertical"
    android:id="@+id/eventListRV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/spinner">



    </android.support.v7.widget.RecyclerView>

然后在我的代码中

client.listCreators(request, new Callback<ServiceResponse<Event>>() {
            @Override
            public void success(ServiceResponse<Event> eventServiceResponse, Response response) {
                eventList.addAll(eventServiceResponse.data.results);
                Log.i("tag", String.valueOf(eventServiceResponse.data.total));

                if (eventList.size() > 60) {
                    adapter.notifyDataSetChanged();
                    loadingText.setVisibility(View.GONE);
                }
            }

但我希望 RecyclerView 在数据加载时不可见,我希望进度条位于 RecyclerView 的顶部,我不希望它是文本视图

【问题讨论】:

    标签: android android-layout


    【解决方案1】:
    1. 将 RecyclerView 和加载布局(或 TextView)包装在 FrameLayout 中。然后将宽度和高度都设置为match_parent
    2. 在活动的 onCreate() 或片段的 onCreateView() 中隐藏 RecyclerView:recyclerView.setVisibility(View.GONE);
    3. 在你的回调方法中用setVisibility(View.GONE)隐藏加载布局,用setVisibility(View.VISIBLE)显示你的RecyclerView并用adapter.notifyDataSetChanged()触发适配器的重新加载

    【讨论】:

    • 好的,它现在居中,除了加载图标很大!我想我可以想办法让它变小
    • 当我使用 notifyDataSetChanged 时 UI 冻结,progressBar 停止:stackoverflow.com/questions/41945734/…
    【解决方案2】:

    回收站视图。

    1. 使用RelativeLayoutProgressBar 准备与android:layout_centerInParent="true"

      <ProgressBar
          android:id="@+id/progressbar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true" />
      
      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recycler_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      

    2. MainViewAdapter extends RecyclerView.Adapter&lt;MainViewAdapter.MyViewHolder&gt;

    onBindViewHolder

     @Override
     public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
     MainItem mainItem = mainItemList.get(position);
     holder.getTextViewTitle().setText(mainItem.getNameMain());
    
     //all your code here...
    
     //After that display progressbar at the below
     progressBar.setVisibility(View.GONE);
     }
    

    【讨论】:

    • 非常感谢,它节省了我很多时间!
    【解决方案3】:

    要缩放进度条大小,您可以使用 scaleX 和 scaleY 并将值调整为更小的尺寸。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frameLayout">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <ProgressBar
            android:id="@+id/progressBar"
            android:visibility="visible"
            android:scaleX="0.10"
            android:scaleY="0.10"
            android:textColor="@color/colorAccent"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_gravity="center"/>
    </FrameLayout>
    

    【讨论】:

      【解决方案4】:

      对于进度条的居中用户使用

      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      

      【讨论】:

        【解决方案5】:

        创建自定义进度对话框布局

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="center"
        android:layout_width="match_parent" android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:gravity="center"
            android:padding="3dp"
            android:background="@drawable/dialog_background"
            android:layout_height="wrap_content">
        
            <ProgressBar
                android:padding="3dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        
        </LinearLayout>
        
        </RelativeLayout>
        

        在 Drawable 中创建自定义背景

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="@android:color/white"/>
        <size android:height="60dp" android:width="60dp"/>
        </shape>
        

        在 Java 中创建 ProgressDialog

        public static ProgressDialog mProgressDialog;
        

        定义属性并在创建时显示

                mProgressDialog = new ProgressDialog(CategoryActivity.this);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
                mProgressDialog.setContentView(R.layout.custom_dailog);
                mProgressDialog.getWindow().setBackgroundDrawableResource(
                        android.R.color.transparent
                );
        

        调用 ProgressDialog 以在此处关闭

             @Override
         public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
         MainItem mainItem = mainItemList.get(position);
         holder.getTextViewTitle().setText(mainItem.getNameMain());
        
         //all your code here...
        
         //at end of onBindViewHolder dimiss this
         CategoryActivity.mProgressDialog.dismiss();
         }
        

        进度对话框将一直显示,直到项目在 recyclerview 中加载,并且在项目加载后将被关闭

        【讨论】:

          【解决方案6】:

          首先,在XML文件中添加ProgressBar标签

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

          那么,

          在 Kotlin 中,

          如果您使用的是 LiveData,那么在观察者内部调用该方法

          checkProgressBar(list) // 发送列表

          viewModel!!.readData.observe(viewLifecycleOwner, Observer {
          
                  checkProgressBar(it)
          
                  viewBindingObject.rv.adapter =
                      context?.let { it1 -> ImagesAdapter(it1, it as MutableList<SingleImage>) }
              })
          

          然后在检查列表大小后在方法中设置progressBar的可见性为Gone

          private fun checkProgressBar(list: List<SingleImage>) {
              if(list.size?.compareTo(0) != 0)
                  viewBindingObject.progressBar.visibility = View.GONE
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-12
            • 1970-01-01
            • 2017-02-10
            相关资源
            最近更新 更多