I'm trying to implement an开发者_C百科droid-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
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.
精彩评论