无法显示片段

时间:2019-03-08 02:37:10

标签: android android-fragments

尝试使用3个片段创建底部导航栏。遵循了本教程https://www.youtube.com/watch?v=tPV8xA7m-iw,该教程运行良好,但我无法从一个片段切换到另一个片段。它甚至不是从初始片段开始的。

您会注意到,我设置了打印语句,以查看它是否实际记录了按钮的点击,然后打印出来但未激活片段

    package com.starenkysoftware.macapp;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        //webView.loadUrl("https://calendar_layout.google.com/calendar_layout/htmlembed?src=wlmacci@gmail.com&ctz=America/Toronto&dates=20190401/20190501");

        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CalendarFragment()).commit();
}

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment=null;

                    switch (menuItem.getItemId()) {
                        case R.id.nav_calander:
                            selectedFragment = new CalendarFragment();
                            System.out.println("1");
                            break;
                        case R.id.nav_announcements:
                            selectedFragment = new AnnouncementsFragment();
                            System.out.println("2");
                            break;
                        case R.id.nav_late_starts:
                            selectedFragment = new LateStartsFragment();
                            System.out.println("3");
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                    return true;
                }
            };
}

这是calendar_layout 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"
    android:background="@android:color/holo_red_light">

    <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Calendar Fragment"
    android:textSize="30sp"
    android:gravity="center"
    android:layout_centerInParent="true"/>

</RelativeLayout>

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.starenkysoftware.macapp">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">

        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有什么想法吗?非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您应该先添加片段。

 getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, new CalendarFragment(), CalendarFragment.class.getSimpleName())
                .commitAllowingStateLoss();

答案 1 :(得分:0)

我发现了问题。我以前有一个WebView,但我认为它不会显示,但只是占用了屏幕。将可见性设置为不可见,修复所有问题