【问题标题】:ProgressBar not showing进度条不显示
【发布时间】:2016-09-13 09:19:20
【问题描述】:

我的相对布局有两个子 Rcyclerview (Invisible) 和 progressbar 。

我想要的是:Oncreate 布局,recyclerview 不可见,进度条显示,直到 AsyncTask(从 SD 文件夹获取图像)完成,recyclerview 可见,进度条不可见。

发生了什么:我的应用没有显示进度条。在 asyctask 完成后,我的 recyclerview 出现了。

XML:

 <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/view3">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:divider="@null"
                    android:visibility="invisible"
                    android:id="@+id/recycler_view_cameragallery"
                    android:dividerHeight="0dp"
                    android:fadingEdge="none" />

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/progressBar_Gallery"
                    style="?android:attr/progressBarStyleLarge"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:padding="12dp" />
            </RelativeLayout>

活动代码:

public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){

        View rootView=inflater.inflate(R.layout.camera_layout,container,false);
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        ButterKnife.inject(this,rootView);
        init();
        initialise(rootView);
        initialiseListeners();
        listofImagesPath = new ArrayList<String>();
        new mediaScanAyscTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);}

异步任务

public class mediaScanAyscTask extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {

            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            adapter.clearApplications();
            galleryProgressBar.setVisibility(View.VISIBLE);
            galleryProgressBar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#ff4081"), PorterDuff.Mode.MULTIPLY);
            cameraRecyclerView.setVisibility(View.INVISIBLE);

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            listofImagesPath = RetriveCapturedImagePath();

            if(listofImagesPath!=null){
                adapter.addApplications(listofImagesPath);
            }
            galleryProgressBar.setVisibility(View.INVISIBLE);
            cameraRecyclerView.setVisibility(View.VISIBLE);
            super.onPostExecute(aVoid);
        }
    }

请帮我指出我出错的地方。

【问题讨论】:

  • 尝试将回收站视图的可见性更改为消失而不是不可见
  • @ShashankUdupa :不,仍然无法正常工作
  • 我觉得进度条太小看不清,试试把 ProgressBar 中的 padding="12dp" 去掉
  • @ShashankUdupa:不,这也行不通。发生的事情是:这个异步任务运行了两次。 1. oncreate 和 2. 在相机的 ActivityResult 方法上单击 。所以发生的事情是点击后,屏幕变黑,直到所有 postExecution 完成。

标签: android progress-bar android-recyclerview android-progressbar android-mediascanner


【解决方案1】:

我遇到了同样的问题,原因是ProgressBar 位于RecyclerView 的底部。您需要将 RecyclerViewProgressBar 包装到 FrameLayout

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/view3">
         <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:divider="@null"
                    android:visibility="invisible"
                    android:id="@+id/recycler_view_cameragallery"
                    android:dividerHeight="0dp"
                    android:fadingEdge="none" />

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/progressBar_Gallery"
                    style="?android:attr/progressBarStyleLarge"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:padding="12dp" />
        </FrameLayout>
  </RelativeLayout>

【讨论】:

    【解决方案2】:

    希望这个回答对你有帮助。

    @Override
    protected void onPostExecute(Void aVoid) {
        listofImagesPath = RetriveCapturedImagePath();
            if(listofImagesPath!=null){
                adapter.addApplications(listofImagesPath);
            }
        galleryProgressBar.setVisibility(View.GONE);
        cameraRecyclerView.setVisibility(View.VISIBLE);
        super.onPostExecute(aVoid);
    }
    

    第二个选项是从 .xml 文件中删除您的 ProgressBar 并以编程方式创建。

    class YourClassName extends AsyncTask<String, Void, Boolean>{
        ProgressDialog dialog;  
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(Detail_Activity.this);
            dialog.setTitle("your title");
            dialog.setMessage("your message");
            dialog.setCancelable(false);
            dialog.show();
        }
    
        @Override
        protected Boolean doInBackground(String... params) {
            return null;
        }
    
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            dialog.cancel();
        }
    }
    

    【讨论】:

    • 试试我上面提到的第二个代码。它会帮助你。
    • 没有还是不行。发生的事情是:在调试它显示的 Progressbar 但是当我正常运行时它仍然显示
    • 你能上传截图吗?它有助于充分理解确切的问题
    【解决方案3】:

    在我的例子中,模拟器在开发者模式下关闭了动画,所以我看不到进度条。

    【讨论】:

    • 你是救星!
    【解决方案4】:
    Try this
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="100"
        android:id="@+id/progress"/>
    

    【讨论】:

    • 解释您的答案,以便用户更清楚地了解它
    • 这个属性 style=?android:attr/progressBarStyleLarge 给程序员有限的进度条控制权来完全控制进度条像 android:progress="100" 你是新手使用这个属性 style="@ android:style/Widget.ProgressBar.Horizo​​ntal"
    • 请注意comments are temporary and could be deleted anytime。如果您有其他信息要提供,请点击帖子下方的“edit”链接更新您的答案。谢谢。
    • 谢谢。我会注意的。
    • 或 style="?android:progressBarStyleHorizo​​ntal"
    【解决方案5】:

    让你的布局消失而不是隐形

    【讨论】:

    • @young_08 in android.support.v7.widget.RecyclerView 将可见性设置为不可见,不要更改 xml 文件中 ProgressBar 的可见性
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多