使用底部导航视图查看ViewPager内的Flipper

时间:2017-09-12 09:51:11

标签: android android-fragments android-viewpager viewflipper bottomnavigationview

我的MainActivity中有一个底部导航,其中包含一个查看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;
                }
            });

这很好用,但在Fragment 3中我得到了一个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>

这是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;
                }
            });

当我滑动时遇到麻烦,viewPager带头并将我发送到我的lastFragment,我的触摸事件没有执行viewFlipper的ACTION_UP。通过viewPager检测滑动的唯一方法是将一根手指放在屏幕上,然后用另一根手指滑动布局。

我不知道如何处理这个问题,当我的手指在viewFlipper区域内时,是否可以阻止viewPager滑动?

0 个答案:

没有答案