ProgressBar(s)(Horizo​​ntal)未显示(int)long的进度

时间:2014-06-20 02:46:42

标签: java android android-layout android-progressbar

我在市场上有一个应用程序,并且正在添加几个横向ProgressBars。第一个栏正确更新和显示,没有任何问题。第二个条(相同的代码)在那里,但尽管设置了最大值和进度,但没有显示任何进度。我已经尝试使视图无效(和postInvalidate())没有运气。这是一些瑕疵:

Layout.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>

以下是我在Fragment

内部使用setProgress和Max on ProgressBars的代码

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:这似乎在想要的时候有效。我已经在几种具有不同存储方案的设备上进行了测试,有些显示完美,有些则完全没有。

1 个答案:

答案 0 :(得分:7)

我打赌它与整数溢出有关。

你的最大值是27 480 944 640,而整数最大值是2 147 483 647.由于进度条适用于整数,它可能做了很奇怪的事情。

您应该将进度条的值标准化。

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

这应该给你足够的代表性。