android - Two toolbars are visible in my fragment -
i have been working on navigation drawer
using toolbar
, while clicking on drawer items
, respective fragments
displayed,but here problem,when ever clicking drawer items
,fragments
2 toolbars
displayed.please help.
fragment.xml
<framelayout 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" tools:context="archerpenny.impdrawerfragment.blankfragment"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <!-- todo: update blank fragment layout --> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </linearlayout> </framelayout> activity_main.xml.. `<android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar" xmlns:android="http://schemas.android.com/apk/res/android"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <framelayout android:id="@+id/container_body" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="hi"/> </framelayout> </linearlayout> <android.support.v7.widget.recyclerview android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/drawerlist" android:layout_margintop="?android:attr/actionbarsize" android:background="@mipmap/menu_bg" android:layout_gravity="left"/> </android.support.v4.widget.drawerlayout> mainactivity.java.... ` import android.app.fragment; import android.app.fragmentmanager; 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.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.motionevent; import android.view.view; public class mainactivity extends appcompatactivity { actionbardrawertoggle mdrawertoggle; recyclerview.adapter madapter; recyclerview recyclerview; drawerlayout mdrawerlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); madapter = new navigationdraweradapter(this); mdrawerlayout=(drawerlayout)findviewbyid(r.id.drawer_layout); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); navigationdraweradapter adapter; recyclerview = (recyclerview)findviewbyid(r.id.drawerlist); recyclerview.sethasfixedsize(true); linearlayoutmanager llm = new linearlayoutmanager(this); recyclerview.setlayoutmanager(llm); recyclerview.setadapter(madapter); getsupportactionbar().setdisplayshowtitleenabled(false); mdrawertoggle = new actionbardrawertoggle(this,mdrawerlayout,toolbar,r.string.open,r.string.close) { @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); } @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); getsupportactionbar().sethomebuttonenabled(true); getsupportactionbar().setdisplayhomeasupenabled(true); recyclerview.addonitemtouchlistener( new recycleritemclicklistener(mainactivity.this, new recycleritemclicklistener.onitemclicklistener() { @override public void onitemclick(view view, int position) { // whatever if(position==0) { blankfragment blankfragment=new blankfragment(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, blankfragment) .commit(); } mdrawerlayout.closedrawers(); } }) ); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); mdrawertoggle.syncstate(); } @override public boolean onoptionsitemselected(menuitem item) { if (mdrawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } } blankfragment.java import android.os.bundle; import android.app.fragment; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class blankfragment extends fragment { // todo: rename parameter arguments, choose names match // fragment initialization parameters, e.g. arg_item_number // todo: rename , change types , number of parameters public static blankfragment newinstance(string param1, string param2) { blankfragment fragment = new blankfragment(); bundle args = new bundle(); return fragment; } public blankfragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { appcompatactivity activity = (appcompatactivity) getactivity(); view view=inflater.inflate(r.layout.fragment_blank, container, false); toolbar toolbar = (toolbar) view.findviewbyid(r.id.toolbar); activity.setsupportactionbar(toolbar); activity.getsupportactionbar().setdisplayhomeasupenabled(true); // inflate layout fragment return view; } }
remove include statement in fragment
<framelayout 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" tools:context="archerpenny.impdrawerfragment.blankfragment"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- todo: update blank fragment layout --> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </linearlayout>
Comments
Post a Comment