简体   繁体   中英

Fragment not being replaced when pressing a button

I made a fragment that looks like the standard drawer layout from Android Studio to be able to switch Fragments. The thing is, it's not switching fragments when I'm pressing a button. The buttons are referred and made. But nothing is happening when I press the button

This is the "Course list" fragment. Don't mind the names that has Activity in them, it should be named as a fragment.

package com.example.project_b_open_day_application;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class CoursesActivity extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        return inflater.inflate(R.layout.fragment_courses, container, false);
    }

    public void selectCourse(int itemId) {
        //creating fragment object
        Fragment fragment = null;

        //initializing the fragment object which is selected
        switch (itemId) {
            case R.id.informaticaOpleiding:
                fragment = new InformaticaActivity();
                break;
            case R.id.technischeinformaticaOpleiding:
                fragment = new TechnischeinformaticaActivity();
                break;
            case R.id.communicatieOpleiding:
                fragment = new CommunicatieActivity();
                break;
            case R.id.cmgtOpleiding:
                fragment = new CmgtActivity();
                break;
        }
        //replacing the fragment
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }
    }
}

fragment_courses.xml

    <?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="491dp"
        android:alpha=".7"
        android:background="@android:color/background_light"
        android:lineSpacingExtra="10sp"
        android:scrollbars="vertical"
        android:text="@string/courseSelectie"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textSize="15sp" />

    <Button
        android:id="@+id/cmgtOpleiding"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="98dp"
        android:layout_marginLeft="98dp"
        android:layout_marginTop="567dp"
        android:text="CMGT" />

    <Button
        android:id="@+id/informaticaOpleiding"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="98dp"
        android:layout_marginLeft="98dp"
        android:layout_marginTop="159dp"
        android:layout_marginBottom="373dp"
        android:text="Informatica" />

    <Button
        android:id="@+id/technischeinformaticaOpleiding"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="95dp"
        android:layout_marginLeft="95dp"
        android:layout_marginTop="400dp"
        android:text="Technische Informatica" />

    <Button
        android:id="@+id/communicatieOpleiding"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="98dp"
        android:layout_marginLeft="98dp"
        android:layout_marginTop="486dp"
        android:text="Communicatie" />

    </RelativeLayout>


Your layout XML file does not feature any content_frame element you refer to. Unless you posted incorrect layout file, it's common practice to use FrameLayout as empty fragment container.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout/>

您应该使用导航组件https://developer.android.com/guide/navigation,这将简化您的片段处理。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM