这是我的第一个问题(社区,谢谢!)
所以,我有这个问题:
我正在开发带有标签布局的android应用程序。每当我导航到“状态”选项卡时,我都想显示一个根据变量值更新的循环ProgressBar。我们将此变量称为prog。此外,我想在ProgressBar的中心显示一个TextView,它显示与prog相同的值。
我想出了这部分代码:
public class StatusFragment extends Fragment
{
private ProgressBar torqueRefProgressBar;
private TextView torqueRefTextView;
RunningThreadExample r;
private Thread t1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Inflate the layout for this fragment
return ...;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
// Initializations and other parts not relevant for the question...
this.torqueRefProgressBar = getView().findViewById(R.id.torqueRefProgressBar);
this.torqueRefTextView = getView().findViewById(R.id.torqueRefTextView);
t1 = new Thread( this.torqueRefProgressBar, this.torqueRefTextView)); // passing arguments by value but values are references to the objects, therefore I'm passing by ref
t1.start();
}
}
在此[[Fragment中,我创建ProgressBar和TextView,然后将它们传递给Runnable,如下所示)>public class RunningThreadExample implements Runnable
{
ProgressBar torqueRefProgBar_thread;
TextView torqueRefTextView_thread;
public RunningThreadExample(ProgressBar progressBar, TextView textView)
{
this.torqueRefProgBar_thread = progressBar;
this.torqueRefTextView_thread = textView;
this.endScale = this.torqueRefProgBar_thread.getMax();
}
@Override
public void run()
{
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
float i = 0;
double temp = 0;
while(Running)
{
try
{
Thread.sleep(1000/60); // update freq 60Hz
}
catch (InterruptedException e)
{
e.printStackTrace();
}
temp = getCurrentVariableValue(); // Not shown here (returns a double)
this.torqueRefProgBar_thread.setProgress((int) temp);
this.torqueRefTextView_thread.setText(String.valueOf(temp));
}
Log.i(INFO_TAG,"Finishing thread!!!!!");
}
}
我希望此更新在应用运行期间一直运行(因此,我忽略了[[AsyncTask))。一段时间后,ProgressBar停止更新,而TextView继续正常工作而没有任何问题。
我一直在阅读,可能的原因可能是调用setProgress(temp)
可能卡住了UI。但是当TextView仍在工作时,我听不懂。您能否建议一种更好的方式来更新progressBar?
谢谢!这是我的第一个堆栈问题(伟大的社区,谢谢!)因此,我遇到了这个问题:我正在开发带有选项卡布局的android应用程序。每当我导航到“状态”标签时,我都想...