码迷,mamicode.com
首页 > 移动开发 > 详细

Android编程入门--BottomNavigationView+ViewPager

时间:2017-09-29 15:02:20      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:lstat   imp   com   UI   使用方法   get   listener   win   android   

参考博客:Android (争取做到)最全的底部导航栏实现方法

Bottom Navigation是5.0(API level 21)新出的一种符合MD规范的导航栏规范。

规范参考:Android Bottom navigation 规范一:使用方法

3个比较火的开源库,GitHub - aurelhubert/ahbottomnavigation,GitHub - roughike/BottomBar, Ashok-Varma/BottomNavigation

ashokvarma 参考博客:Android学习之BottomNavigationBar实现Android特色底部导航栏

或参考博客:Android底部导航BottomNavigationBar

 

BottomNavigationView+ViewPager+Fragment布局方式

ViewPagerAdapter:

package com.zyp.energy.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment) {
        mFragmentList.add(fragment);
    }

}
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(HomeFragment.newInstance());
        adapter.addFragment(NotifyFragment.newInstance());
        adapter.addFragment(SettingFragment.newInstance());
        mViewPager.setAdapter(adapter);

 

布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/activity_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.zyp.energy.ui.activity.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>
    <include layout="@layout/content_main" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

事件代码:

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

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

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mViewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_notifications:
                    mViewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_setting:
                    mViewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }

    };

 

Android编程入门--BottomNavigationView+ViewPager

标签:lstat   imp   com   UI   使用方法   get   listener   win   android   

原文地址:http://www.cnblogs.com/bmbh/p/7117431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!