c# - ViewPager/PagerAdapter shows all views on first page -
what's happening
viewpager in app shows views instantiated on page one. if instantiate 2 views example, both views shown in page one, , second page blank.
images showing problem
can see below, page 1 contains both pages , page 2 empty.
i can't figure out why happening. have been struggling long time now. tried not add view adapter, adding view , providing index, not inflating creating imageview
in adapter, etc.
what missing? appreciated!
code
activity
[activity(label = "foto's", screenorientation = screenorientation.portrait)] public class picturesactivity : baseactivity { private imagepageradapter _imageadapter; private list<string> _filepaths; private viewpager _viewpager; private const int _maxpictures = 5; protected override void oncreate(bundle bundle) { base.oncreate(bundle); setcontentview(resource.layout.pictures); _viewpager = findviewbyid<viewpager>(resource.id.viewpager); } protected override void onstart() { base.onstart(); _imageadapter = new imagepageradapter(this, _filepaths); _viewpager.adapter = _imageadapter; _viewpager.setcurrentitem(0, false); } }
pageradapter
internal class imagepageradapter : pageradapter { activity _activity; list<string> _filepaths; public imagepageradapter(activity activity, list<string> filepaths) { _activity = activity; _filepaths = filepaths; } public override java.lang.object instantiateitem(viewgroup container, int position) { var view = _activity.layoutinflater.inflate(resource.layout.picturepageitem, container, false); var imageview = view.findviewbyid<imageview>(resource.id.imageview); var filepath = _filepaths[position]; displaymetrics metrics = _activity.resources.displaymetrics; using (var bitmap = bitmaphelper.getresizedbitmap(filepath, new size(metrics.widthpixels, metrics.heightpixels))) imageview.setimagebitmap(bitmap); container.javacast<viewpager>().addview(view); return view; } public override void destroyitem(viewgroup container, int position, java.lang.object @object) { var view = (linearlayout)@object; var imageview = view.findviewbyid<imageview>(resource.id.imageview); if (imageview != null && imageview.drawable != null) { imageview.drawable.dispose(); imageview.setimagebitmap(null); } container.javacast<viewpager>().removeview(view); } public override int count { { return _filepaths.count; } } public override bool isviewfromobject(view view, java.lang.object @object) { return view linearlayout; } }
pictures layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
picturepageitem layout (adapter page layout)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="centerinside" /> </linearlayout>
i think problem may due method isviewfromobject().
this method called compare view key object. version returning true viewpager thinks views on same page.
you should modify code this:
public override bool isviewfromobject(view view, java.lang.object @object) { return (object)view == @object; }
for more details, see:
http://developer.android.com/reference/android/support/v4/view/pageradapter.html
Comments
Post a Comment