导航抽屉和碎片

时间:2016-06-13 14:40:13

标签: android android-fragments navigation-drawer

我使用助手而不是教程创建了一个NavDrawer Activity。

现在,当我想要打开一个新片段时,它不会替换主要内容,但prepends it.文本字段应该会消失。

我想我正在尝试更换错误的容器。但为什么它是错误的,哪个是正确的

ReadActivity.java

public class ReadActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //more code...

    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.setNavigationItemSelectedListener(this);

    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_read) {
            // Handle the camera action
        } else if (id == R.id.nav_settings) {
//the probably wrong 
            getFragmentManager().beginTransaction()
                    .replace(R.id.content, new SettingFragment)
                    .commit();
        } else {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

activity_read.xml

<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
    android:id="@id/content"
    layout="@layout/app_bar_read"
    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_read"
    app:menu="@menu/activity_read_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_read.xml

<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:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.max.speedread.ReadActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_read" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_menu_paste" />

</android.support.design.widget.CoordinatorLayout>

content_read.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.max.speedread.ReadActivity"
    tools:showIn="@layout/app_bar_read">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:minHeight="200dp" />
</RelativeLayout>

SettingsFragment.java

public class SettingFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the prefrences from XML resource
        addPreferencesFromResource(R.xml.prefrences);
    }
}

prefrences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="foobar">

        <EditTextPreference
                android:key="checkbox_prefrence"
                android:title="asdf"
                android:summary="fdsa"
                android:dialogTitle="asdf"/>

    </PreferenceCategory>

</PreferenceScreen>

我希望我没有添加太多代码:)

1 个答案:

答案 0 :(得分:0)

您的代码一切正常,偏好设置屏幕只是在您的旧布局上绘制。

解决方法是: 在你的content_read

使用FrameLayout交换RelativeLayout(这不是必需的,您只需将新的framelayout添加为相对布局的子框架),这将是您的片段持有者

应该看起来像这样&gt;

<强> content_read.xml

  <FrameLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/container"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="202dp"
        android:gravity="center"
        android:text="JUST A RANDOM TEXT INSIDE CONTAINER WHICH WILL BE REPLACED BY SETTINGS FRAGMENT"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</FrameLayout>

注意FrameLayout的id为 R.id.container

下一步创建settings_screen_layout,它应该看起来像这样

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@color/colorPrimary"
                  android:orientation="vertical">

        <ListView android:id="@android:id/list"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent" />
    </LinearLayout>

请注意,您需要一个带有“@android:id / list”的列表视图,它将作为您的偏好的容器

在你的SettingFragment Override onCreateView中,像这样

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        addPreferencesFromResource(R.xml.prefrences);
        View view = inflater.inflate(R.layout.preference_screen_layout, container, false);


        return view;
    }

并执行交易

 getFragmentManager().beginTransaction()
                    .replace(R.id.container, new SettingFragment)
                    .commit();