在导航抽屉中单击后,片段未被替换

时间:2018-10-22 16:24:40

标签: android android-layout android-fragments android-fragmentactivity

我尝试了各种方法来解决此问题,但是我仍然没有发现这段代码有什么问题。你能帮我吗?

我的主页活动为

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class MainPage extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private FragmentTransaction fragmentTransction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_page);

        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mNavigationView=findViewById(R.id.idnav_view);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        mDrawerLayout=findViewById(R.id.iddrawer_layout);


        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                  Fragment frag=null;
                  int itemId=menuItem.getItemId();

                if(itemId==R.id.id_nvHome){
                    frag=new HomeFragment();
                    }

                    else if(itemId==R.id.id_nvSearch){
                   frag=new SearchFragment();

                   }

                Toast.makeText(getApplicationContext(),menuItem.getTitle(),Toast.LENGTH_SHORT).show();

                if(frag!=null){
                    FragmentTransaction transction=getSupportFragmentManager().beginTransaction();
                    transction.replace(R.id.idcontent_frame,frag);
                    transction.commit();
                    mDrawerLayout.closeDrawers();
                    return true;
                }


                return false;



            }
        });

    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;

        }
        return super.onOptionsItemSelected(item);
    }



}

,对于Mypage活动布局为

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iddrawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/idcontent_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/idnav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header"/>

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

我的家庭片段是

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_home,container,false);
        return v;

    }
}

以及布局也是如此

<?xml version="1.0" encoding="utf-8"?>
<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=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_marginTop="150dp"
        android:layout_marginLeft="150dp"
       android:textColor="#000000"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

我想在点击导航抽屉项目时用其他片段替换片段

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我可以为您提供适用于Android 27.1.1的工作代码:

 public class MainActivity extends AppCompatActivity implements 
 BottomNavigationView.OnNavigationItemSelectedListener {

private int mSelectedId;

/**
 * Create Navigation bar and load the main fragment "statistics".
 */

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

    loadFragment(new FragmentStatistics());

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(this);

    mSelectedId = navigation.getSelectedItemId();
}

/**
 * Checks for any event in the Navigation Bar.
 * If the item selected is the same as the actual item
 * Do nothing
 * Else
 * Load new fragment selected.
 */

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    Fragment fragment = null;

    if (mSelectedId != item.getItemId()) {

        mSelectedId = item.getItemId();

        switch (item.getItemId()) {

            case R.id.statistics:
                fragment = new FragmentStatistics();
                break;

            case R.id.shops:
                fragment = new FragmentShops();
                break;

          }

    }

    return loadFragment(fragment);
}

private boolean loadFragment(Fragment fragment) {

    //switching fragment
    if (fragment != null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
        return true;
    }
    return false;
 }

 }

其余代码与我的完全相同。

相关问题