简体   繁体   English

底部导航和片段选项卡布局

[英]Bottom Navigation and Fragment Tab Layout

I have a tab layout with 3 Fragments, I also have a BottomNavigation.我有一个包含 3 个片段的选项卡布局,我还有一个底部导航。 So should I place BottomNavigation in an Activity for 3 Fragments or place a bottom navigation in each Fragment?那么我应该将底部导航放在 3 个片段的活动中还是在每个片段中放置一个底部导航? Because I have a ListView in each Fragment so I think each Fragment should have one BottomNavigation for click event.因为我在每个 Fragment 中有一个 ListView,所以我认为每个 Fragment 应该有一个用于单击事件的 BottomNavigation。

Just one BottomNavigation for all fragment.所有片段只有一个底部导航。 You can easily use this library and then in YOUR_ACTIVITY_LAYOUT set it like this:您可以轻松使用此,然后在 YOUR_ACTIVITY_LAYOUT 中将其设置如下:

<RelativeLayout
        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">

    <FrameLayout
            android:layout_above="@id/bottom_navigation"
            android:name="com.ali.digikalaapp.Fragment_pager_one"
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


<com.ss.bottomnavigation.BottomNavigation
                android:id="@+id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_alignParentBottom="true"
                android:background="@color/colorPrimary">

                <com.ss.bottomnavigation.TabItem
                    android:id="@+id/tab_home"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:tab_text="Home"
                    app:tab_icon="@drawable/ic_home_white_24dp"
                    />
                <com.ss.bottomnavigation.TabItem
                    android:id="@+id/tab_images"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:tab_text="Images"
                    app:tab_icon="@drawable/ic_image_black_24dp"
                    />
                <com.ss.bottomnavigation.TabItem
                    android:id="@+id/tab_camera"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:tab_text="Camera"
                    app:tab_icon="@drawable/ic_camera_white_24dp"
                    />
     </com.ss.bottomnavigation.BottomNavigation>


</RelativeLayout>

and if you have 3 Fragment , replace fragment like this in YOUR_ACTIVITY:如果您有 3 个 Fragment ,请在 YOUR_ACTIVITY 中像这样替换 Fragment:

public class YOUR_ACTIVITY extends AppCompatActivity {

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

            BottomNavigation bottomNavigation = (BottomNavigation) findViewById(R.id.bottom_navigation);

            bottomNavigation.setOnSelectedItemChangeListener(new OnSelectedItemChangeListener() {

                @Override
                public void onSelectedItemChanged(int itemId) {
                    switch (itemId) {
                        case R.id.tab_home:
                            Fragment_Home home = new Fragment_Home();
                            FragmentManager fragment = getSupportFragmentManager();
                            FragmentTransaction transaction = fragment.beginTransaction();
                            transaction.replace(R.id.frameLayout, home);
                            break;
                        case R.id.tab_images:
                            Fragment_Image image = new Fragment_Image();
                            FragmentManager fragment = getSupportFragmentManager();
                            FragmentTransaction transaction = fragment.beginTransaction();
                            transaction.replace(R.id.frameLayout, image);
                            break;
                        case R.id.tab_camera:
                            Fragment_Camera camera = new Fragment_Camera();
                            FragmentManager fragment = getSupportFragmentManager();
                            FragmentTransaction transaction = fragment.beginTransaction();
                            transaction.replace(R.id.frameLayout, camera);
                            break;

                    }
                    transaction.commit();
                }
            }
        }    

and here is one Fragment example :这是一个片段示例:

public class Fragment_Home extends Fragment {

    public Fragment_Home() {
    }

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

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

}

and here is fragment_home XML :这是 fragment_home 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=".Fragment.Fragment_Home">


        <TextView
            android:text="Blank Fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>

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

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