kalel2581 Ответов: 0

Как я могу открыть более одного фрагмента на фрагменте


Привет.
Я создал приложение на android studio с навигационным ящиком, который использует фрагмент, но мне нужно открыть несколько фрагментов из одного фрагмента. Я уже успешно нашел способ открыть один фрагмент, но я хочу открыть больше, чем один. Код ниже:

Что я уже пробовал:

MainActitivity.java:
package com.urbannet.user2.receitassaudveis;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    private DrawerLayout drawer;

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

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

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new ReceitasSaudaveisFragment()).commit();
        navigationView.setCheckedItem(R.id.receitas_saudaveis);}

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.add(R.id.fragment_container, new ReceitasSaudaveisFragment());
        fragmentTransaction.commit();

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.receitas_saudaveis:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasSaudaveisFragment()).commit();
                break;

            case R.id.receitas_vegetarianas:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasVegetarianasFragment()).commit();
                break;

            case R.id.receitas_peixe:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasPeixeFragment()).commit();
                break;

            case R.id.receitas_sopas:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasSopasFragment()).commit();
                break;

            case R.id.receitas_bolo:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasBoloFragment()).commit();
                break;

            case R.id.receitas_carnes:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasCarneFragment()).commit();
                break;

            case R.id.outras_receitas:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new OutrasReceitasFragment()).commit();
                break;

            case R.id.receitas_afrodesiacas:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ReceitasAfrodesiacasFragment()).commit();
                break;

            case R.id.sobre_receitas_saudaveis:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new SobreReceitasSaudaveisFragment()).commit();
                break;

        }

        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {

            super.onBackPressed();
        }
    }
}


activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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


    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />

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


ReceitasSaudaveisFragment.java:

package com.urbannet.user2.receitassaudveis;

    import android.media.Image;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;


    public class ReceitasSaudaveisFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_receitas_saudaveis, container, false);
        Button btnFragment = (Button)view.findViewById(R.id.buttonRV);

        btnFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fr=getFragmentManager().beginTransaction();
                fr.replace(R.id.fragment_container, new ReceitasVegetarianasFragment());
                fr.commit();
            }
        });
        return view;

    }
}


fragment_receitas_saudaveis.xml:
<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical">


        //Receitas Vegetarianas
        <Button
            android:id="@+id/buttonRV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Receitas Vegetarianas"
            android:textColor="#ffffff"
            android:textSize="28sp" />

        <ImageView
            android:id="@+id/imgRV"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/receitas_vegetarianas" />



        //Receitas de Peixe
        <Button
            android:id="@+id/buttonRP"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Receitas de Peixe"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgRP"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/peixe" />

        //Receitas de Sopa
        <Button
            android:id="@+id/buttonRS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Receitas de Soupa"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgRS"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/soupas" />

        //Receitas de Bolo

        <Button
            android:id="@+id/buttonRB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Recitas de Bolo"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgRB"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/bolos1" />

        //Receitas de Carne

        <Button
            android:id="@+id/buttonRC"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Receitas de Carne"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgRC"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/carne" />

        //Outras Receitas

        <Button
            android:id="@+id/buttonOR"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Outras Receitas"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgOR"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/outras" />

        //Receitas Afrodisíacas

        <Button
            android:id="@+id/buttonRA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="Receitas Afrodisíacas"
            android:textColor="#ffffff"
            android:textSize="30sp" />

        <ImageView
            android:id="@+id/imgRA"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="fitXY"
            android:src="@drawable/afrodisiaco1" />


    </LinearLayout>

</ScrollView>

David Crow

Это форум "быстрые ответы". Задумывались ли вы о том, чтобы задать вопрос такого масштаба на самом форуме Android?

0 Ответов