android - PageTransformer with infinite viewpager -
i've been combining tablettransformer
(use source code here) infinite view pager looping.
results not expected. has no compiling error, blank page happened when swipe between pages.
main activity
public class mainactivity extends appcompatactivity { public viewpager mpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); // initialize viewpager mpager = (viewpager) findviewbyid(r.id.container); infiniteviewpageradapter adapter = new infiniteviewpageradapter(getsupportfragmentmanager()); list<fragment> pagerfragments = createpagerfragments(); adapter.setpagerfragments(pagerfragments); mpager.setadapter(adapter); mpager.setpagetransformer(true, new tablettransformer()); mpager.setcurrentitem(1); } private list<fragment> createpagerfragments() { list<fragment> datalist = new linkedlist<fragment>(); datalist.add(0, firstfragment.newinstance("first page")); datalist.add(1, secondfragment.newinstance("main page")); datalist.add(2, thirdfragment.newinstance("third page")); datalist.add(3, fourthfragment.newinstance("fourth page")); return datalist; } }
activity_main.xml
<android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context="com.example.wlwong.tablettransformer.mainactivity"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/apptheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:popuptheme="@style/apptheme.popupoverlay" /> </android.support.design.widget.appbarlayout> <com.example.wlwong.tablettransformer.infiniteviewpager.infiniteviewpager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.design.widget.coordinatorlayout>
i called 2 classes implement infinite view-pager loop : infiniteviewpager , infiniteviewpageradapter
infiniteviewpager class
public class infiniteviewpager extends viewpager { private onpagechangelistener externalonpagechangelistener = null; public infiniteviewpager(context context) { super(context); setactualonpagechangelistener(new pagechangelistener(this)); } public infiniteviewpager(context context, attributeset attrs) { super(context, attrs); setactualonpagechangelistener(new pagechangelistener(this)); } private void setactualonpagechangelistener(onpagechangelistener listener) { super.setonpagechangelistener(listener); } @override public void setonpagechangelistener(onpagechangelistener listener) { this.externalonpagechangelistener = listener; } private onpagechangelistener getexternalonpagechangelistener() { return this.externalonpagechangelistener; } private static class pagechangelistener implements onpagechangelistener { private final infiniteviewpager viewpager; public pagechangelistener(final infiniteviewpager viewpager) { this.viewpager = viewpager; } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { final onpagechangelistener externalonpagechangelistener = viewpager.getexternalonpagechangelistener(); if (externalonpagechangelistener != null) { externalonpagechangelistener.onpagescrolled( position, positionoffset, positionoffsetpixels); } } @override public void onpageselected(int position) { final onpagechangelistener externalonpagechangelistener = viewpager.getexternalonpagechangelistener(); if (externalonpagechangelistener != null) { externalonpagechangelistener.onpageselected(position); } infiniteviewpageradapter adapter = (infiniteviewpageradapter) viewpager.getadapter(); list<fragment> pagerfragments = adapter.getpagerfragments(); // ensure cycling occurs if there 3 or more fragments. if (pagerfragments.size() > 2) { final int cycleresult = cyclepagerfragments(pagerfragments, position); if (cycleresult != 0) { adapter.setpagerfragments(pagerfragments); adapter.notifydatasetchanged(); // turn off actual , external onpagechangelisteners, // function not unnecessarily called again when // setting current item. viewpager.setonpagechangelistener(null); viewpager.setactualonpagechangelistener(null); viewpager.setcurrentitem(position + cycleresult, false); viewpager.setonpagechangelistener(externalonpagechangelistener); viewpager.setactualonpagechangelistener(this); } } } @override public void onpagescrollstatechanged(int state) { final onpagechangelistener externalonpagechangelistener = viewpager.getexternalonpagechangelistener(); if (externalonpagechangelistener != null) { externalonpagechangelistener.onpagescrollstatechanged(state); } } } private static int cyclepagerfragments(list<fragment> pagerfragments, final int position) { final int lastposition = pagerfragments.size() - 1; if (position == lastposition) { pagerfragments.add(pagerfragments.remove(0)); return -1; } else if (position == 0) { pagerfragments.add(0, pagerfragments.remove(lastposition)); return 1; } return 0; } }
infiniteviewpageradapter class
public class infiniteviewpageradapter extends fragmentstatepageradapter { private list<fragment> pagerfragments; public infiniteviewpageradapter(fragmentmanager fm) { super(fm); } public list<fragment> getpagerfragments() { return pagerfragments; } public void setpagerfragments(final list<fragment> pagerfragments) { this.pagerfragments = pagerfragments; } @override public fragment getitem(int position) { return (pagerfragments != null && pagerfragments.size() > position) ? pagerfragments.get(position) : null; } @override public int getcount() { return (pagerfragments != null) ? pagerfragments.size() : 0; } @override public int getitemposition(object object) { return position_none; } }
i've tried failed find caused issues. or combining tablettransformer
, infinite view-pager
impossible work?
Comments
Post a Comment