gpt4 book ai didi

java - 进度条 setVisibility VISIBLE

转载 作者:行者123 更新时间:2023-12-01 17:32:01 24 4
gpt4 key购买 nike

我是 JAVA 新手,所以我试图在程序员列表渲染时显示进度条可见,但进度条始终可见

公共(public)类ListActivity扩展AppCompatActivity { 私有(private)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();
});
}

}

最佳答案

onCreate 中删除 progressBar.setVisibility(View.INVISIBLE); 并在 getProgrammers 的回调中进行隐藏:

repository.getProgrammers(programmers -> {
this.programmers.clear();
this.programmers.addAll(programmers);
this.programmersListAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE); // hide progressBar after data is loaded
});

我还建议使用View.GONE而不是View.INVISIBLE因此进度条在不可见时不会占用任何空间。

关于java - 进度条 setVisibility VISIBLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61102221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com