如何自动更改值更新进度条最大值?

时间:2018-04-13 18:23:41

标签: java android

我正在使用Google fit API处理健身应用。

来自API的步骤数据传递给方法stepDisplayer,例如params(3300,"步骤")。

我正在尝试更新进度条value == daily1,以便最后的条形为pbStepBar.setMax(daily2),并在pbStepBar.setMax(daily3)时再次更新为value == daily2,同时进度为setNextGoal(value) bar始终设置为值,例如3500/4000然后4102/8000和8900/12000。

我尝试使用一个switch语句,将目标与值进行比较,使用一个名为getCurrentGoal()的方法,使用getter和setter方法setCurrentGoal()daily1,如下所示,但这不起作用。运行应用程序时,最大值不会从pbStepBar更改,它看起来像5050/4000。

我做错了什么?我可以将<ProgressBar android:id="@+id/pbStepBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="300dp" android:layout_height="15dp" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:layout_marginTop="25dp" android:indeterminate="false" android:scaleY="3" /> public class MainActivity { //these are goals int daily1 = 4000 int daily2 = 8000 int daily3 = 12000 //set to this onCreate pbStepBar.setMax = daily1 private void stepDisplayer(final int value, final String field) { tvHomeSteps = (TextView) findViewById(R.id.tvHomeSteps); pbStepBar = (ProgressBar) findViewById(R.id.pbStepBar); setNextGoal(value); runOnUiThread(new Runnable() { @Override public void run() { tvHomeSteps.setText("today's " + field + " " + value + "/" + dailyStepGoal); } }); new Thread(new Runnable() { @Override public void run() { while (value <= getCurrentGoal()) { handler.post(new Runnable() { @Override public void run() { pbStepBar.setProgress(value); } }); try { Thread.sleep(25); } catch (InterruptedException e) { e.printStackTrace(); } if (getCurrentGoal() == value) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Well done you reached your daily goal of " + getCurrentGoal() + " " + field, Toast.LENGTH_LONG).show(); } }); } setNextGoal(value); } } }).start(); } private void setNextGoal (final int value) { switch (value) { case dailyStepGoal: setCurrentGoal(dailyStepGoal + stepGoalIncrement); pbStepBar.setMax(dailyStepGoal + stepGoalIncrement); break; case dailyStepGoal + stepGoalIncrement: setCurrentGoal(dailyStepGoal + (stepGoalIncrement*2)); pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*2)); break; case dailyStepGoal + (stepGoalIncrement*2): setCurrentGoal(dailyStepGoal + (stepGoalIncrement*3)); pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*3)); break; case dailyStepGoal + (stepGoalIncrement*3): setCurrentGoal(dailyStepGoal + (stepGoalIncrement*4)); pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*4)); break; case dailyStepGoal + (stepGoalIncrement*4): setCurrentGoal(dailyStepGoal + (stepGoalIncrement*5)); pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*5)); break; } } 替换为另一个进度条并切换可见性吗?

布局

<%= form_for_filterrific @filterrific do |f| %>
    <div>
        <%= f.select(:with_player_club, 
            @filterrific.select_options[:with_player_club],
            { include_blank: t('all_teams') }, class: 'form-control') %>
    </div>
    <%= render_filterrific_spinner %>
<% end %>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

我使用了如下所示的int变量currentGoal来跟踪当前目标。我认为问题是我试图从UI线程中调用活动中的方法。我使用了简单的if语句,不知道为什么我之前没有想到这一点。

我也测试了它运行良好的解决方案。

private void stepDisplayer(final int value,final String field){

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (value >= currentGoal) {
                Toast.makeText(getApplicationContext(), "Well done you reached your daily goal of " + currentGoal + " " + field,
                        Toast.LENGTH_LONG).show();
                currentGoal += stepGoalIncrement;
                tvHomeSteps.setText("today's " + field + " " + value + "/" + currentGoal);
            }
            else {
                tvHomeSteps.setText("today's " + field + " " + value + "/" + currentGoal);
            }
        }
    });

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (value <= currentGoal) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        pbStepBar.setProgress(value);

                        if (value >= pbStepBar.getMax()) {
                            pbStepBar.setMax(currentGoal);
                            pbStepBar.setProgress(value);
                        }
                        else {
                            pbStepBar.setProgress(value);
                        }
                    }
                });
                try {
                    Thread.sleep(25);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //setNextGoal(pbStepBar, value);
            }
        }
    }).start();
}