如何正确从 SQLite 数据库填充 android-viewFlow
我正在尝试实现 android-viewFlow (https://github.com/pakerfeldt/android-viewflow) 但是,在我的应用程序中,我很难正确填充每个页面。
问题是我有 SQLite 数据库,并且需要有多个视图来匹配与我的查询匹配的行数(通常在 9 到 21 之间),并且每个视图都应该显示该行的值。
在 viewFlow 中,有一个“sidebuffer”字段,它将根据该“sidebuffer”的值加载视图数量。那么,如何从 SQLite 填充适配器并强制“侧缓冲区”匹配我的行数。 (如果“sidebuffer = 3”,则加载的视图将为2 * 3 + 1,如果为5,则将加载2 * 5 + 1)。
即使我永远不会显示那么多视图,硬编码“sidebuffer = 100”是否有缺点?
我使用 viewFlow 而不是 Android 的兼容性 viewPager 的原因是我需要实现的标题指示器。
提前致谢!
现在,我在 viewFlow.java 中创建了一个新方法
public setSidebuffer(int sidebuffer) {
mSideBuffer = sidebuffer;
init();
}
,并在我设置 viewFlow 的 main_activity 中创建了一个新方法,如下所示:
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
AndroidVersionAdapter adapter = new AndroidVersionAdapter(this);
viewFlow.setAdapter(adapter, 3);
viewFlow.setSidebuffer(adabter.getSize() / 2); // THIS IS THE THING
I'm trying to implement android-viewFlow (https://github.com/pakerfeldt/android-viewflow) in my app, however, I'm having hard time populating each page correctly.
The problem is that I have SQLite database, and I need to have a number of views that will match the number of rows matching my query (typically between 9 and 21), and each of those views should show values from that exact row.
In viewFlow, there's this "sidebuffer" field that will load the number of views depending on what's the value of this "sidebuffer". So, how do I populate the adapter from SQLite and force the "sidebuffer" to match my number of rows. (If "sidebuffer = 3", the views loaded will be 2 * 3 + 1, and if it's 5, then it will load 2 * 5 + 1).
Is there a downside of having "sidebuffer = 100" hard-coded even if I will never have even near that much of views to be shown?
The reason I'm using viewFlow, and not the Android's compatibility viewPager, is that I need the title indicator which is implemented.
Thanks in advance!
Right now, I've created a new method in viewFlow.java
public setSidebuffer(int sidebuffer) {
mSideBuffer = sidebuffer;
init();
}
and in my main_activity where I'm setting up the viewFlow I'm calling the method as follows:
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
AndroidVersionAdapter adapter = new AndroidVersionAdapter(this);
viewFlow.setAdapter(adapter, 3);
viewFlow.setSidebuffer(adabter.getSize() / 2); // THIS IS THE THING
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我担心设置一个大于适配器中元素数量的侧缓冲区可能会导致 FC。在这种情况下,我必须调查 android-viewflow 中的一个错误。无论如何,如此大的侧缓冲区将导致您的适配器为每个元素创建一个视图,从而消耗(可能会大量)内存。适配器的整个想法是仅在需要时加载视图。
我认为当您打算将完整的缓冲区大小设置为适配器的大小时,您正在从错误的角度解决问题。这不是它的用途。以这样的方式实现适配器,当调用 getView(...) 时,适配器将从数据库中检索正确的行,在必要时膨胀视图,并返回填充的视图。如果此过程相对较快,您可能会坚持使用较低的侧缓冲区,例如 2 或 3,因为用户滚动的速度不会快于将视图加载到视图流中所需的速度。这有道理吗?
如果您更喜欢使用 ViewPager,您可能需要查看 https://github.com /JakeWharton/Android-ViewPagerIndicator
该库源自 android-viewflow 中为启用 ViewPager 指示器而完成的工作。也许 android-viewflow 将来也会支持这一点。
I'm afraid that setting a sidebuffer greater than the number of elements in your adapter might cause a FC. In that case, there's a bug in android-viewflow I'll have to look into. Anyway, such large sidebuffer will cause your adapter to create a View for each and every element, thus consuming, potentially a lot, memory. The whole idea with the adapter is to only load Views when needed.
I think you're approaching the problem from the wrong angle when you're aiming on setting the complete buffer size to the size of your adapter. That's not how it is meant to be used. Implement your adapter in such way that when getView(...) is called, the adapter will retrieve the correct row from your database, inflate a view if necessary, and return the populated view. If this procedure is relatively fast you may stick with a low sidebuffer such as 2 or 3, because a user won't be able to scroll faster than it takes to load views into viewflow. Does that make sense?
If you rather like to use the ViewPager instead, you might want to have a look at https://github.com/JakeWharton/Android-ViewPagerIndicator
This library is derived from work done in android-viewflow to enable indicators for ViewPager. Perhaps android-viewflow will support that as well in the future.