带有片段的底部导航栏

4
我在我的主活动中有一个底部导航栏。通过点击底部导航的选项卡之一,我想要更改视图中的碎片。我有以下代码: 主活动:
public class StartActivity extends AppCompatActivity {

SwiftFragment swiftFragment;
FrameworksFragment frameworksFragment;
FrameLayout content;
android.support.v4.app.FragmentManager fragmentManager;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();

        switch (item.getItemId()) {
            case R.id.navigation_home:
                Log.i("Nachricht", "HOME");
                return true;
            case R.id.navigation_swift:
                Log.i("Nachricht", "SWIFT");
                transaction.replace(android.R.id.content, swiftFragment);
                transaction.commit();
                return true;
            case R.id.navigation_frameworks:
                Log.i("Nachricht", "FRAMEWORKS");
                transaction.replace(android.R.id.content, frameworksFragment);
                transaction.commit();
                return true;
            case R.id.navigation_profile:
                Log.i("Nachricht", "PROFILE");
        }
        return false;
    }
};

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

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    swiftFragment = (SwiftFragment) android.support.v4.app.Fragment.instantiate(this, SwiftFragment.class.getName(), null);
    frameworksFragment = (FrameworksFragment) android.support.v4.app.Fragment.instantiate(this, FrameworksFragment.class.getName(), null);
    content = (FrameLayout) findViewById(android.R.id.content);
    fragmentManager = getSupportFragmentManager();
}

}

我的一个片段:

public class SwiftFragment extends Fragment {

ArrayList<ModelTutorial> items;
ListView list;
ArrayAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_swift, container, false);

    //TODO: Get Firebase Data
    items = new ArrayList<>();
    items.add(new ModelTutorial("Swift Anlegen der Liste", "TableView", "Test Test Test", null, null));
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null));
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null));

    list = (ListView) view.findViewById(R.id.swiftTutorialsList);
    adapter = new ListAdapter(getActivity(), items);
    list.setAdapter(adapter);

    return view;
}

}

如果我点击其中一个选项卡,右侧的片段就会出现,所以这是有效的。然而,当新片段出现并且我想点击另一个选项卡显示另一个片段时,这不起作用。底部导航栏没有对点击做出反应。即使是Log.i语句也不起作用,因此似乎没有调用OnNavigationItemSelectedListener

我完全是新手,不了解为什么不起作用。如果有人能帮我解决问题,我将不胜感激。

编辑:XML文件StartActivity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.marcelspindler.codingtutorials.StartActivity">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>

XML文件片段:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.marcelspindler.codingtutorials.SwiftFragment">

<!-- TODO: Update blank fragment layout -->

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

你能否添加XML文件?检查一下你的监听器是否真正被调用,但返回false的话,它就不会记录日志。 - Binary Baba
2
需要XML布局,我认为XML中可能会出现错误,因为您正在使用android.R.id.content替换片段,这样做将替换整个窗口,例如通过为片段背景设置颜色进行检查。 - Hemanth S
我编辑了我的问题并添加了XML文件。@RickSanchez 我尝试在返回false时记录日志,但从未调用。@HermanSTobi 我更改了背景颜色,你是对的,它覆盖了整个窗口,包括底部导航栏。如果android.R.id.content不起作用,您知道避免这种情况的方法吗? - Marcel
1个回答

4
您需要将Fragment添加到MainActivity内的容器中。
可以尝试以下代码:
<android.support.constraint.ConstraintLayout 
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.marcelspindler.codingtutorials.StartActivity">

<FrameLayout 
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>

然后将您的替换行更改为:

transaction.replace(R.id.fragment_container, fragment);

BONUS: 您可以像这样启动您的片段: swiftFragment = new SwiftFragment();

在此之后查看: 最佳实践:实例化新的Android Fragment


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接