android 底部菜单 BottomNavigationView + ViewPager + Fragment

编译版本在21及以上
第一步、创建三个Fragment 自己自定义命名
HomeFragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.ms.lemonnote.R;
public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment, null);
    }

}

fragment_home.xml




    

FindFragMent、 MineFragment 和HomeFragment 一样

第二、创建FragmentAdapter

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

public class FragmentAdapter extends FragmentPagerAdapter {
    private List mFragments;

    public FragmentAdapter(@NonNull FragmentManager fm, int behavior, List fragments) {
        super(fm, behavior);
        this.mFragments = fragments;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments == null ? 0 : mFragments.size();
    }

}

第三、在res资源文件下,创建menu资源文件并在其目录下面创建bottom_menu.xml,注意个数不能超过5个否则会报错



    
    
    

第五、创建MainActivity

... 省略引用
public class MainActivity extends AppCompatActivity {
    BottomNavigationView bottomNavigationView = null;
    ViewPager viewPager;
    private List mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = findViewById(R.id.navigation);
        viewPager = findViewById(R.id.view_pager);

        //初始化Fragment及底部导航栏
        mFragments = new ArrayList<>(3);
        mFragments.add(new HomeFragment());
        mFragments.add(new FindFragment());
        mFragments.add(new MineFragment());
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, mFragments);
        viewPager.setAdapter(adapter);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int menuId = menuItem.getItemId();
                // 跳转指定页面:Fragment
                switch (menuId) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.navigation_find:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.navigation_mine:
                        viewPager.setCurrentItem(2);
                        break;
                    default:

                        break;
                }
                return true;
            }
        });

        // ViewPager 滑动事件监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                //将滑动到的页面对应的 menu 设置为选中状态
                bottomNavigationView.getMenu().getItem(i).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}

activity_main.xml





    

    



你可能感兴趣的:(android 底部菜单 BottomNavigationView + ViewPager + Fragment)