繁体   English   中英

带有碎片的底部导航栏

[英]Bottom Navigation bar with fragments

我的主要活动中有一个底部导航栏。 通过单击我底部导航中的一个选项卡,我想更改视图中的片段。 我有以下代码:主要活动:

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文件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>

您需要将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片段的最佳实践

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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