java - ProgressBar(s)(水平)不显示(int)long的进度

标签 java android android-layout android-progressbar

我在市场上有一款应用程序,我正在添加一些水平进度条。第一个栏更新并正确显示,没有任何问题。第二个栏(相同的代码)在那里,但尽管设置了最大值和进度,但没有显示任何进度。我试过使 View 无效(和 postInvalidate()),但没有成功。这是一些 fragment :

布局.xml:

       <TableRow
            android:layout_width="match_parent"
            android:gravity="center" >

            <TextView
                android:id="@+id/tvStorageAName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:padding="3dp"
                android:text="@string/storage_a" />

            <TextView
                android:id="@+id/tvStorageA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="1"
                android:padding="3dp"
                android:text="N/A" />

            <ProgressBar
                android:id="@+id/progBarStorageA"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="8dp"
                android:layout_column="2"
                android:layout_weight="2"
                android:background="@drawable/progbar_bg"
                android:indeterminate="false"
                android:indeterminateOnly="false"
                android:progressDrawable="@drawable/progbar_bg" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:gravity="center" >

            <TextView
                android:id="@+id/tvStorageBName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_weight="1"
                android:padding="3dp"
                android:text="@string/storage_b" />

            <TextView
                android:id="@+id/tvStorageB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_weight="1"
                android:padding="3dp"
                android:text="N/A" />

            <ProgressBar
                android:id="@+id/progBarStorageB"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="8dp"
                android:layout_column="2"
                android:layout_weight="2"
                android:background="@drawable/progbar_bg"
                android:indeterminate="false"
                android:indeterminateOnly="false"
                android:progressDrawable="@drawable/progbar_bg" />
        </TableRow>

这是我用来在 ProgressBars 上设置 Progress 和 Max 的代码,在 Fragment

fragment .java:

progA = (ProgressBar) getView().findViewById(R.id.progBarStorageA);
    progB = (ProgressBar) getView().findViewById(R.id.progBarStorageB);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        kitKatWorkaround();
    } else if (storageInfoList.size() > 0) {

        tvStorageAName.setText(storageInfoList.get(0).path);

        long usedA = StorageUtils.getUsedSpace(storageInfoList.get(0).path);
        long totalA = StorageUtils
                .getTotalSpace(storageInfoList.get(0).path);

        devStorageA = StorageUtils.getReadableFileSize(usedA, true) + "/"
                + StorageUtils.getReadableFileSize(totalA, true);

        progA.setMax((int) totalA);
        progA.setProgress((int) usedA);

        if (storageInfoList.size() > 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                    && !storageInfoList.get(0).internal) {
                kitKatWorkaround();
            }
            tvStorageBName.setText(storageInfoList.get(1).path);

            long usedB = StorageUtils
                    .getUsedSpace(storageInfoList.get(1).path)
                    + (StorageUtils.getUsedSpace("system/"));
            long totalB = StorageUtils
                    .getTotalSpace(storageInfoList.get(1).path);

            devStorageB = StorageUtils.getReadableFileSize(usedB, true)
                    + "/" + StorageUtils.getReadableFileSize(totalB, true);

            progB.setMax((int) totalB);
            progB.setProgress((int) usedB);
        } else {

            tvStorageBName.setVisibility(View.GONE);
            tvStorageB.setVisibility(View.GONE);
            progB.setVisibility(View.GONE);
            devStorageB = "N/A";
        }
    } else {
        devStorageA = "N/A";

        tvStorageBName.setVisibility(View.GONE);
        tvStorageB.setVisibility(View.GONE);
        progB.setVisibility(View.GONE);
        devStorageB = "N/A";
    }
            @SuppressLint("NewApi")
public void kitKatWorkaround() {

    tvStorageA = (TextView) getView().findViewById(R.id.tvStorageA);
    tvStorageB = (TextView) getView().findViewById(R.id.tvStorageB);

    File[] sdCards = getActivity().getApplicationContext()
            .getExternalCacheDirs();

    if (sdCards.length > 0
            && sdCards[0] != null
            && Environment.getStorageState(sdCards[0]).equals(
                    Environment.MEDIA_MOUNTED)) {

        File sdCard1 = sdCards[0];

        tvStorageAName.setText(sdCard1.getAbsolutePath()
                .replace(
                        "Android/data/" + getActivity().getPackageName()
                                + "/cache", ""));

        long usedA = StorageUtils.getUsedSpace(sdCard1.getAbsolutePath());
        long totalA = StorageUtils.getTotalSpace(sdCard1.getAbsolutePath());

        devStorageA = StorageUtils.getReadableFileSize(usedA, true) + "/"
                + StorageUtils.getReadableFileSize(totalA, true);

        progA.setMax((int) totalA);
        progA.setProgress((int) usedA);
    } else {
        devStorageA = "N/A";
    }

    if (sdCards.length > 1
            && sdCards[1] != null
            && Environment.getStorageState(sdCards[1]).equals(
                    Environment.MEDIA_MOUNTED)) {
        File sdCard2 = sdCards[1];

        tvStorageBName.setText(sdCard2.getAbsolutePath()
                .replace(
                        "Android/data/" + getActivity().getPackageName()
                                + "/cache", ""));

        long usedB = StorageUtils.getUsedSpace(sdCard2.getAbsolutePath());
        long totalB = StorageUtils.getTotalSpace(sdCard2.getAbsolutePath());

        devStorageB = StorageUtils.getReadableFileSize(usedB, true) + "/"
                + StorageUtils.getReadableFileSize(totalB, true);

        progB.setMax((int) totalB);
        progB.setProgress((int) usedB);

    } else {
        tvStorageBName.setVisibility(View.GONE);
        tvStorageB.setVisibility(View.GONE);
        progB.setVisibility(View.GONE);
        devStorageB = "N/A";
    }

    tvStorageA.setText(devStorageA);
    tvStorageB.setText(devStorageB);
}

这是结果(我觉得某处有一个笨拙的错误),非常感谢任何见解。感谢您的时间 This is the result

编辑:现在,我已经下载了一个文件,第一个存储空间为 25Gb/27.5Gb。 progressBar 现在都没有显示进度。我现在迷路了......我可以用 logcat 确认(以字节为单位):

06-19 23:56:00.729: D/StorageUtils(24282): /proc/mounts
06-19 23:56:00.739: D/Storage(24282): 24006316032/27480944640
06-19 23:56:00.739: D/Storage(24282): 15548907520/15923150848

EDIT2: 这似乎在需要时起作用。我已经在具有不同存储场景的多台设备上进行了测试,有些显示完美,有些则完全没有。

最佳答案

我敢打赌这与整数溢出有关。

您的最大值是 27 480 944 640,而整数最大值是 2 147 483 647。由于进度条使用整数,它可能会做一些奇怪的事情。

您应该规范化进度条的值。

progB.setMax(100);
progB.setProgress((int) ((((float)usedB) / totalB)*100));

这应该给你一个足够接近的表示。

关于java - ProgressBar(s)(水平)不显示(int)long的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319188/

相关文章:

android - 在 android flutter 中使用 gif 图像启动屏幕

java - 设置可见性上的三星 G5 空指针异常

java - 安卓.view.InflateException : Binary XML file line #13: Error inflating class ViewPager

android - 图形布局显示空指针异常

java - 在 Java 1.8.0_152 中使用 Math.round(double d) 时转换为 int

Java - 高效处理文件上传并创建新的http请求

java - 为什么 Java 不允许从静态初始化 block 中抛出已检查的异常?

java - 如何在 JSONArray 中正确嵌套数组

java - GCMParameterSpec 抛出 InvalidAlgorithmParameterException : unknown parameter type

Android ListView 切断比第一个项目更宽的项目