我花了一个小时尝试将“加载更多”Button
和不确定的 ProgressBar
添加到我的 ListView
的页脚。
假设的场景是这样的:
单击按钮时,会显示 ProgressBar
,同时 AsyncTask
正在下载 20 个项目。当项目插入到 ListView
中时,ProgressBar
消失,Button
再次出现在页脚中。
我得出以下解决方案,如果您有更好的解决方案,那就太好了:
布局/progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
/>
假设您的 Activity 中有以下字段:
private ListView listview;
private Button loadMoreButton;
private ProgressBar progressBar;
准备好 ListView 后,添加如下页脚(在 activity.onCreate()
的末尾):
loadMoreButton = new Button(this);
loadMoreButton.setText("Load More");
progressBar = (ProgressBar) LayoutInflater.from(this).inflate(R.layout.progress_bar, null);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//downloadItems();
listview.removeFooterView(loadMoreButton);
listview.addFooterView(progressBar);
}
});
当数据准备好(第一页或后续页面)时,在适配器更新后调用以下命令。
//adapter.notifyDataSetChanged();
listview.removeFooterView(progressBar);
listview.addFooterView(loadMoreButton);
如果您有更好的解决方案,请在答案中分享。
最佳答案
您可以在同一个页脚 View 中同时包含按钮和进度条,并根据要求使可见性可见和可见性消失,而不是删除和添加 View 作为页脚。
关于java - 带有加载更多按钮和圆形进度条的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075508/