web-dev-qa-db-fra.com

le viseur utilisant la vue de navigation inférieure ne fait pas glisser les fragments

Je veux faire glisser les onglets donnés en bas. Pour cela, j'ai utilisé ViewPager en utilisant BottomNavigationView par this . Mon code est donné ci-dessous.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    xmlns:tools="http://schemas.Android.com/tools"
    tools:context=".MainActivity">
    <Android.support.v4.view.ViewPager
        Android:id="@+id/viewpager"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_above="@+id/navigation"
        Android:layout_alignParentTop="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <Android.support.design.widget.BottomNavigationView
        Android:id="@+id/navigation"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation_items"
        Android:background="@color/colorPrimary"
        app:itemIconTint="@color/colorAccent"
        app:itemTextColor="@Android:color/white"></Android.support.design.widget.BottomNavigationView>
</RelativeLayout>

Le code Java est

    public class MainActivity extends AppCompatActivity {
    BottomNavigationView bottomNavigationView;
    ViewPager viewPager;
    MenuItem prevMenuItem;
    FragmentWallet fragmentWallet;
    FragmentNavigationDrawer fragmentNavigationDrawer;
    FragmentHome fragmentHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.action_menu_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.action_menu_wallet:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.action_menu_navigation_d:
                        viewPager.setCurrentItem(2);
                        break;
                }

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

            }

            @Override
            public void onPageSelected(int position) {
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                Log.d("page", "onPageSelected: " + position);
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                prevMenuItem = bottomNavigationView.getMenu().getItem(position);

            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        setupViewPager(viewPager);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        fragmentHome = new FragmentHome();
        fragmentWallet = new FragmentWallet();
        fragmentNavigationDrawer = new FragmentNavigationDrawer();
        viewPagerAdapter.addFragment(fragmentHome);
        viewPagerAdapter.addFragment(fragmentWallet);
        viewPagerAdapter.addFragment(fragmentNavigationDrawer);
        viewPager.setAdapter(viewPagerAdapter);
    }
}

Le code de l'adaptateur de téléavertisseur est

   public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment) {
        mFragmentList.add(fragment);
    }
}

Cela fonctionne bien pour naviguer d'un fragment à un autre, mais il ne fait pas glisser les fragments. Aidez-moi ou devrais-je changer de solution.

8
Kiran S Kulkarni

Retirez cette partie:

viewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
11
Pavel Kuznetsov