[英]navigation drawer move between fragments
我將主活動設置為導航抽屜活動的應用程序遇到問題,
我得到的是,當我按導航列表中的某個項目時,該片段不會被相應的片段替換。
替換代碼如下:
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文件如下:
<?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" />
height屬性將占用片段中的所有空間,而框架布局中包含的片段未顯示。 可以通過將match_parent更改為wrap_content來進行修改,並且可以解決。
還是謝謝你們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.