android - How to switch between fragments through a navigation drawer? -
i have basic question related navigation drawer. have made app has navigation drawer, have no idea how change view on selection of item in nav drawer, can display toast on every click, means issue in switching fragments during runtime.
this main code:
import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.content.res.configuration; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.gesturedetector; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view; import android.widget.adapterview; import android.widget.linearlayout; import android.widget.listview; import android.widget.toast; public class mainactivity extends actionbaractivity{ string titles[] = {"home","search","recent logs","my wallet","settings","logout"}; int icons[] = {r.drawable.ic_action,r.drawable.ic_action_search,r.drawable.ic_action_logs, r.drawable.ic_action_wallet,r.drawable.ic_action_settings,r.drawable.ic_action_logout}; string name = "akash bangad"; string email = "akash.bangad@android4devs.com"; int profile = r.drawable.default_user; toolbar toolbar; recyclerview mrecyclerview; recyclerview.adapter madapter; recyclerview.layoutmanager mlayoutmanager; drawerlayout drawer; actionbardrawertoggle mdrawertoggle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.tool_bar); setsupportactionbar(toolbar); mrecyclerview = (recyclerview) findviewbyid(r.id.recyclerview); mrecyclerview.sethasfixedsize(true); madapter = new myadapter(titles,icons,name,email,profile); mrecyclerview.setadapter(madapter); //detect gestures final gesturedetector mgesturedetector = new gesturedetector(mainactivity.this, new gesturedetector.simpleongesturelistener() { @override public boolean onsingletapup(motionevent e) { return true; } }); //listen clicks on navigation drawer mrecyclerview.addonitemtouchlistener(new recyclerview.onitemtouchlistener() { @override public boolean onintercepttouchevent(recyclerview recyclerview, motionevent motionevent) { view child = recyclerview.findchildviewunder(motionevent.getx(),motionevent.gety()); if(child!=null && mgesturedetector.ontouchevent(motionevent) && recyclerview.getchildposition(child)>0){ drawer.closedrawers(); toast.maketext(mainactivity.this,titles[recyclerview.getchildposition(child)-1], toast.length_short).show(); selectitem(recyclerview.getchildposition(child)-1); fragment fragment; fragmentmanager fragmentmanager = getfragmentmanager(); // appcompat use getsupportfragmentmanager switch(recyclerview.getchildposition(child)) { default: case 1: fragment = new profileinfofragment(); break; } fragmentmanager.begintransaction() .replace(r.id.mainlayout, fragment) .commit(); return true; } return false; } @override public void ontouchevent(recyclerview recyclerview, motionevent motionevent) { } }); mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); drawer = (drawerlayout) findviewbyid(r.id.drawerlayout); mdrawertoggle = new actionbardrawertoggle(this,drawer,toolbar,r.string.opendrawer,r.string.closedrawer){ @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); } @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); } }; drawer.setdrawerlistener(mdrawertoggle); getsupportactionbar().sethomebuttonenabled(true); getsupportactionbar().setdisplayhomeasupenabled(true); mdrawertoggle.syncstate(); } public void selectitem(int i) { getsupportactionbar().settitle(titles[i]); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } else if(mdrawertoggle.onoptionsitemselected(item)){ return true; } return super.onoptionsitemselected(item); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:elevation="7dp"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/mainlayout"> <include android:id="@+id/tool_bar" layout="@layout/tool_bar"> </include> </linearlayout> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ffffff" android:scrollbars="vertical"> </android.support.v7.widget.recyclerview>
now have created simple fragment , want app switch on fragment when clicked on item in navigation drawer. replacing mainlayout fragment giving me classcastexception.
process: com.pocketcash.pocketcash, pid: 2456 java.lang.classcastexception: com.pocketcash.pocketcash.mainactivity@c08ff73 @ com.pocketcash.pocketcash.profileinfofragment.onattach(profileinfofragment.java:31) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:853) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.app.backstackrecord.run(backstackrecord.java:833) @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1452) @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java:447) @ 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:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
if use actionbaractivity
, should use classes support library: android.support.v4.app.fragmentmanager
, android.support.v4.app.fragment
instead of default ones.
so have replace getfragmentmanager()
getsupportfragmentmanager()
, import android.app.fragment;
import android.support.v4.app.fragment;
.
go through document link
Comments
Post a Comment