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没有调用。

我对android开发完全陌生,我不明白为什么这不起作用。如果有人可以帮助我解决我的问题,我将不胜感激。

编辑:XML 文件启动活动:

<?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>
4

1 回答 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);

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

之后看看这个: 实例化新 Android 片段的最佳实践

于 2017-12-28T12:56:11.473 回答