显示大表而不阻塞 UI 线程
我有一个要显示大约 1k 行的表。此任务显然会阻塞 UI 线程,导致 onCreate() 构建表时出现黑屏。
我通过使用 AsyncTask 解决了这个问题,它在“doInBackground”函数中构建所需的 TableLayout 并将其显示在“onPostExecute”函数上。
问题#1: 有没有我不熟悉的更好的做法?
问题#2: 我的(简化的)“doInBackground”函数如下所示:
protected Void doInBackground(Void... v) {
tmpTableLayout = populateTable("");
return null;
}
我的(简化的)“onPostExecute”函数如下所示:
protected void onPostExecute(Void v) {
TableLayout ct = (TableLayout)findViewById(R.id.RealTable);
ct.removeAllViews();
/* WHATS HERE? */
}
我应该写什么而不是“这里是什么?”在最后一行代码中以便在“ct”中显示“tmpTableLayout”的内容?
提前致谢!
I have a table with about 1k rows that I want to display. This task obviously chokes the UI thread, resulting in a black screen while the onCreate() builds the table.
I've solved this by using AsyncTask which builds the wanted TableLayout in the "doInBackground" function and display it on the "onPostExecute" function.
Question #1:
Is there any better practice that I'm not familiar with?
Question #2:
My (simplified) "doInBackground" function looks like this:
protected Void doInBackground(Void... v) {
tmpTableLayout = populateTable("");
return null;
}
And my (simplified) "onPostExecute" function looks like this:
protected void onPostExecute(Void v) {
TableLayout ct = (TableLayout)findViewById(R.id.RealTable);
ct.removeAllViews();
/* WHATS HERE? */
}
What should I write instead of the "WHATS HERE?" in the last line of code in order to display the content of "tmpTableLayout" in "ct" ?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定要一次性显示所有内容吗?
一种方法是当用户向下滚动时动态加载更多行。因此,有一个滚动侦听器来检查用户是否接近显示内容的末尾,从而启动 AsyncTask 或加载更多内容的线程。
例子:
具有动态加载图像表单的 Android 列表 Activity Android 中的网络
Are you sure you want to display it all in one go?
One approach would be to dynamically load in more lines as the user scrolls down. So have a scroll listener that checks if the user is approaching the end of the content that is displayed and therefore start an AsyncTask or a thread loading more content.
Example:
Android List Activity with dynamically loaded images form the web in android
我可能会使用
ListView
和CursorAdapter
并让 Android 为您管理获取数据。请参阅此处接受的答案。I would probably use a
ListView
andCursorAdapter
and let Android manage fetching the data for you. See the accepted answer here.