android - ViewPager with PagerAdapter/Fragments does not show anything -
in fragmentactivity on android below sdk 23, many fragments added transaction. inside of 1 of these shouldt appear simple viewpager few images. "images fragment" creates pageradapter custom array list. in case below, no view rendered within viewpager. no other threads , research helped me. trimed down code here, maybe can help. identations: wrong context, hidden content, false view return, ive found nothing , tried lot. instantiateitem() called, no destroy , remove methods called , empty widget red background shown. ive tried functions update list or rerender pager. original code not use image ressources there textview too!
main activity xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="mw.imageslidertest.mainactivity" android:id="@+id/fragmentcontainer"> </relativelayout>
mainactivity.java
package mw.imageslidertest; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmenttransaction; import android.os.bundle; public class mainactivity extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if(findviewbyid(r.id.fragmentcontainer) != null) { fragment container = new images(); container.setarguments(getintent().getextras()); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.add(r.id.fragmentcontainer, container); transaction.addtobackstack(null); transaction.commit(); } } }
the fragment named "images" xml:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="medianetwork24.imageslidertest.images"> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="300dp" android:layout_height="300dp" android:visibility="visible" android:layout_gravity="center_horizontal" android:background="#ff0000" /> </framelayout>
images.java
package mw.imageslidertest; import android.content.context; import android.os.bundle; import android.os.parcelable; import android.support.v4.app.fragment; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import java.util.arraylist; public class images extends fragment { class myimages { int resource; public myimages() { this.resource = 0; } } arraylist<myimages> imagesarray = new arraylist<>(); public images() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view layout = inflater.inflate(r.layout.fragment_images, container, false); myimages animage = new myimages(); animage.resource = r.mipmap.ic_launcher; imagesarray.add(animage); myimages anotherimage = new myimages(); anotherimage.resource = r.mipmap.ic_launcher; imagesarray.add(anotherimage); viewpager mpager = (viewpager) layout.findviewbyid(r.id.pager); imageframeadapter mimageframeadapter = new imageframeadapter(layout.getcontext(), imagesarray); mpager.setadapter(mimageframeadapter); return layout; } public class imageframeadapter extends pageradapter { private final layoutinflater minflater; private arraylist<myimages> images; public imageframeadapter(context context, arraylist<myimages> images) { super(); log.d("list", "create imageframeadapter"); this.images = images; minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); } @override public int getcount() { return images.size(); } @override public object instantiateitem(viewgroup view, int position) { log.d("list", "create pager page instance " + view); view imagelayout = minflater.inflate(r.layout.elements, view, false); // assert imagelayout != null; final imageview imageview = (imageview) imagelayout.findviewbyid(r.id.image); myimages res = images.get(position); imageview.setimageresource(res.resource); view.addview(imagelayout, 0); log.d("list", "created view: " + view); return view; } @override public boolean isviewfromobject(view view, object object) { return view.equals(object); } @override public void restorestate(parcelable state, classloader loader) { } @override public parcelable savestate() { return null; } } }
layout elements:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="300dp" android:padding="1dip" > <imageview android:id="@+id/image" android:layout_width="300dp" android:layout_height="300dp" android:adjustviewbounds="true" android:layout_gravity="center" android:src="@mipmap/ic_launcher" android:scaletype="centercrop" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="heellloeooeifoisdpoidopidopsiodpip" android:id="@+id/textviewid" android:layout_gravity="center" /> </framelayout>
only red background of pager widget visible. nothing else, not textview neither images. thx
Comments
Post a Comment