在导航抽屉中的OnClick事件期间,片段未被替换

时间:2015-01-10 14:12:59

标签: android android-fragments navigation-drawer

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

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

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

我不知道问题在哪里,希望有人可以提供帮助,这是我的代码:

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类示例(在本例中为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>

我的主要活动xml:

<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>

2 个答案:

答案 0 :(得分:0)

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

  1. 在您的主要活动的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);
    

答案 1 :(得分:0)

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