0

I recently managed to have my navigation drawer edited and responding clicks, but after one transition to another activity, navigation drawer is displayed but wont response to any further clicks. the only way to go back is by pressing android buikt in "back" button

Thanks

Ori Cohen
  • 19
  • 1
  • 5
  • post the relevant code here – Henry Oct 25 '15 at 05:45
  • You can put a fragment in the navigation drawer and handle the view and onClick events in one place or you have to set the click listener in activity on resume for the drawer. – Saeed Entezari Oct 25 '15 at 05:52
  • public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.graph) { Intent intent = new Intent(this, ShowWebChartActivity.class); startActivity(intent); – Ori Cohen Oct 25 '15 at 05:58
  • can you please elaborate\give example about fragment? – Ori Cohen Oct 25 '15 at 05:59

1 Answers1

0

In your activity layout you should add NavigationView to the drawerLayout as follows:

<android.support.v4.widget.DrawerLayout>
...
<android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        app:itemIconTint="#333"
        app:itemTextColor="#333">
        <fragment
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="fragments.MenuFragment"
            android:id="@+id/fragment"/>
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

In your MenuFragment layout you have something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <include
        android:id="@+id/header"
        layout="@layout/nav_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/menuRecyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"/>
</RelativeLayout>

And in the class of your fragment you can do this:

public class MenuFragment extends Fragment {
    RecyclerView recyclerView;

    ArrayList<MenuItem> menuItems = new ArrayList<>();
    private OnFragmentInteractionListener mListener;

    public MenuFragment() {
    }
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.menu_fragment, container, false);
recyclerView = root.findViewById(R.id.menuRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);

recyclerView.addOnItemTouchListener(...);//add the code to handle menu click...


return root;
}

EDIT

nav_header.xml is actually the header of the navigation view and is nothing special:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout2"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:padding="10dp">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:paddingBottom="20dp"
        android:src="@drawable/ic_white"/>

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/logo">

        <ImageView
            android:id="@+id/account"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="2dp"
            android:src="@drawable/ic_account_box_white_48dp"/>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_toLeftOf="@+id/account"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="UserName"
            android:textColor="#ffffff"/>
    </RelativeLayout>

</RelativeLayout>

The menuRecyclerView is just a recyclerView which is responsible for showing the menu items and you have to add the items to that (You can see this or this for getting started with recyclerView)

Once you added the items you are going to need to add the OnItemClickListener to the recycler view which you can learn here

And after all that is done you can start another activity like this:

Intent newActivity = new Intent(getActivity(), newActivity.class);
startActivity(newActivity);
Community
  • 1
  • 1
Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
  • thanks,my main activity already had NavigationView, but i dont understand the fragment xml ingredients:nav_header,menuRecyclerView. what are their purpose? plus, where do i implement switching activities when drawer button press? – Ori Cohen Oct 26 '15 at 06:04