swipe tablayout is not working correctly

765 Views Asked by At

i am using "compile 'com.android.support:design:25.1.0'" to implement swipe tablayout. My code has no syntax error but due to some logical error it is not working correctly Problems: 1) when i slide a page it should move on next tab but it slide or move very slowly and go between two tabs 2) when i come back on privious tabs sometime it does not show even any page and sometime it show wrong page e.g on second tabs it shows third page(fragment) MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tabLayout;
       String[] tabs={"SCORES","NEWS","SERIES"};
        tabLayout=(TabLayout) findViewById(R.id.cricTabLayout);
        tabLayout.addTab(tabLayout.newTab().setText(tabs[0]));
        tabLayout.addTab(tabLayout.newTab().setText(tabs[1]));
        tabLayout.addTab(tabLayout.newTab().setText(tabs[2]));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabTextColors(Color.parseColor("#627179"), Color.parseColor("#BF4A32"));

        final ViewPager viewPager=(ViewPager)findViewById(R.id.cricPagerView);

        final PageAdapter pageAdapter=new PageAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(pageAdapter);
        // new line
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
       // viewPager.clearOnPageChangeListeners();
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
               // Toast.makeText(MainActivity.this,"Tab changed",Toast.LENGTH_LONG).show();
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

PageAdapter.java

public class PageAdapter extends FragmentStatePagerAdapter {

    int numOfTabs;

    public PageAdapter(FragmentManager fm, int numOfTabs) {
        super(fm);
        this.numOfTabs=numOfTabs;
    }

    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new ScoreFragment();
            case 1:
                return new NewsFragment();
            case 2:
                return new SeriesFragment();
            default: return null;
        }

    }

    @Override
    public int getCount() {
        return numOfTabs;
    }
}

ScoreFragment.java

public class ScoreFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=null;
        Toast.makeText(getContext(),"Score",Toast.LENGTH_LONG).show();
        return view;
    }
}

SeriesFragment.java

public class ScoreFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=null;
        Toast.makeText(getContext(),"Score",Toast.LENGTH_LONG).show();
        return view;
    }
}

NewsFragment.java

public class NewsFragment extends Fragment {
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=null;
            Toast.makeText(getContext(),"News",Toast.LENGTH_LONG).show();
            return view;
        }
    }

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_news"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.apple.crickapp.MainActivity"
    android:layout_marginLeft="0dp">

    <android.support.design.widget.TabLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:elevation="8dp"
        android:background="@color/colorBlack"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/cricTabLayout"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/cricPagerView"
        android:layout_below="@id/cricTabLayout"
        >
    </android.support.v4.view.ViewPager>

</RelativeLayout>
0

There are 0 best solutions below