java - Recyclerviews in ViewPager -
i'm trying working: want 2 recyclerviews in 1 viewpager. followed tutorial: http://android-java-development.blogspot.de/2012/05/system.html, doesn't seem work. looks view pager empty , recycler view doesn't show up.
here's layout code:
<android.support.v4.widget.swiperefreshlayout android:id="@+id/pager_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingtop="@dimen/tab_height"> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/accent"/> </android.support.v4.widget.swiperefreshlayout>
and pageadapter:
public class newspageradapter extends pageradapter { private context context; private vector<recyclerview> recyclerviewlist; private string[] titles; public newspageradapter(context context, int titlesid) { this.context = context; this.recyclerviewlist = new vector<>(); settitles(titlesid); } public void add(recyclerview recyclerview) { recyclerviewlist.add(recyclerview); } public recyclerview.adapter getadapterforviewatindex(int index) { return recyclerviewlist.get(index).getadapter(); } @override public object instantiateitem(viewgroup container, int position) { container.addview(recyclerviewlist.get(position), new recyclerview.layoutparams(viewgroup.layoutparams.match_parent, 100) ); return container; } @override public int getcount() { return recyclerviewlist.size(); } @override public boolean isviewfromobject(view view, object object) { return view.equals(object); } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); } @override public charsequence getpagetitle(int position) { return titles[position]; } public void settitles(int titles) { this.titles = context.getresources().getstringarray(titles); } }
and oncreatview method:
gridlayoutmanager layoutmanager1 = new gridlayoutmanager(getactivity(), getresources().getinteger(r.integer.news_column_count)); gridlayoutmanager layoutmanager2 = new gridlayoutmanager(getactivity(), getresources().getinteger(r.integer.news_column_count)); recyclerview listview1 = new recyclerview(getactivity()); recyclerview listview2 = new recyclerview(getactivity()); listview1.setlayoutmanager(layoutmanager1); listview2.setlayoutmanager(layoutmanager2); newsadapter adapter1 = new newsadapter(getactivity(), null); newsadapter adapter2 = new newsadapter(getactivity(), null); adapter1.setonitemclicklistener(this); adapter2.setonitemclicklistener(this); listview1.setadapter(adapter1); listview2.setadapter(adapter2); newspageradapter.add(listview1); newspageradapter.add(listview2); newsviewpager.setadapter(newspageradapter);
here i'm passing cursor object adapter:
@override public void onloadfinished(loader<cursor> loader, cursor data) { ((newsadapter) newspageradapter.getadapterforviewatindex(0)).swapcursor(data); }
you have extend fragmentpageradapter
or fragmentstatepageradapter
in order embed recyclerview. if going update viewpager contents during lifecycle strictly recommended use fragmentstatepageradapter
you have create additional fragment layout, containing recyclerview
.
if wish update viewpager
swiperefreshlayout
, don't wrap swiperefreshlayout
. instead, must have swiperefreshlayout
inside fragment layout.
therefore fragment may following xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.swiperefreshlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listrefresh" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:id="@+id/categorylist" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.swiperefreshlayout>
and create additional fragment class, inflate layout , implement methods, need update refresh indicator status.
a bit old example found here: http://developer.android.com/training/implementing-navigation/lateral.html
if wish connect viewpager new support library tablayout, 1 with:
tablayout.setupwithviewpager(viewpager);
finally, if update "fragmented" viewpager
, don't try reset adapter, fragments managed not adapter, fragmentmanager
. wiser update content of corresponding recyclerviews
public class myfragmentedpageradapter extends fragmentstatepageradapter { private final tablayout mtablayout; private final swiperefreshlayout.onrefreshlistener mrefreshlistener; private vector<pricelistfragment> fragmentlist; private vector<string> titles; public myfragmentedpageradapter(fragmentmanager fm, mycomplexdata data, oncartactionlistener listener, tablayout tablayout, swiperefreshlayout.onrefreshlistener refreshlistener) { super(fm); mtablayout = tablayout; // external refresh listener, trigger updatedata() mrefreshlistener = refreshlistener; fragmentlist = new vector<>(); titles = new vector<>(); updatedata(data); } public void updatedata(mycomplexdata data) { boolean updatetabs = false; boolean hasnewdata = false; vector<string> newtitles = new vector<>(); int position = 0; for(tabcontents tabcontents : data.gettabcontents()) { if(tabcontents.getlistcontents() == null) continue; hasnewdata = true; boolean isnewfragment; myfragment fragment; try { fragment = fragmentlist.get(position); isnewfragment = false; } catch (arrayindexoutofboundsexception e) { fragment = new myfragment(); isnewfragment = true; } // update data, title , hide update indicator of swiperefreshlayout fragment.settabcontents(tabcontents); newtitles.add(tabcontents.getname()); if(isnewfragment) { fragment.setrefreshlistener(mrefreshlistener); fragmentlist.add(fragment); } position++; } if(!hasnewdata) return; // need decide, whether update tabs if(titles.size() != newtitles.size()) { updatetabs = true; } else { for(position = 0; position < titles.size(); position++) { if(!titles.get(position).equals(newtitles.get(position))) { updatetabs = true; break; } } } titles = newtitles; notifydatasetchanged(); if(updatetabs) mtablayout.settabsfrompageradapter(this); } @override public fragment getitem(int position) { return fragmentlist.get(position); } // need override method @override public int getitemposition(object object) { myfragment fragment = (myfragment) object; string title = (string) fragment.gettitle(); int position = titles.indexof(title); if (position >= 0) { return position; } else { return position_none; } } @override public int getcount() { return titles.size(); } @override public charsequence getpagetitle(int position) { return fragmentlist.get(position).gettitle(); } }
your myfragment
class has implement gettitle()
method.
Comments
Post a Comment