简体   繁体   English

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

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

I want to use the ViewPager with the BottomNavigation bar from Aurel Hubert https://github.com/aurelhubert/ahbottomnavigation 我想将ViewPager与Aurel Hubert的BottomNavigation栏一起使用https://github.com/aurelhubert/ahbottomnavigation

I have following code: 我有以下代码:

My activity_main.xml: 我的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>

and my MainActivity.java: 和我的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();
        }
    }
}

I have no clue how to implement a ViewPager in this case. 在这种情况下,我不知道如何实现ViewPager。 I have already googled but have found nothing specific to this NavigationBar. 我已经在Google上搜索过,但是没有找到特定于该NavigationBar的内容。 Is it possible at all to add a swipe effect with this navigation bar? 是否可以通过此导航栏添加滑动效果? All I want is that I can wipe left and right between my Fragments. 我只想在片段之间左右擦拭即可。

I know it is a bit late but here goes a way of achieving what you pretend. 我知道现在有点晚了,但是这里提供了一种实现您假装的方式。 If you want a swipe behaviour you should make a touch event in your activity to detect a swipe. 如果要进行滑动操作,则应在活动中进行触摸事件以检测滑动。 After you detect a swipe it should be simple, just get the current item, and depending on the swipe set the position on the bottom navigation. 检测到滑动后,它应该很简单,只需获取当前项目,然后根据滑动位置在底部导航中设置位置即可。

Example: 例:

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.

相关问题 如何使用导航抽屉选择ViewPager - How do I use the navigation drawer to select a viewpager 如何在ViewPager的每个部分中使用片段 - How do I use fragments in each section of ViewPager 如何在片段中使用Viewpager - How can I use Viewpager in a fragment 如何在ViewPager中使用PreferenceFragment? - How can I use a PreferenceFragment with a ViewPager? 如何使用ViewPager显示应用程序? - How can I use ViewPager to display apps? 如何在 Java 代码中使用两个条形“//”? [等候接听] - How do I use two bar “//” in a Java Code? [on hold] 如何使用 viewpager2 和 fragmentstateadapter 动态添加和删除片段? - How do I dynamically add and delete fragments with viewpager2 and fragmentstateadapter? ViewPager.setOffscreenPageLimit(0)完全没有效果。 如何在ViewPager中一次只加载一个页面? - ViewPager.setOffscreenPageLimit(0) has no effect at all. How do I only load one page at a time in a ViewPager? 当用户到达ViewPager的末尾时,如何动态添加片段以启动ViewPager? - How do I dynamically add fragments to start of ViewPager as user reaches the end of the ViewPager? 如何使用 RecyclerView 打开 ViewPager 中的特定页面? - How can I use the RecyclerView to open specific pages in ViewPager?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM