Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewpager using bottom navigation view is not swiping the fragments

I want to swipe the tabs given at bottom. For that I have used ViewPager using BottomNavigationView by this. My code is given below.

    <?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>

Java code is

    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(); // this should be in `FragmentPagerAdapter.getItem()`
        fragmentWallet = new FragmentWallet(); // this should be in `FragmentPagerAdapter.getItem()`
        fragmentNavigationDrawer = new FragmentNavigationDrawer(); // this should be in `FragmentPagerAdapter.getItem()`

        viewPagerAdapter.addFragment(fragmentHome); // this can cause crashes
        viewPagerAdapter.addFragment(fragmentWallet); // this can cause crashes
        viewPagerAdapter.addFragment(fragmentNavigationDrawer); // this can cause crashes
        viewPager.setAdapter(viewPagerAdapter);
    }
}

View Pager Adapter code is

 public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>(); // this line can cause crashes

    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);  // this line can cause crashes
    }
}

It works good for navigating from one fragment to another, but it is not swiping the fragments. Please help,or should I have to switch some another solution.

like image 231
Kiran S Kulkarni Avatar asked Dec 08 '17 12:12

Kiran S Kulkarni


People also ask

Can I use ViewPager with views not with fragments?

The solution: start working with the ViewPager but without additional fragments, which means, creating a Pager Adapter extending PagerAdapter rather than either FragmentStatePagerAdapter (in my case) or FragmentPagerAdapter.

Can you put a ViewPager in a fragment?

Steps for implementing viewpager: Adding the ViewPager widget to the XML layout (usually the main_layout). Creating an Adapter by extending the FragmentPagerAdapter or FragmentStatePagerAdapter class.

How do I turn off swipe in ViewPager?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.

How do you get ViewPager fragments?

ViewPager save Fragment in FragmentManager with particular tags, So We can get ViewPager's fragment by FragmentManager class by providing tag. And ViewPager provide tag to each fragment by following syntax : Fragment page = getSupportFragmentManager(). findFragmentByTag("android:switcher:" + R.


1 Answers

Remove this part:

viewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
like image 140
Pavel Kuznetsov Avatar answered Oct 17 '22 06:10

Pavel Kuznetsov