Android ProgressBar setProgress() 无法按预期工作



我正在制作一个显示游戏评级的进度条。这里有一个示例CorrectGameRateingProgressBar。

这是代码

ProgressBar userRatingProgressBar = root.findViewById(R.id.users_rating);
userRatingProgressBar.setProgress(0);
...
// game ratings
if (game.getRatingCount() > 0) {
userRatingProgressBar.setProgress((int) game.getRating());
gameUsersRatingText.setText(String.valueOf((int) game.getRating()));
} else {
userRatingProgressBar.setProgress(0);
gameUsersRatingText.setText("N/A");
}
...

它工作得很好,除了一件事:当我从一个游戏切换到另一个没有评级的游戏时,setProgress(0(有时似乎不会更新进度条,如IncorrectGameRateingProgressBar所示。我甚至试图在"之前将进度条设置为0;空校验">但这件事还是会发生的。

我在MIUI 12.0.5的小米Mi9T(安卓10 QKQ1.190825.002(和安卓11 的安卓工作室模拟的Pixel 2上尝试了这个

有办法解决这个问题吗?如果你需要更多的信息,不要问
感谢所有


我把活动的布局layout.xml留在这里

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/user_rating_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/GameDetailTextColor"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/users_rating"
app:layout_constraintEnd_toEndOf="@+id/users_rating"
app:layout_constraintStart_toStartOf="@+id/users_rating"
app:layout_constraintTop_toTopOf="@+id/users_rating"
tools:text="60" />
<ProgressBar
android:id="@+id/users_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminateOnly="false"
android:progressDrawable="@drawable/rating_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:progress="60" />
</androidx.constraintlayout.widget.ConstraintLayout>

和可绘制的custom_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="?attr/SearchBarBackgroundColor" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:innerRadiusRatio="3.1"
android:shape="ring"
android:thicknessRatio="12"
android:useLevel="true">
<solid android:color="@color/orange_700" />
</shape>
</rotate>
</item>
</layer-list>

编辑:这是java片段的代码:

public class GameDetailFragment extends Fragment {
...
private long gameId = 0;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_game_detail, container, false);
if (getArguments() != null) {
gameId = getArguments().getLong("GAME_ID");
}
GameDetailViewModelFactory viewModelFactory = ((MainActivity) requireActivity()).getAppContainer().gameDetailViewModelFactory;
mViewModel = new ViewModelProvider(this, viewModelFactory).get(GameDetailViewModel.class);
...
ProgressBar userRatingProgressBar = root.findViewById(R.id.users_rating);
userRatingProgressBar.setProgress(0);
if (gameId != 0) {
...
// game ratings
if (game.getRatingCount() > 0) {
userRatingProgressBar.setProgress((int) game.getRating());
gameUsersRatingText.setText(String.valueOf((int) game.getRating()));
} else {
userRatingProgressBar.setProgress(0);
gameUsersRatingText.setText("N/A");
}
...
return root;
}
}

我找到了解决方案!显然,我没有在layout.xml中设置进度条
因此,如果在xml中添加android:progress="1",进度条每次都会更新。