我正在创建一个程序,当它在后台执行某些操作时会显示进度条(使用AsyncTask
),当任务完成时,进度条会显示,并显示结果。
我在RecyclerView
中添加了一个进度条,但设置可见性已消失。
在AsyncTask PreExecute中,我将其可见性设置为VISIBLE
,并在OnPostExecute
中将其可见性设置为GONE
但是当我运行应用程序时,进度条根本不显示。
我已经包含了XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/rv_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTint="@color/plus_icon_color"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/train_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
和Java代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView mTrainsRv;
private TrainAdapter mTrainAdapter;
ArrayList<Train> trains = new ArrayList<>();
ProgressBar mRvProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRvProgress = (ProgressBar)findViewById(R.id.rv_progress_bar);
mTrainsRv = (RecyclerView) findViewById(R.id.train_rv);
mTrainsRv.setLayoutManager(new LinearLayoutManager(this));
mTrainAdapter = new TrainAdapter(createDefaultTrains());
mTrainsRv.setAdapter(mTrainAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
trains.add(new Train(4, "Berowra Platform 4", 12, "Late", "Beverly", "15:18"));
mTrainAdapter.notifyDataSetChanged();
}
});
}
private List<Train> createDefaultTrains() {
trains.add(new Train(0, "Albion Park Platform 1", 3, "On Time", "Allawah", "14:11"));
trains.add(new Train(1, "Arncliffe Platform 2", 4, "Late", "Central", "14:34"));
trains.add(new Train(2, "Artarmion Platform 3", 7, "On Time", "Ashfield", "15:01"));
trains.add(new Train(3, "Berowra Platform 4", 12, "Late", "Beverly", "15:18"));
return trains;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// Handle item selection
switch (item.getItemId()) {
case R.id.action_delete:
trains.clear();
mTrainAdapter.notifyDataSetChanged();
return true;
case R.id.action_refresh:
new RefreshProgressAsync().execute();
return true;
case R.id.action_quit:
Toast.makeText(this, R.string.quit_menu,
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class RefreshProgressAsync extends AsyncTask<Void,Void,Integer> {
private int mGenerated;
@Override
protected Integer doInBackground(Void... voids) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int ran = 0;
Random r = new Random();
for(int i=0; i<trains.size(); i++) {
int min = 1;
int max = 20;
ran = r.nextInt(max - min + 1) + min;
mGenerated = ran;
}
return mGenerated;
}
@Override
protected void onPreExecute() {
mRvProgress.setVisibility(View.VISIBLE);
mTrainsRv.setAlpha(0);
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer arrivalTime) {
super.onPostExecute(arrivalTime);
for (int i=0; i<trains.size();i++){
trains.get(i).setArrivalTime(arrivalTime);
}
mTrainAdapter.notifyDataSetChanged();
mTrainsRv.setAlpha(1);
mRvProgress.setVisibility(View.GONE);
}
}
我不知道为什么它没有显示出来。 感谢
答案 0 :(得分:1)
表现如预期。 ContentLoadingProgressBar
的目的是仅在必要时显示进度。如果操作需要很少的时间,则不会显示进度条。这是为了避免进度条的闪烁效果,快速连续显示和消失。
如果你想让它出于测试目的(我不建议在生产代码中),那么将你的睡眠时间增加到10秒:
@Override
protected Integer doInBackground(Void... voids) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
....
}
或者,您可以使用普通ProgressBar