我制作了一个类似于Android Studio中标准抽屉布局的片段,以便能够切换片段。关键是,当我按下按钮时,它不会切换片段。这些按钮被引用并完成。但是当我按下按钮时什么也没发生
这是“课程列表”片段。不要介意其中包含Activity的名称,应将其命名为一个片段。
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>
答案 0 :(得分:0)
您的布局XML文件不包含您引用的任何content_frame
元素。除非您发布了错误的布局文件,否则通常将FrameLayout
用作空片段容器。
<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/>
答案 1 :(得分:-1)
您应该使用导航组件https://developer.android.com/guide/navigation 这将简化您的片段处理。