android-fragments 在android studio中,滑动片段会发生变化,但底部导航图标不会发生变化

tv6aics1  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(189)

我创建了一个底部的导航栏与3个图标和3个不同的片段链接到他们。

单击图标时

1.图标改变2.图标颜色改变3.相关片段出现在屏幕上

滑动时

只有该片段在改变。(图标没有改变)。

带3个图标的菜单

<item
    android:id="@+id/ic_birthdays"
    android:title="@string/birthdays"
    android:icon="@drawable/change_ic_cake"
    />
<item
    android:id="@+id/ic_add"
    android:title="@string/add"
    android:icon="@drawable/change_ic_add"
    />
<item
    android:id="@+id/ic_profile"
    android:title="@string/profile"
    android:icon="@drawable/change_ic_profile"
    />

菜单中链接的三个图标是:
1.生日图标

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/ic_cake_clicked" />
    <item android:state_checked="false" android:drawable="@drawable/ic_cake_not_clicked"/>
</selector>

2.添加图标

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_add_not_clicked" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_add_clicked" android:state_checked="true"/>
</selector>

3.配置文件图标

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_profile_not_clicked" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_profile_clicked" android:state_checked="true"/>
</selector>

主要活动.java

package com.example.bottom_nav;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.bottom_nav.databinding.ActivityMainBinding;
import com.google.android.material.navigation.NavigationBarView;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding ui;

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

        ui = ActivityMainBinding.inflate(getLayoutInflater());
        ViewGroup root = ui.getRoot();
        setContentView(root);

        //Instantiating 3 fragments
        ViewPageAdapter viewPageAdapter = new ViewPageAdapter(this);
        ui.viewPager.setAdapter(viewPageAdapter);

        //Linking those three fragments to their respective icons of bottom nav bar
        ui.bottomNav.setOnItemSelectedListener(
            new NavigationBarView.OnItemSelectedListener(){
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()){
                        case R.id.ic_birthdays:
                            ui.viewPager.setCurrentItem(0);
                            break;

                        case R.id.ic_add:
                            ui.viewPager.setCurrentItem(1);
                            break;
                        case R.id.ic_profile:
                            ui.viewPager.setCurrentItem(2);
                            break;
                    }
                    return true;
                }
            }
        );
    }
}

布局_主文件.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_nav" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation_menu" />

</RelativeLayout>

视图页面适配器.java

package com.example.bottom_nav;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.example.bottom_nav.fragments.add;
import com.example.bottom_nav.fragments.birthdays;
import com.example.bottom_nav.fragments.profile;

public class ViewPageAdapter extends FragmentStateAdapter {

    public ViewPageAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position){
            case 1: return new add();
            case 2: return new profile();
            default: return new birthdays();
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

3.碎片代码

1.生日. java

package com.example.bottom_nav.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bottom_nav.R;

public class birthdays extends Fragment {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_birthdays, container, false);
    }
}

2.添加. java

package com.example.bottom_nav.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bottom_nav.R;
public class add extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add, container, false);
    }
}

3.配置文件. java

package com.example.bottom_nav.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bottom_nav.R;

public class profile extends Fragment {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add, container, false);
    }
}
0qx6xfy6

0qx6xfy61#

您需要在viewpager上有一个侦听器,并跟踪上一个选定的项,因此创建一个全局变量,
全局变量:

MenuItem previousMenuItem;

然后是一个视图寻呼机侦听器:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            
        }

        @Override
        public void onPageSelected(int position) {
        if (previousMenuItem != null) {
            previousMenuItem.setChecked(false);
        }
        else {
            mBottomNavigationView.getMenu().getItem(0).setChecked(false);
        }
        mBottomNavigationView.getMenu().getItem(position).setChecked(true);
        previousMenuItem = mBottomNavigationView.getMenu().getItem(position);
            }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

别忘了在Activity启动时设置BottomNav的默认选择项(第一个)。

相关问题