Mat*_*att 1 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>
Run Code Online (Sandbox Code Playgroud)
这里是我用于在ProgressBars上设置setProgress和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);
}
Run Code Online (Sandbox Code Playgroud)
这是结果(我觉得某处出现了一个无意义的错误),非常感谢任何见解.感谢您的时间

编辑:现在,我已经下载了一个文件,并在第一次存储时有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
Run Code Online (Sandbox Code Playgroud)
EDIT2:这似乎在它想要的时候起作用.我已经在几个具有不同存储方案的设备上进行了测试,有些显示完美,有些则完全没有.
我打赌它与整数溢出有关.
你的最大值是27 480 944 640,而整数最大值是2 147 483 647.由于进度条适用于整数,它可能做了奇怪的事情.
您应该将进度条的值标准化.
progB.setMax(100);
progB.setProgress((int) ((((float)usedB) / totalB)*100));
Run Code Online (Sandbox Code Playgroud)
这应该给你一个足够接近的表示.
| 归档时间: |
|
| 查看次数: |
841 次 |
| 最近记录: |