android - ViewPager does not show content -
i trying create viewpager
shows list of imageviews. able implement pager , adapter properly, , works expected on swiping. however, not show content of imageview
properly, i.e. image itself. if images there, transparent. 1 image visible out of whole lot.
here code-
imagepageradapter.java
public class imagepageradapter extends pageradapter { layoutinflater inflater; list<imageview> views = new arraylist<>(); boolean[] done = {false,false,false,false,false}; itemdetailfragment idf; public imagepageradapter(itemdetailfragment idf, layoutinflater inflater) { this.idf = idf; this.inflater = inflater; (int = 0; < getcount(); i++) { imageview iv = new imageview(idf.getactivity()); iv.setimageresource(r.drawable.light_grey_background); views.add(iv); } } public imagepageradapter(layoutinflater inflater) { this.inflater = inflater; (int = 0; < getcount(); i++) { imageview iv = new imageview(inflater.getcontext()); iv.setimageresource(r.drawable.light_grey_background); views.add(iv); } } @override public object instantiateitem(viewgroup container, int position) { if (done[position]) { return views.get(position); } imageview v = views.get(position); views.set(position, v); ((viewpager) container).addview(v); done[position] = true; return v; } @override public void destroyitem(viewgroup container, int position, object object) { } @override public int getcount() { return 5; } @override public boolean isviewfromobject(view view, object object) { return true; } }
itemdetailfragment.java : set adapter.
public class itemdetailfragment extends fragment { viewpager pager; imagepageradapter adapter; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_item_detail, container, false); pager = (viewpager) rootview.findviewbyid(r.id.pager); adapter = new imagepageradaper(this, inflater); pager.setadapter(adapter); }
fragment_item_detail.xml
<linearlayout 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:orientation="vertical" android:padding="16dp" tools:context="com.trial.piclist.itemdetailfragment" > <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="200dip" > </android.support.v4.view.viewpager> <tablerow android:id="@+id/tablerow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/item_title" style="?android:attr/textappearancelarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:textisselectable="true" /> <space android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /> <include android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/controls_layout" /> </tablerow> <scrollview android:id="@+id/descscrollview" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:id="@+id/item_detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" /> </linearlayout> </scrollview> </linearlayout>
please help. thanks.
use:
public class imagepageradapter extends pageradapter { layoutinflater inflater; activity act; int count; public viewpageradapter(activity a, int c) { act = a; count=c; inflater = (layoutinflater) act .getsystemservice(context.layout_inflater_service); } @override public int getcount() { return count; } @override public object instantiateitem(view collection, int position) { view v1 = inflater.inflate(r.layout.element_image, null); imageview image = (imageview) v1 .findviewbyid(r.id.about_image); image.setimageresource(r.drawable.light_grey_background); ((viewpager) collection).addview(v1, 0); return v1; } @override public void destroyitem(view arg0, int arg1, object arg2) { ((viewpager) arg0).removeview((view) arg2); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == ((view) arg1); } @override public parcelable savestate() { return null; } }
make layout element_image.xml:
<?xml version="1.0" encoding="utf-8"?> <imageview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/smv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
replace
adapter = new imagepageradaper(this, inflater);
with
adapter = new imagepageradaper(getactivity(), count_of_images);
Comments
Post a Comment