progressBar的可见性设置为VISIBLE。

huangapple go评论80阅读模式
英文:

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&lt;Programmer&gt; programmers = new ArrayList&lt;&gt;();
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&#39;s important not to change reference of the list provided here. That&#39;s why it&#39;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&#39;t know that there&#39;s been update to it&#39;s data
this.programmersListAdapter.notifyDataSetChanged();
repository.getProgrammers(programmers -&gt; {
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 -&gt; {
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.

huangapple
  • 本文由 发表于 2020年4月8日 21:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61102221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定