繁体   English   中英

导航抽屉在片段之间移动

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM