我想用ViewPager從斯坦休伯特的BottomNavigation條https://github.com/aurelhubert/ahbottomnavigation如何在AHBottomNavigation欄中使用ViewPager?
我有以下代碼:
我activity_main.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.aaron.waller.mrpolitik.MainActivity"
android:id="@+id/content_id">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/myBottomNavigation_ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
和我MainActivity.java:
public class MainActivity extends AppCompatActivity implements AHBottomNavigation.OnTabSelectedListener {
AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
bottomNavigation = (AHBottomNavigation) findViewById(R.id.myBottomNavigation_ID);
bottomNavigation.setOnTabSelectedListener(this);
bottomNavigation.setDefaultBackgroundColor(Color.BLUE);
this.createNavItems();
}
//Create items, add them to bar, set propertied and set current item
private void createNavItems() {
//CREATE ITEMS
AHBottomNavigationItem ohnemundItem = new AHBottomNavigationItem("Parteien", R.drawable.parteienicon);
AHBottomNavigationItem grinseItem = new AHBottomNavigationItem("Statistiken", R.drawable.statsicon);
AHBottomNavigationItem lachItem = new AHBottomNavigationItem("Fragen", R.drawable.fragenicon);
//ADD THEM to bar
bottomNavigation.addItem(ohnemundItem);
bottomNavigation.addItem(grinseItem);
bottomNavigation.addItem(lachItem);
//set properties
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
//set current item
bottomNavigation.setCurrentItem(0);
}
@Override
public void onTabSelected(int position, boolean wasSelected) {
if (position == 0) {
ParteienFragment parteienFragment = new ParteienFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id, parteienFragment).commit();
} else if (position == 1) {
StatistikenFragment statistikenFragment = new StatistikenFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id, statistikenFragment).commit();
} else if (position == 2) {
FragenFragment fragenFragment = new FragenFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.content_id, fragenFragment).commit();
}
}
}
我不知道如何在這種情況下實現一個ViewPager。 我已經使用Google搜索,但沒有發現任何特定於此NavigationBar的內容。 是否有可能在此導航欄上添加滑動效果? 我想要的是我可以在我的碎片之間擦拭左右。
。您有什麼內容? – Joy
使用與BottomNavigation一個ViewPager違背材質設計 - https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior – michaelcarrano
@Joy你是什麼意思?我的問題出了什麼問題?請解釋一下,我是Android Studio的新手 –