Issue
I'm having trouble developing NavigationDrawer
+ Toolbar
menu. (All on Lollipop with appcompat v21)
The problem is that when I use the NavigationDrawer
and the Toolbar
in my main_activity, does not respond to the listeners.
Is it a problem of sintax or anything else?
This is my project code:
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.Toolbar;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private ActionBarDrawerToggle toogle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.tlb_toolbar);
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
if (item.getItemId()==R.id.info)
{
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new fragment_2())
.addToBackStack("1")
.commit();
toolbar.getMenu().clear();
toolbar.setLogo(null);
toolbar.setTitle("F.A.Q");
toolbar.inflateMenu(R.menu.menu_second);
return true;
}
if (item.getItemId()==R.id.back) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new BlankFragment())
.addToBackStack("2")
.commit();
toolbar.getMenu().clear();
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(R.string.app_name);
toolbar.inflateMenu(R.menu.menu_main);
return true;
}
if (item.getItemId()==R.id.mes)
{
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new fragment_3())
.addToBackStack("3")
.commit();
toolbar.getMenu().clear();
toolbar.setTitle("Fragments2");
toolbar.inflateMenu(R.menu.menu_second);
}
return false;
}
});
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(R.string.app_name);
toolbar.inflateMenu(R.menu.menu_main);
toogle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open,R.string.close);
toogle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(toogle);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BlankFragment())
.commit();
}
}
}
I am using Fragment
s in the layout. I have read there is cert of glitch problems with the Fragment
s into Toolbar
+ NavigationDrawer
layout, is there something I am missing? what can I do?
Main Layout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar_tb" />
<FrameLayout
android:layout_below="@+id/tlb_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
/>
<!-- MAIN CONTENT -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame5"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
<ListView
android:id="@+id/lv_menuoverflow"
android:layout_width="260dp"
android:layout_gravity="start"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#2E2E2E"
android:dividerHeight="2dp"
android:background="#ffffff"
android:textColor="#424242"
android:entries="@array/planetes"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Thak you in advance.
P.S: Sorry for my english.
Solution
Your layout seems to be using a bunch of containers that are unnecessary. They might be blocking the views, ultimately consuming the clicks in that area.
Try this layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar_tb"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<ListView
android:id="@+id/lv_menuoverflow"
android:layout_width="260dp"
android:layout_gravity="start"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#2E2E2E"
android:dividerHeight="2dp"
android:background="#ffffff"
android:textColor="#424242"/>
</android.support.v4.widget.DrawerLayout>
Answered By - Simas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.