BottomNavigationView+fragment 实现底部菜单栏
来源:互联网 发布:弹歌软件 编辑:程序博客网 时间:2024/05/21 22:55
第三方大神:GitHub - aurelhubert/ahbottomnavigation,GitHub - roughike/BottomBar, Ashok-Varma/BottomNavigation。
直接上代码:
activity_main:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.tablayout.MainActivity"> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:visibility="visible"> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" android:text="@string/title_home" android:visibility="gone" /> </FrameLayout> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:visibility="gone"> </android.support.v4.view.ViewPager> <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" /></LinearLayout>
MainActivity代码:
import android.os.Bundle;import android.support.annotation.NonNull;import android.support.design.widget.BottomNavigationView;import android.support.v4.app.Fragment;import android.support.v7.app.AppCompatActivity;import android.view.MenuItem;import android.widget.FrameLayout;import android.widget.TextView;import com.example.administrator.tablayout.fragment.BlankFragment;import com.example.administrator.tablayout.fragment.FragmentOne;import com.example.administrator.tablayout.fragment.FragmentTwo;import com.example.administrator.tablayout.fragment.ItemFragment;import com.example.administrator.tablayout.fragment.PlusOneFragment;public class MainActivity extends AppCompatActivity { private TextView mTextMessage; private BlankFragment blankFragment; private ItemFragment itemFragment; private PlusOneFragment plusOneFragment; private FrameLayout fl; private Fragment[] fragments; private int lastShowFragment = 0; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: if (lastShowFragment != 0) { switchFrament(lastShowFragment, 0); lastShowFragment = 0; } return true; case R.id.navigation_dashboard: if (lastShowFragment != 1) { switchFrament(lastShowFragment, 1); lastShowFragment = 1; } return true; case R.id.navigation_notifications: if (lastShowFragment != 2) { switchFrament(lastShowFragment, 2); lastShowFragment = 2; } return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fl = (FrameLayout) findViewById(R.id.content); mTextMessage = (TextView) findViewById(R.id.message); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); initFragments(); } /** * 切换Fragment * * @param lastIndex 上个显示Fragment的索引 * @param index 需要显示的Fragment的索引 */ public void switchFrament(int lastIndex, int index) { android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.hide(fragments[lastIndex]); if (!fragments[index].isAdded()) { transaction.add(R.id.content, fragments[index]); } transaction.show(fragments[index]).commitAllowingStateLoss(); } private void initFragments() { FragmentOne one = new FragmentOne(); FragmentTwo one2 = new FragmentTwo(); FragmentOne one3 = new FragmentOne(); fragments = new Fragment[]{one, one2, one3}; lastShowFragment = 0; getSupportFragmentManager() .beginTransaction() .add(R.id.content, one) .show(one) .commit(); }}
over很简单。
阅读全文
1 0
- BottomNavigationView+fragment 实现底部菜单栏
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- Fragment实现底部菜单栏
- BottomNavigationView+ViewPager+Fragment 实现底部导航栏
- FragmentTabHost+Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- 使用Fragment实现底部菜单栏
- 使用Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- FragmentTabHost+Fragment实现底部菜单栏
- 底部菜单栏(二)使用Fragment实现底部菜单栏
- 底部导航BottomNavigationView+ViewPager+Fragment
- Android TabHost +Fragment 实现底部菜单栏
- Android TabHost +Fragment 实现底部菜单栏 .
- 【Android】底部菜单栏--Fragment+FragmentTabHost实现
- #Android笔记#fragment+fragmentTabHost实现底部菜单栏
- 【Android UI】RadioGroup+Fragment实现底部菜单栏
- RTG-tools的安装与子工具vcfeval的使用
- jrebel7.0破解
- 微信公众号页面开发的一些小记录(地图)
- MyEclipse和Eclipse中修改编码
- python--连接hive实现代码
- BottomNavigationView+fragment 实现底部菜单栏
- 指纹识别
- 在centos安装jdk
- expect
- docker常用命令
- Python 运算符
- 浏览VIM中文文档------用户手册之usr_01.txt——usr_02.txt——usr_03.txt
- 4位一线技术大咖齐聚LiveVideoStack 多媒体开发技术沙龙提前观
- Spring ERROR