I'm not managing to change the fragments

2
import android.content.Intent;
import android.os.Bundle;


import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.facebook.accountkit.AccessToken;
import com.facebook.accountkit.Account;
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.AccountKitCallback;
import com.facebook.accountkit.AccountKitError;
import com.facebook.accountkit.PhoneNumber;



public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;

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

        //Verificacion de inicio de sesion
        AccountKit.initialize(getApplicationContext());
        AccessToken accessToken = AccountKit.getCurrentAccessToken();
        if (accessToken == null) {
            IrLogin();
        }


        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.inicio:

                        setFragment(0);

                        return true;
                    case R.id.buscar:

                        setFragment(1);

                        return true;
                    case R.id.perfil:

                        setFragment(2);
                        return true;
                }
                return true;
            }
        });


    }

    public void setFragment(int position) {

        switch (position) {
            case 0:
                Fragment fragment1 = new frInicio();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment1).commit();
                break;
            case 1:
                Fragment fragment2 = new frBuscarPerfiles();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment2).commit();

                break;
            case 2:
                Fragment fragment3 = new frPerfil();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment3).commit();

        }
    }


}

Activity Main

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/barainf" />

    <FrameLayout
        android:id="@+id/barainf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">


        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="top"
            android:background="@drawable/shadow"/>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            design:menu="@menu/menu_navigation"/>


    </FrameLayout>
</RelativeLayout>

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

menu_navigation

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


<item
    android:title="@string/home"
    android:icon="@drawable/ic_home"
    android:id="@+id/inicio">
</item>
<item
    android:title="buscar"
    android:icon="@drawable/ic_explore"
    android:id="@+id/buscar">
</item>

<item
    android:title="pefil"
    android:icon="@drawable/ic_person"
    android:id="@+id/perfil">
</item>

</menu>
    
asked by Mathias Toledo 29.03.2017 в 00:05
source

2 answers

0

Use an instance of Fragment (in this case fragment ) of the type obtained in the switch and make the transaction:

 public void setFragment(int position) {

    Fragment fragment = null;

        switch (position) {
            case 0:
                fragment = new frInicio();
                  break;
            case 1:
                fragment = new frBuscarPerfiles();

                break;
            case 2:
                fragment = new frPerfil();
                break;                
        }

    if (fragment != null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();                  
     }

 }

In this way the instance of fragment will always be replaced.

Update: when reviewing your layout, where you try to add the Fragment , fragment_container is a RelativeLayout

  <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/barainf" />

must be a FrameLayout :

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

or a Fragment if you want to add the Fragment directly.

 <fragment
            android:id="@+id/fragment_container"
            android:name="<PÄQUETE>Fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
answered by 29.03.2017 в 00:08
0

If you have not yet solved your problem, I have a project with only a navigation drawer that changes the fragments.

You can copy the project structure .

    
answered by 20.04.2017 в 13:30