在导航抽屉中的 OnClick 事件期间不会替换片段

Fragments get not replaced during OnClick event in navigation drawer

我在导航抽屉中创建了一个包含 5 个项目的菜单,这 5 个项目代表 5 个不同的片段,每次点击一个项目时它应该显示相关的片段。 再点击一个不同的项目,片段应该被替换等等。

我为片段创建了 5 个单独的 classes。

当我启动应用程序并第一次单击某个项目时,会显示相关片段,但在我单击菜单中的另一个项目后没有任何反应(片段未被替换),我也没有收到任何错误,并且应用程序不会崩溃。

我不知道问题出在哪里希望有人能帮忙,这是我的代码:

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private Toolbar toolbar;
private MyAdapter myAdapter;

public ListView mainMenuList;
public Fragment_Android fAndroid;
public Fragment_Latest_Releases fLatest;
public Fragment_My_Applications fMyapps;
public Fragment_Platforms fPlatforms;
public Fragment_Settings fSettings;
public android.support.v4.app.FragmentManager fragmanager;
public android.support.v4.app.FragmentTransaction fragtrans;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    mainMenuList = (ListView) findViewById(R.id.testList);
    myAdapter = new MyAdapter(this);
    mainMenuList.setAdapter(myAdapter);

    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp((R.id.fragment_navigation_drawer), (DrawerLayout)  findViewById(R.id.drawer_layout), toolbar);

    fAndroid = new Fragment_Android();
    fLatest = new Fragment_Latest_Releases();
    fMyapps = new Fragment_My_Applications();
    fPlatforms = new Fragment_Platforms();
    fSettings = new Fragment_Settings();

    mainMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {
                case 0: {
                    Toast.makeText(getApplicationContext(), "Android has been clicked.", Toast.LENGTH_SHORT).show();

                    fragmanager = getSupportFragmentManager();
                    fragtrans = fragmanager.beginTransaction();
                    fragtrans.replace(R.id.fragment_container, fAndroid);
                    fragtrans.commit();
                    drawer.closeDrawers();

                    break;
                }
                case 1: {
                    Toast.makeText(getApplicationContext(), "Latest Releases has been clicked.", Toast.LENGTH_SHORT).show();

                    fragmanager = getSupportFragmentManager();
                    fragtrans = fragmanager.beginTransaction();
                    fragtrans.replace(R.id.fragment_container, fLatest);
                    fragtrans.commit();
                    drawer.closeDrawers();

                    break;
                }
                case 2: {
                    Toast.makeText(getApplicationContext(), "Platforms has been clicked.", Toast.LENGTH_SHORT).show();

                    fragmanager = getSupportFragmentManager();
                    fragtrans = fragmanager.beginTransaction();
                    fragtrans.replace(R.id.fragment_container, fPlatforms);
                    fragtrans.commit();
                    drawer.closeDrawers();

                    break;
                }
                case 3: {
                    Toast.makeText(getApplicationContext(), "My Applications has been clicked.", Toast.LENGTH_SHORT).show();

                    fragmanager = getSupportFragmentManager();
                    fragtrans = fragmanager.beginTransaction();
                    fragtrans.replace(R.id.fragment_container, fMyapps);
                    fragtrans.commit();
                    drawer.closeDrawers();

                    break;
                }
                case 4: {
                    Toast.makeText(getApplicationContext(), "Settings has been clicked.",  Toast.LENGTH_SHORT).show();

                    fragmanager = getSupportFragmentManager();
                    fragtrans = fragmanager.beginTransaction();
                    fragtrans.replace(R.id.fragment_container, fSettings);
                    fragtrans.commit();
                    drawer.closeDrawers();

                    break;
                }

            }
        }
    });

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Toast.makeText(this, "Just hit settings.." + item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

示例 1 class(在本例中 Android):

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_Android extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {

    View fragandroid = inflater.inflate(R.layout.layout_android, null);

    return fragandroid;

}

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

1 个片段示例 xml(Android 片段):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:text="Android"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

我的主要activityxml:

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="android.kadas.org.teka.MainActivity">

<include
    android:id="@+id/my_toolbar"
    layout="@layout/app_bar" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="android.kadas.org.teka.NavigationDrawerFragment"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

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

在点击监听器中,您实际上是用所需的片段替换 activity 的整个布局。

  1. 在您的主要 activity 的 xml 中添加以下布局, RelativeLayout 中:

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
  2. 改变你的

    fragtrans.replace(R.id.drawer_layout, fragmentX);
    

    的陈述
    fragtrans.replace(R.id.container, fragmentX);
    

在Alexandru Rosianu的大力帮助下终于解决了。 代码已修改正确