滑动tablayout无法正常工作

时间:2016-12-26 15:53:17

标签: java android android-tablayout

我正在使用"编译com.android.support:design:25.1.0'"实现滑动tablayout。我的代码没有语法错误,但由于某些逻辑错误,它无法正常工作 问题:1)当我滑动页面时,它应该在下一个选项卡上移动,但它会滑动或移动得很慢并且在两个选项卡之间移动 2)当我回到私有标签时,它甚至没有显示任何页面,有时它显示错误页面,例如在第二个标签页上显示第三页(片段) 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 个答案:

没有答案