Android TabLayout inside Fragment -
i using navigation drawer
navigate between fragments use mainactivity toolbar each fragment has it's own tablayout
3 tabs , in each tab using recyclerview
show different text , images.
i did last time using actvities instead of fragments using drawer it's beter use fragments.
i have got point tabs work put viewpager
in , connect adapters tabs , open new fragment drawer null pointer problem here?
mainactivity.java
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); return id == r.id.action_settings || super.onoptionsitemselected(item); } @override public boolean onnavigationitemselected(menuitem item) { int id = item.getitemid(); fragment fragment; if (id == r.id.fragment_one) { fragment = new newfragment(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.container, fragment); ft.commit(); } else if (id == r.id.fragment_two) { fragment = new newfragment(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.container, fragment); ft.commit(); } else if (id == r.id.fragment_three) { fragment = new newfragment(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.container, fragment); ft.commit(); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; } }
activity_main.xml
<android.support.v4.widget.drawerlayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <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.xcandy.guideforfifa17.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> <include layout="@layout/content_main" /> </android.support.design.widget.coordinatorlayout> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout>
newfragment.java
public class newfragment extends fragment { private recyclerview mrecyclerview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.activity_fragment_one, container, false); tablayout tablayout = (tablayout) view.findviewbyid(r.id.tabs); tablayout.addtab(tablayout.newtab().settext("a")); tablayout.addtab(tablayout.newtab().settext("b")); tablayout.addtab(tablayout.newtab().settext("c")); final viewpager viewpager = (viewpager) view.findviewbyid(r.id.viewpager); mrecyclerview = (recyclerview) view.findviewbyid(r.id.recycler_view); recyclerview.layoutmanager mlayoutmanager = new linearlayoutmanager(getactivity()); mrecyclerview.setlayoutmanager(mlayoutmanager); viewpager.setadapter(new pageradapter (getfragmentmanager(), tablayout.gettabcount())); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); return view; } public class pageradapter extends fragmentstatepageradapter { int mnumoftabs; public pageradapter(fragmentmanager fm, int numoftabs) { super(fm); this.mnumoftabs = numoftabs; } @override public fragment getitem(int position) { switch (position) { case 0: recyclerview.adapter madapter = new adapterone(); mrecyclerview.setadapter(madapter); case 1: madapter = new adaptertwo(); mrecyclerview.setadapter(madapter); case 2: madapter = new adapterthree(); mrecyclerview.setadapter(madapter); default: return null; } } @override public int getcount() { return mnumoftabs; } } }
activity_fragment.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=".newfragment"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/apptheme.appbaroverlay"> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabmode="fixed" app:tabgravity="fill"/> </android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"/> <android.support.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.design.widget.coordinatorlayout>
error log
--------- beginning of crash 06-23 11:19:56.985 2466-2466/com.candyx.sims e/androidruntime: fatal exception: main process: com.candyx.sims, pid: 2466 java.lang.nullpointerexception: attempt invoke virtual method 'void android.support.v7.widget.recyclerview.setlayoutmanager(android.support.v7.widget.recyclerview$layoutmanager)' on null object reference @ com.candyx.sims.blankfragment.oncreateview(blankfragment.java:38) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1252) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:742) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:517) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 06-23 11:23:14.912 5839-5873/com.candyx.sims d/openglrenderer: use egl_swap_behavior_preserved: true [ 06-23 11:23:14.920 5839: 5839 d/ ] hostconnection::get() new host connection established 0xb4125190, tid 5839 06-23 11:23:14.927 5839-5839/com.candyx.sims d/atlas: validating map... 06-23 11:23:15.011 5839-5873/com.candyx.sims i/openglrenderer: initialized egl, version 1.4 06-23 11:23:15.027 5839-5873/com.candyx.sims d/openglrenderer: enabling debug mode 0 06-23 11:23:15.039 5839-5873/com.candyx.sims w/egl_emulation: eglsurfaceattrib not implemented 06-23 11:23:15.039 5839-5873/com.candyx.sims w/openglrenderer: failed set egl_swap_behavior on surface 0xae838600, error=egl_success 06-23 11:23:16.444 5839-5839/com.candyx.sims w/pathparser: points far apart 4.000000596046461 06-23 11:23:17.342 5839-5839/com.candyx.sims w/pathparser: points far apart 4.000000596046461 06-23 11:23:17.350 5839-5839/com.candyx.sims i/appcompatviewinflater: app:theme deprecated. please move using android:theme instead. 06-23 11:23:17.369 5839-5839/com.candyx.sims d/androidruntime: shutting down vm 06-23 11:23:17.369 5839-5839/com.candyx.sims e/androidruntime: fatal exception: main process: com.candyx.sims, pid: 5839 java.lang.nullpointerexception: attempt invoke virtual method 'void android.support.v7.widget.recyclerview.setlayoutmanager(android.support.v7.widget.recyclerview$layoutmanager)' on null object reference @ com.candyx.sims.newfragment.oncreateview(newfragment.java:38) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1252) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:742) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:517) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 06-23 11:25:16.961 7518-7518/com.candyx.sims w/art: before android 4.1, method android.graphics.porterduffcolorfilter android.support.graphics.drawable.vectordrawablecompat.updatetintfilter(android.graphics.porterduffcolorfilter, android.content.res.colorstatelist, android.graphics.porterduff$mode) have incorrectly overridden package-private method in android.graphics.drawable.drawable 06-23 11:25:17.078 7518-7548/com.candyx.sims d/openglrenderer: use egl_swap_behavior_preserved: true [ 06-23 11:25:17.081 7518: 7518 d/ ] hostconnection::get() new host connection established 0xb412a1f0, tid 7518 06-23 11:25:17.082 7518-7518/com.candyx.sims d/atlas: validating map... 06-23 11:25:17.109 7518-7548/com.candyx.sims i/openglrenderer: initialized egl, version 1.4 06-23 11:25:17.126 7518-7548/com.candyx.sims d/openglrenderer: enabling debug mode 0 06-23 11:25:17.134 7518-7548/com.candyx.sims w/egl_emulation: eglsurfaceattrib not implemented 06-23 11:25:17.134 7518-7548/com.candyx.sims w/openglrenderer: failed set egl_swap_behavior on surface 0xb41234c0, error=egl_success 06-23 11:25:31.030 7518-7518/com.candyx.sims w/pathparser: points far apart 4.000000596046461 06-23 11:25:32.174 7518-7518/com.candyx.sims w/pathparser: points far apart 4.000000596046461 06-23 11:25:32.187 7518-7518/com.candyx.sims i/appcompatviewinflater: app:theme deprecated. please move using android:theme instead. 06-23 11:25:32.207 7518-7518/com.candyx.sims d/androidruntime: shutting down vm 06-23 11:25:32.207 7518-7518/com.candyx.sims e/androidruntime: fatal exception: main process: com.candyx.sims, pid: 7518 java.lang.nullpointerexception: attempt invoke virtual method 'void android.support.v7.widget.recyclerview.setlayoutmanager(android.support.v7.widget.recyclerview$layoutmanager)' on null object reference @ com.candyx.sims.newfragment.oncreateview(newfragment.java:38) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1252) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:742) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:517) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
after looking yours code, found forget return fragment in getitem method of pageradapter overrided fragmentstatepageradapter , there no break statement in each case due every time null value returned.
to doing need create new fragment each page in viewpager or can create 1 new fragment use in page of viewpager.
you can't use same recyclerview tabs of view pager have defined in parent fragment.
activity_fragment_one.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=".newfragment"> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/apptheme.appbaroverlay"> <android.support.design.widget.tablayout android:id="@+id/tabs" android:layout_width="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:tabtextappearance="?android:attr/textappearancemedium" app:tabtextcolor="#ffffff" android:layout_height="?android:actionbarsize" app:tabmode="fixed" app:tabgravity="fill"/> </android.support.design.widget.appbarlayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:background="@android:color/white"/> </android.support.design.widget.coordinatorlayout>
i have fixed fragment , created new fragment can use in viewpager pages.
public class newfragment extends fragment { private recyclerview mrecyclerview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.activity_fragment_one, container, false); tablayout tablayout = (tablayout) view.findviewbyid(r.id.tabs); tablayout.addtab(tablayout.newtab().settext("a")); tablayout.addtab(tablayout.newtab().settext("b")); tablayout.addtab(tablayout.newtab().settext("c")); final viewpager viewpager = (viewpager) view.findviewbyid(r.id.viewpager); // mrecyclerview = (recyclerview) view.findviewbyid(r.id.recycler_view); linearlayoutmanager mlayoutmanager = new linearlayoutmanager(getactivity()); mlayoutmanager.setorientation(linearlayoutmanager.vertical); //mrecyclerview.setlayoutmanager(mlayoutmanager); viewpager.setadapter(new pageradapter(getfragmentmanager(), tablayout.gettabcount())); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.setupwithviewpager(viewpager); tablayout.settabmode(tablayout.mode_scrollable); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); return view; } public class pageradapter extends fragmentstatepageradapter { int mnumoftabs; public pageradapter(fragmentmanager fm, int numoftabs) { super(fm); this.mnumoftabs = numoftabs; } @override public fragment getitem(int position) { switch (position) { case 0: return new fragmenttab(); case 1: return new fragmenttab(); case 2: return new fragmenttab(); default: return null; } } @override public int getcount() { return mnumoftabs; } } }
fragmenttab each page in view pager (or can create different fragment each page according yours need).
public class fragmenttab extends fragment { recyclerview mrecyclerview; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_tab, container, false); mrecyclerview = (recyclerview) view.findviewbyid(r.id.recycler_view); linearlayoutmanager mlayoutmanager = new linearlayoutmanager(getactivity()); mlayoutmanager.setorientation(linearlayoutmanager.vertical); mrecyclerview.setlayoutmanager(mlayoutmanager); recyclerview.adapter madapter = new adapterone(); mrecyclerview.setadapter(madapter); return view; } }
fragment_tab.xml
<?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.v7.widget.recyclerview android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
Comments
Post a Comment