导航抽屉在片段之间移动
navigation drawer move between fragments
我在将主要 activity 设置为导航抽屉 activity、
时制作的应用程序出现问题
我得到的是,当我按下导航列表中的一个项目时,片段没有被相应的片段替换。
替换代码如下:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment objFragment = null;
if (id == R.id.nav_dashboard) {
objFragment = new Dashboard_fragment();
} else if (id == R.id.nav_myschedule){
objFragment = new Schedule_fragment();
} else if (id == R.id.nav_rides) {
objFragment = new Rides_fragment();
} else if (id == R.id.nav_vouchers) {
objFragment = new Vouchers_fragment();
} else if (id == R.id.nav_help) {
objFragment = new Help_fragment();
} else if (id == R.id.nav_settings) {
objFragment = new Settings_fragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, objFragment)
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
其中每个片段如下:
package com.example.user.greenmiles;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Dashboard_fragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Dashboard_fragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class Dashboard_fragment extends Fragment {
private OnFragmentInteractionListener mListener;
public static Dashboard_fragment newInstance() {
Dashboard_fragment fragment = new Dashboard_fragment();
return fragment;
}
public Dashboard_fragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
getActivity().setTitle("Dashboard");
return inflater.inflate(R.layout.fragment_dashboard_fragment, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@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 OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
而xml文件对应的主要activity文件如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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" />
与片段对应的xml文件如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.greenmiles.Dashboard_fragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Dashboard"
android:textSize="16pt" />
拜托我已经卡在这几天没能解决了。
非常感谢
我终于找到了答案,它与编码无关,而是与 xml 文件有关。问题具体在于以下块:
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
高度 属性 占用了片段中的所有 space,而框架布局中包含的片段未显示。修改一下,把match_parent改成wrap_content就可以解决了。
谢谢大家
我在将主要 activity 设置为导航抽屉 activity、
时制作的应用程序出现问题
我得到的是,当我按下导航列表中的一个项目时,片段没有被相应的片段替换。
替换代码如下:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment objFragment = null;
if (id == R.id.nav_dashboard) {
objFragment = new Dashboard_fragment();
} else if (id == R.id.nav_myschedule){
objFragment = new Schedule_fragment();
} else if (id == R.id.nav_rides) {
objFragment = new Rides_fragment();
} else if (id == R.id.nav_vouchers) {
objFragment = new Vouchers_fragment();
} else if (id == R.id.nav_help) {
objFragment = new Help_fragment();
} else if (id == R.id.nav_settings) {
objFragment = new Settings_fragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, objFragment)
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
其中每个片段如下:
package com.example.user.greenmiles;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Dashboard_fragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Dashboard_fragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class Dashboard_fragment extends Fragment {
private OnFragmentInteractionListener mListener;
public static Dashboard_fragment newInstance() {
Dashboard_fragment fragment = new Dashboard_fragment();
return fragment;
}
public Dashboard_fragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
getActivity().setTitle("Dashboard");
return inflater.inflate(R.layout.fragment_dashboard_fragment, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@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 OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
而xml文件对应的主要activity文件如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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" />
与片段对应的xml文件如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.greenmiles.Dashboard_fragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Dashboard"
android:textSize="16pt" />
拜托我已经卡在这几天没能解决了。
非常感谢
我终于找到了答案,它与编码无关,而是与 xml 文件有关。问题具体在于以下块:
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
高度 属性 占用了片段中的所有 space,而框架布局中包含的片段未显示。修改一下,把match_parent改成wrap_content就可以解决了。
谢谢大家