Send a picture first to let you know what I'm talking about:
Before that, I wrote a FlipView for Xamarin.form (hereinafter referred to as XF), which was simulated with Horizontalscrollview under Android.
Last week, inadvertently saw how the Viewpager to achieve the infinite loop method, so the elusive under Xamarin to realize it.
Under the XF, you can see that the large image loaded from the network shows very smooth, because under the XF, Uriimagesource this image source has so two attributes:
cachevalidity How long the cache takes
cachingenabled whether to enable caching
This means that under the XF, the Android-like LRUCache or disklrucache have been encapsulated, and note that this is only possible under the XF.
At first don't understand this stem, in the Android project (not the XF project) have encountered these several problems:
1, the network picture cannot be loaded on the main thread
Do not understand the reason, the data found that after Android 4.0 (not verified), the main thread is not allowed to block.
Loading pictures directly from the network is obviously blocked, so running is definitely no result waiting until it crashes.
2, OOM
This problem, for Android developers, in the ordinary but. The full name is out of memory, which is RAM overflow.
The workaround for OOM is to use the cache, which is the above mentioned LRUCache (available in this Android SDK) and Disklrucache (not in the SDK, but with the source code).
The LRUCache is put into memory, and when the application is closed, the cached data is lost.
Disklrucache is the "hard disk" (memory) that holds the file to the phone.
3, Adapter data source is IEnumerable
Custom Adapter, if the data source is a IEnumerable, if passed to Adapter, there is no ToList, in the execution, will continue to call this IEnumerable generation function.
Like this:
1 PrivateIenumerable<av. View>getchildrenviews () {2 foreach(varVinch This. Element.children) {3 varRender =Rendererfactory.getrenderer (v);4 varc =NewAW. Framelayout ( This. Context);5 C.setbackgroundcolor (Color.Blue.ToAndroid ());6 C.addview (render. ViewGroup, Layoutparams.matchparent, layoutparams.matchparent);7 yield returnC;8 }9}
If you do not ToList the result of this function into Adapter, the foreach block will always be called.
4, System.IO.IOException:Sharing violation on path XXX
This problem is reported because of the use of Mono's IsolatedStorage, if you do not specify BufferSize in Stream.copytoasync, you will report this error ,
Oddly, on the company's computer if the designation of buffersize, there is no such error, on the home computer refers to the non-designated buffersize will report the mistake.
But put a try. Catch.. , the file can be written successfully (from the content of the debug output, the second run, will not load the image from the network).
Do I have a problem with my spelling?
1 Public AsyncTask Writestream (stringfile, Stream stm) {2 varPath = This. GetPath (file);3 4 Try {5 using(varFS =ISF. OpenFile (Path, FileMode.OpenOrCreate, FileAccess.Write)) {6 //await STM. Copytoasync (FS); If the buff size is not specified, an error is given 7 awaitStm. Copytoasync (FS,16384);8 }9}Catch {Ten One } A}
5, how to achieve infinite loop
This part is referred to others (sources can not find), in layman's terms, is this:
A, the Count property of Adapter Returns an integer of the number of real data bars, if I have only 3 data, I will cheat Adapter, I have 6.
B, when determining which data to display, get the actual data entry that corresponds to the position. Use this position to find the entries for the remainder of the real data.
That is, in the Instantiateitem method of Adapter, use position%= this. Items.Count () to remove the actual data to be displayed. If there are 3 data, the current position is 5, actually to show is the 2nd data.
C, the Finishupdate method of Adapter.
1 Public Override voidfinishupdate (ViewGroup container) {2 varVP = Container asViewpager;3 varpos =VP. CurrentItem;4 5 if(pos = =0) {6pos = This. Items.Count ();/////////////7}Else if(pos = = This. Count-1) {8pos = This. Items.Count ()-1;///////////////9 }Ten //System.Diagnostics.Debug.WriteLine ("====> pos {0}", POS); OneVp. Setcurrentitem (POS,false); A}
If the true number of bars is 3, the count of Adapter is set to 6, this will be the case:
0 1 2 0 1 2
If the current POS is 0, set the POS to the 4th position on the 0, i.e. the No. 0 bit of the second round (the subscript starts with 0).
If POS is 5 (subscript starting from 0), the POS is set to the last of the first round, that is, the first 2.
-----------------
OK, on these, as usual, on the source:
Direct Android Project: Https://github.com/gruan01/Xamarin-Example/tree/master/FlipView
For Xamarin.form: Https://github.com/gruan01/FlipView
In addition, I did not realize the online spread of the Disklrucache, but with IsolatedStorage to achieve a simple. It is quite possible to hide other problems.
Xamarin realizes Android Infinite loop display, FlipView