android progressBar does not update progress view/drawable

后端 未结 15 2269
悲哀的现实
悲哀的现实 2020-11-28 09:11

two Bars which shows the progress of a game. If the user get points or time is up etc the progressBars should be updated:

private TextView tv;
private Progre         


        
相关标签:
15条回答
  • 2020-11-28 09:48

    In my case, I create VerticalSeekBar, and solution of Stuck does not work. After hours, I have found a solution:

    @Override
    public synchronized void setProgress(int progress) {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }
    

    Hope that help anyone.

    0 讨论(0)
  • 2020-11-28 09:52

    For me, calling setMax() before setProgress() worked for some reason.

    0 讨论(0)
  • 2020-11-28 09:58

    This worked for me...

    OneFragmen.java

    public class OneFragment extends Fragment{
    
    public OneFragment() {
        // Required empty public constructor
    }
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    
    int progress=0;
    private Handler handler = new Handler();
    TextView txtProgress;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(
        LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState ) {
    
       View view =  inflater.inflate(R.layout.fragment_one, container, false);
        getActivity().setTitle(R.string.app_name);
    
       final ProgressBar spinner = 
           (ProgressBar) view.findViewById(R.id.outerProgressBar);
        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.circular);
        txtProgress = (TextView)view.findViewById(R.id.txtProgress);
        spinner.setProgressDrawable(drawable);
        spinner.setSecondaryProgress(100);
        spinner.setMax(100);
        spinner.setProgress(0);
    
        new Thread(new Runnable() {
    
            @Override
            public void run() {
                while (progress < 100) {
                    progress += 1;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            spinner.setProgress(progress);
                            txtProgress.setText(progress + "%");
                        }
                    });
                    try {
                        Thread.sleep(28);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return view;
      }
    }
    
    0 讨论(0)
  • 2020-11-28 09:59

    For me worked if I set setVisibility() to visible before each setProgress()

    setVisibility(View.VISIBLE);
    setProgress(progress);
    
    0 讨论(0)
  • 2020-11-28 10:00

    I have tried in many ways by calling setProgress in Thread, runOnUiThread, Handler, Runnable. All of them dont work.

    So finally, set value of dialog.setIndeterminate(false); will work.

    Once you set dialog.setIndeterminate(true); the progress WONT UPDATE AT ALL.

    public static ProgressDialog createProgressDialog(Activity activity, int messageID, boolean cancelable) {
        ProgressDialog dialog = new ProgressDialog(activity, R.style.DialogButtonTint);
        dialog.setMessage(activity.getString(messageID));
        dialog.setIndeterminate(false);
        dialog.setMax(100);
        dialog.setSecondaryProgress(0);
        dialog.setProgress(0);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setCancelable(cancelable);
        dialog.show();
        return dialog;
    }
    
    0 讨论(0)
  • 2020-11-28 10:04

    Nothing here worked for me, so I've came with a bit different solution. I noticed that everything is working well until I try to change maximal value (calling setMax with different value).

    My code:

    class FixedSeekBar : SeekBar {
    
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        companion object {
            private const val PROGRESS_MAX = 1000000.toDouble()
        }
    
        private var mLastMaxValue: Int = 100
    
        override fun setMax(max: Int) {
            mLastMaxValue = max
            super.setMax(PROGRESS_MAX.toInt())
        }
    
        fun setProgressFixed(progress: Int) {
            setProgress((PROGRESS_MAX / mLastMaxValue * progress).toInt())
        }
    
        fun getProgressFixed(): Int {
            return (getProgress() / PROGRESS_MAX * mLastMaxValue).toInt()
        }
    
    }
    

    This works perfectly for me. I can call setMax as usually and I just need to replace getProgress and setProgress with getProgressFixed and setProgressFixed.

    Do not override setProgress and getProgress. They are called internally.

    My solution doesn't count with setMin. If you need it, change the code accordingly.

    0 讨论(0)
提交回复
热议问题