繁体   English   中英

如何将ViewPager与AHBottomNavigation栏一起使用?

[英]How do I use a ViewPager with the AHBottomNavigation bar?

我想将ViewPager与Aurel Hubert的BottomNavigation栏一起使用https://github.com/aurelhubert/ahbottomnavigation

我有以下代码:

我的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的内容。 是否可以通过此导航栏添加滑动效果? 我只想在片段之间左右擦拭即可。

我知道现在有点晚了,但是这里提供了一种实现您假装的方式。 如果要进行滑动操作,则应在活动中进行触摸事件以检测滑动。 检测到滑动后,它应该很简单,只需获取当前项目,然后根据滑动位置在底部导航中设置位置即可。

例:

if(isSwipeRight && bottomNavigation.getCurrentItem() > 0)
    bottomNavigation.setCurrentItem(bottomNavigation.getCurrentItem()-1);
else if(isSwipeLeft && bottomNavigation.getCurrentItem() < numTabs-1)
    bottomNavigation.setCurrentItem(bottomNavigation.getCurrentItem()+1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM