我正在尝试在片段中实现导航抽屉,但是当我在FeedFragment.java中实现NavigationListener时遇到问题,请你帮我解决问题并帮助我了解实现监听器和工具栏的过程片段活动。我已经添加了下面我用来实现它的所有代码结构。甚至附加了截图。非常感谢提前。
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;
}
}
答案 0 :(得分:1)
为什么不让你的基本活动实现NavigationView.OnNavigationItemSelectedListener
然后添加这些调用
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);
所以你不需要从片段中实现它。 您只需替换或添加片段。