Android ProgressBar setProgress()不';我不能如愿工作
我正在制作一个进度条,显示游戏的等级。 这里有一个例子 这是密码Android ProgressBar setProgress()不';我不能如愿工作,android,progress-bar,Android,Progress Bar,我正在制作一个进度条,显示游戏的等级。 这里有一个例子 这是密码 ProgressBar userRatingProgressBar = root.findViewById(R.id.users_rating); userRatingProgressBar.setProgress(0); ... // game ratings if (game.getRatingCount() > 0) { userRatingProgressBar.setProgress
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)似乎不会更新进度条。我甚至尝试在“空检查”之前将进度条设置为0,但这种情况还是发生了
我在Miui12.0.5的小米Mi9T(安卓10 QKQ1.190825.002)和安卓11的安卓工作室模拟的像素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>
我找到了解决办法!
显然,我没有在layout.xml中设置进度条因此,如果在xml中添加
android:progress=“1”
,则进度条每次都会更新。这可能是巧合,但在进度值错误的情况下,是否与其他进度条值相同?只是一些随机值?或者之前游戏的值?错误的进度条值与之前显示的游戏值相同。您可以共享java代码的上下文吗?也就是说,它使用什么方法,什么调用该方法,等等。此外,以防万一,您是否可以确认,当您“切换到一个没有评级的游戏”时,您100%确定“else”块中的代码被命中?如果它与前面显示的游戏相同,那么您设置的数据错误,或者循环错误,我添加了GameDetail的java代码。我已经调试过好几次了:在调试器中,我看到了正确的评级编号,进度条也有正确的编号:只是进度没有得到更新。数据不可能是错误的,因为这有时有效,有时无效。代码中没有循环
<?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>
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;
}
}