Android Studio 底部栏导航:如何添加到每个片段?

吉姆做代码

我按照在线教程创建了一个 Bottom Bar Navigation,但是那个人没有清楚地解释如何向每个片段添加内容并在片段之间交换。我想EditText在主页上添加一个作为如何向片段添加项目的示例。这是我的代码:

主要活动 -

public class MainActivity extends AppCompatActivity {

private static final String TAG_FRAGMENT_CATA = "tag_frag_cata";
private static final String TAG_FRAGMENT_HOME = "tag_frag_home";
private static final String TAG_FRAGMENT_SETTINGS = "tag_frag_settings";

private BottomNavigationView bottomNavigationView;

private List<BottomBarFragment> fragments = new ArrayList<>(3);

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

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                        switch (item.getItemId()) {
                            case R.id.bottombaritem_cata:
                                switchFragment(0, TAG_FRAGMENT_CATA);
                                return true;
                            case R.id.bottombaritem_home:
                                switchFragment(1, TAG_FRAGMENT_HOME);
                                return true;
                            case R.id.bottombaritem_settings:
                                switchFragment(2, TAG_FRAGMENT_SETTINGS);
                                return true;
                        }
                        return false;
                    }
                });

        buildFragmentsList();

        switchFragment(1, TAG_FRAGMENT_HOME);

    }

    private void buildFragmentsList() {
        BottomBarFragment cataFragment = buildFragment("Categories");
        BottomBarFragment homeFragment = buildFragment("Home");
        BottomBarFragment settingsFragment = buildFragment("Settings");

        fragments.add(cataFragment);
        fragments.add(homeFragment);
        fragments.add(settingsFragment);
    }

    private BottomBarFragment buildFragment(String title) {
        BottomBarFragment fragment = new BottomBarFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BottomBarFragment.ARG_TITLE, title);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void switchFragment(int pos, String tag) {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_fragmentholder, fragments.get(pos), tag).commit();
    }
}

底部栏活动 -

public class BottomBarFragment extends Fragment {
    public static final String ARG_TITLE = "arg_title";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.bottom_bar_fragment, container, false);

        return rootView;
    }

}

主要活动的 XML

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:itemBackground="@android:color/white"
    app:itemIconTint="@android:color/black"
    app:itemTextColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottombar_menu" />

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/bottom_nav"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <FrameLayout
        android:id="@+id/frame_fragmentholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

</android.support.constraint.ConstraintLayout>

XML 布局文件 -

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BottomBarFragment">

</android.support.constraint.ConstraintLayout>

如果有人可以解释如何使用此代码,我将不胜感激!

杰勒

您的bottomNavigationBar 中有三个片段。我想这些将是 cata、home 和 settings。对于每一个你需要创建新的类。

例如对于 cata:

public class CataFragment extends Fragment {

    public CataFragment() {
        // Required constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

                    //here you would do the fragment specific coding

        return inflater.inflate(R.layout.fragment_cata, container, false);
    }
}

正如您已经看到的,它们与布局文件联系在一起,这里fragment_cata是您必须使用其名称创建 xml 布局文件的文件。布局文件可以是自动创建的空白片段布局,您可以在创建新文件时在列表中找到该布局。那看起来像这样:

<RelativeLayout 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=".ChatFragment">

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

</RelativeLayout>

您可以像处理任何其他布局文件一样处理此布局文件

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将ABS库添加到Android Studio?

来自分类Dev

如何将ViewPagerIndicator添加到Android Studio

来自分类Dev

如何将系统库添加到Android Studio?

来自分类Dev

如何将 firebase 库添加到 Android Studio

来自分类Dev

Android studio 底部导航栏:如何设置要先点击中间按钮?

来自分类Dev

Android:将相机图标添加到Android Studio的操作栏中

来自分类Dev

将库添加到android studio

来自分类Dev

将ViewPagerIndicator添加到Android Studio

来自分类Dev

在Android Studio中将导航抽屉添加到我的应用中

来自分类Dev

Android Studio] KOTLIN导航栏

来自分类Dev

Android Studio] KOTLIN导航栏

来自分类Dev

无法在Android Studio中将动作按钮添加到我的动作栏中

来自分类Dev

Android Studio导航抽屉如何导航不同的片段?

来自分类Dev

将android包添加到Java模块(android studio)

来自分类Dev

将Android SDK路径添加到Android Studio

来自分类Dev

无法将 android 支持库添加到 android studio

来自分类Dev

如何在Android Studio导航抽屉中设置默认片段?

来自分类Dev

将9补丁图像添加到Android Studio

来自分类Dev

无法将BottomBar添加到Android Studio

来自分类Dev

将twitter-kit添加到android studio项目

来自分类Dev

将epublib添加到Android Studio并运行

来自分类Dev

将库源添加到Android Studio

来自分类Dev

无法在Android Studio中将文件添加到gitignore

来自分类Dev

将Joda Time添加到Android Studio

来自分类Dev

在Android Studio中将本地Javadoc添加到本地AAR

来自分类Dev

将Maven依赖项添加到Android Studio Gradle

来自分类Dev

将静态JSON添加到Android Studio项目

来自分类Dev

将解析javadoc添加到Android Studio

来自分类Dev

将Playhaven SDK添加到Android Studio

Related 相关文章

热门标签

归档