1

I am replacing a NavigationView with a Fragment, and working further with the view. (Since NavigationView is a child of FrameLayout)

When clicking on any item in NavView .. I overlay the NavView with a fragment. If someone clicks back button .. I want to come back to the NavView.

When replacing the NavigationView with a Fragment, I have put a toolbar.

Problem

a. The toolbar shows a 'back' link - But it doesn't work.

b. The toolbar shows the 'Settings' menu - I do NOT want this on the Fragment. But .. i still want the 'Settings' menu on the MainActivity toolbar.

c. The toolbar seems to underlap with the StatusBar - How do i put the toolbar beneath the StatusBar, in the Fragment.

Options tried

a. OnBackPressed method of MainActivity - fragmentManager.popUpStack()

b. onOptionsItemSelected() method of Fragment -fragmentManager.popUpStack()

c. Created a listener on Toolbar, in onClick method : fragmentManager.popUpStack()

Screenshot: enter image description here enter image description here

Here is my code :

MainActivity:> onNavigationItemSelected()

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    ItemsFragment fragment = new ItemsFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.nav_view, fragment);
//                fragmentTransaction.setTransition();
    fragmentTransaction.addToBackStack("LeftMainNavDrawer");
    fragmentTransaction.commit();

//        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//        drawer.closeDrawer(GravityCompat.START);
    return true;
}

Fragment

package com.example.deep_kulshreshtha.expandablelistnavdrawer;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
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.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummyContent;
import com.example.deep_kulshreshtha.expandablelistnavdrawer.dummy.DummyContent.DummyItem;

import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
 * interface.
 */
public class ItemsFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private OnListFragmentInteractionListener mListener;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemsFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static ItemsFragment newInstance(int columnCount) {
        ItemsFragment fragment = new ItemsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }

        setHasOptionsMenu(false);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_item_list, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);

        // Set the adapter
//        if (view instanceof RecyclerView) {
            Context context = view.getContext();
//            RecyclerView recyclerView = (RecyclerView) view;

            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }

            recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
//        }

        Toolbar toolbar = (Toolbar) view.findViewById(R.id.itemsToolbarNavDrawer);
        /*toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Inside Toolbar click", Toast.LENGTH_LONG).show();
            }
        });*/
        AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
        appCompatActivity.setSupportActionBar(toolbar);
        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        appCompatActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.removeItem(R.id.action_settings);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(DummyItem item);
    }
}

FragmentLayout file

<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/itemsToolbarNavDrawer"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v7.widget.RecyclerView

        android:id="@+id/list"
        android:name="com.example.deep_kulshreshtha.expandablelistnavdrawer.ItemsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.example.deep_kulshreshtha.expandablelistnavdrawer.ItemsFragment"
        tools:listitem="@layout/fragment_item"
        android:background="@android:color/white"/>

</LinearLayout>
Deep
  • 673
  • 1
  • 10
  • 23

2 Answers2

0

In your main activity add 1 frame layout and give it id as container. then in your java code

fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.main_container,new HomeFragment());
        fragmentTransaction.commit();

using fragment transaction replace that container with one frament which you want to show as default on app start as home.

Then use this container in each fragment to replace

Rahul
  • 73
  • 10
  • idea is to replace the NavigationView with a Fragment - This is working. What is not working is .... back button from Fragment to NavigationView. – Deep Oct 19 '16 at 04:40
  • fragmentTransaction.addToBackStack("null"); – Rahul Oct 19 '16 at 04:47
  • Yes, it doesn't help. What I want to understand is ..... if we press a back button from a Fragment -> can we go to the original FrameLayout ?? – Deep Oct 19 '16 at 04:50
  • Yes we can, but i did this by different way. Its on mainactivity. I gave the empty container and whenever i perform transaction i was replacing container with fragment which i want. – Rahul Oct 19 '16 at 05:14
0

I figured out one solution .. looks like the 'Toolbar' class has one instance for the whole application.

When I used the onOptionsItemSelected method of the Activity ... I get the control. Here I am able to popUpStack() and go to the NavigationView.

#

Yet to figure out how to keep the Layout beneath the StatusBar. : android:fitsSystemWindows="true" ... does NOT seem to be working.

Deep
  • 673
  • 1
  • 10
  • 23