我想将ViewPager与Aurel Hubert的https://github.com/aurelhubert/ahbottomnavigation
的BottomNavigation栏一起使用我有以下代码:
我的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。 我已经谷歌搜索,但没有找到任何特定的导航栏。 是否可以使用此导航栏添加滑动效果? 我想要的就是我可以在碎片之间左右擦拭。
答案 0 :(得分:0)
我知道这有点晚了但是这里有一种实现你假装的方式。如果您想要滑动行为,则应在活动中进行触摸事件以检测滑动。在您检测到滑动后,它应该很简单,只需获取当前项目,并根据滑动设置底部导航中的位置。
示例:
if(isSwipeRight && bottomNavigation.getCurrentItem() > 0)
bottomNavigation.setCurrentItem(bottomNavigation.getCurrentItem()-1);
else if(isSwipeLeft && bottomNavigation.getCurrentItem() < numTabs-1)
bottomNavigation.setCurrentItem(bottomNavigation.getCurrentItem()+1);