英文:
progressBar setVisibility VISIBLE
问题
我是一个新手在JAVA方面,所以我正在尝试在程序员列表将被渲染时显示进度条,但进度条始终可见。
public class ListActivity extends AppCompatActivity {
private Repository repository;
private final List<Programmer> programmers = new ArrayList<>();
private ListView listView;
private ProgressBar progressBar;
private ProgrammersListAdapter programmersListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = findViewById(R.id.listView);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
repository = CombinedRepository.getInstance();
// 重要的是不要在这里更改提供的列表的引用。这就是为什么它是final的!
programmersListAdapter = new ProgrammersListAdapter(this, programmers);
listView.setAdapter(programmersListAdapter);
refreshProgrammers();
}
void refreshProgrammers(){
// 待办事项:在这里更改进度条的可见性
progressBar.setVisibility(View.VISIBLE);
this.programmers.clear();
// 没有notifyDataSetChanged - ListView将不会知道其数据已更新
this.programmersListAdapter.notifyDataSetChanged();
repository.getProgrammers(programmers -> {
this.programmers.clear();
this.programmers.addAll(programmers);
this.programmersListAdapter.notifyDataSetChanged();
});
}
}
英文:
I am new at JAVA so i'm trying to show progressbar visible when programmers list will render but progressBar is visible all the time
public class ListActivity extends AppCompatActivity {
private Repository repository;
private final List<Programmer> programmers = new ArrayList<>();
private ListView listView;
private ProgressBar progressBar;
private ProgrammersListAdapter programmersListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = findViewById(R.id.listView);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
repository = CombinedRepository.getInstance();
// It's important not to change reference of the list provided here. That's why it's final!
programmersListAdapter = new ProgrammersListAdapter(this, programmers);
listView.setAdapter(programmersListAdapter);
refreshProgrammers();
}
void refreshProgrammers(){
// ToDO: Change Progress bar visibility here
progressBar.setVisibility(View.VISIBLE);
this.programmers.clear();
// Without notifyDataSetChanged - Listview won't know that there's been update to it's data
this.programmersListAdapter.notifyDataSetChanged();
repository.getProgrammers(programmers -> {
this.programmers.clear();
this.programmers.addAll(programmers);
this.programmersListAdapter.notifyDataSetChanged();
});
}
}
答案1
得分: 0
从onCreate
中移除progressBar.setVisibility(View.INVISIBLE);
,并在getProgrammers
的回调中进行隐藏:
repository.getProgrammers(programmers -> {
this.programmers.clear();
this.programmers.addAll(programmers);
this.programmersListAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE); // 在数据加载完成后隐藏 progressBar
});
我还建议使用View.GONE
,而不是View.INVISIBLE
,这样当不可见时,progressBar不会占用任何空间。
英文:
Remove progressBar.setVisibility(View.INVISIBLE);
from onCreate
and do the hiding in the callback to getProgrammers
:
repository.getProgrammers(programmers -> {
this.programmers.clear();
this.programmers.addAll(programmers);
this.programmersListAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE); // hide progressBar after data is loaded
});
I also suggest to use View.GONE
instead of View.INVISIBLE
so the progressBar won't take up any space when invisible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论