1BottomNavigationView
实现参考链接https://juejin.im/entry/583414ce61ff4b006b8cf338http://blog.csdn.net/qq_35366908/article/details/72457909
实现方式:1.1 BottomNavigationView是放置在design包中的,所以,使用前需要先引入com.android.support:design:25.1.0包1.2xml
布局
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tb_toolbar"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="22dp"
tools:text="123524352"/>
android:id="@+id/bnv_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="@color/selector_blue"
app:itemTextColor="@color/selector_blue"
app:menu="@menu/menu_bottom_navigation"/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@id/bnv_menu"
android:background="@color/gray"/>
java代码
publicclassMainActivityextendsAppCompatActivityimplementsBottomNavigationView.OnNavigationItemSelectedListener{privateTextFragment fragment;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
fragment =newTextFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem item){@StringResinttext;switch(item.getItemId()) {caseR.id.recents:
text = R.string.recents;break;caseR.id.favourites:
text = R.string.favourites;break;caseR.id.nearby:
text = R.string.nearby;break;default:returnfalse;
}
switchFragmentText(text);returntrue;
}privatevoidswitchFragmentText(@StringResinttext){
fragment.setText(text);
}
}
}
详细
package com.song.wallpager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
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);
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) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastIndex]);
if (!fragments[index].isAdded()) {
transaction.add(R.id.fragment_container, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
private void initFragments() {
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
fragments = new Fragment[]{fragment1, fragment2, fragment3};
lastShowFragment = 0;
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, fragment1)
.show(fragment1)
.commit();
}
}
网友评论