Reputation: 25
I am trying to implement navigation drawer inside the fragment, but I am facing problem when I am implementing the NavigationListener in FeedFragment.java, can you please help me to solve the problem and help me knowing the procedure implementing listener and toolbar inside the fragment activity. I have added all the code structure below which I have used to implement it.and even attached screenshot. Many a thanks in advance.
here is the screenshot which I am implementing
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sirajm.boom.MainActivity">
<include layout="@layout/content_main" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
content_main.xml
<FrameLayout android:id="@+id/content_panel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
fragment_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_fragment" />
</android.support.design.widget.CoordinatorLayout>
content_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello Blank Fragment" />
</LinearLayout>
MainActivity.java
package com.example.sirajm.boom;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.sirajm.boom.Fragments.FeedFragment;
import com.example.sirajm.boom.Fragments.twoFragment;
import com.example.sirajm.boom.Fragments.threeFragment;
import com.example.sirajm.boom.Fragments.fourFragment;
import com.example.sirajm.boom.Fragments.fiveFragment;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new FeedFragment()).commit();
return true;
case R.id.navigation_store:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new twoFragment()).commit();
return true;
case R.id.navigation_orders:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new threeFragment()).commit();
return true;
case R.id.navigation_service:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new fourFragment()).commit();
return true;
case R.id.navigation_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.content_panel,new fiveFragment()).commit();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getSupportFragmentManager().beginTransaction().add(R.id.content_panel,new FeedFragment()).commit();
}
}
FeedFragment.java
package com.example.sirajm.boom.Fragments;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.sirajm.boom.R;
public class FeedFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {
View rootView;
public FeedFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_feed, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) getActivity());
return rootView;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_event:
Toast.makeText(getContext(), "ev1", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_news:
Toast.makeText(getContext(), "ev2", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_offers:
Toast.makeText(getContext(), "ev3", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_problems:
Toast.makeText(getContext(), "ev4", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_talents:
Toast.makeText(getContext(), "ev5", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_shortmov:
Toast.makeText(getContext(), "ev6", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_share:
Toast.makeText(getContext(), "ev7", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_send:
Toast.makeText(getContext(), "Send", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
Toast.makeText(getContext(), "logout", Toast.LENGTH_SHORT).show();
break;
}
DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Upvotes: 1
Views: 1744
Reputation: 21
Why don't you make your base activity implements NavigationView.OnNavigationItemSelectedListener
Then add these those calls
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
So you don't need to implement it from fragment. You just replace or add the fragments.
Upvotes: 1