View Flipper inside ViewPager with Bottom Navigation View

504 views Asked by At

I got a bottom navigation in my MainActivity with a view Pager

   bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    final ViewPager viewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
    pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                case 2:
                    return new Fragment3();
                case 3:
                    return new Fragment4();
            }
            return null;
        }

    };

    viewPager.setAdapter(pagerAdapter);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
bottomNavigationView.getMenu().getItem(position).setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_1:
                            viewPager.setCurrentItem(0);
                            break;
                        case R.id.action_2:
                            viewPager.setCurrentItem(1);
                            break;
                        case R.id.action_3:
                            viewPager.setCurrentItem(2);
                            break;
                        case R.id.action_4:
                            viewPager.setCurrentItem(3);
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });

This works great, but inside Fragment 3 I got a viewFlipper

   <ViewFlipper
        android:id="@+id/flipper_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
                 SOME XML CODE
        </LinearLayout>
   </ViewFlipper>

This is the code of the viewFlipper :

  viewFlipper.setOnTouchListener(
            new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent touchevent) {
                        switch (touchevent.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                initialX = touchevent.getX();
                                return true;

                            case MotionEvent.ACTION_UP:
                                float finalX = touchevent.getX();
                                if (initialX > finalX * 1.5) {
                                    // Right Swipe
                                } else if (initialX * 1.5 < finalX) {
                                    // Left Swipe
                                }
                                break;
                        }
                        return false;
                }
            });

I got a trouble when I swipe, the viewPager take the lead and send me to my lastFragment, my touch event ACTION_UP of the viewFlipper is not executed. The only way for detect swipe by viewPager is to put one finger on screen and then swipe the layout with another finger.

I don't know how to handle this, is it possible to block the viewPager swipe when my finger is inside the area of viewFlipper ?

0

There are 0 answers