使用viewpager和SlidingTabLayout进行上一次单击时,选项卡选择器不起作用

时间:2015-11-15 02:57:45

标签: android android-fragments android-viewpager pagerslidingtabstrip

我正在尝试使用SlidingTabLayout实现viewpager。正向点击正在使用Tab Selector。但之前的点击不起作用。它只显示选中的上一个选项卡。我已经在论坛上提到过类似的问题。解决方案提供对我不起作用。我试图让它在过去3天没有任何成功。

gradle file:-
compile 'com.android.support:appcompat-v7:21.0.3'
compile "com.android.support:support-v4:21.+"

代码段: -

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT =8;
    private String titles[] ;

    public ViewPagerAdapter(FragmentManager fm, String[] titles2) {
        super(fm);
        titles=titles2;

    }


    @Override
    public Fragment getItem(int position) {
        switch (position) {
            // Open FragmentTab1.java
            case 0:
                return SampleFragment.newInstance(position);

            case 1:
                return SampleFragment.newInstance(position);

            case 2:
                return SampleFragment.newInstance(position);
            case 3:

                return SampleFragment.newInstance(position);
            case 4:
                return SampleFragment.newInstance(position);
            case 5:
                return SampleFragment.newInstance(position);
            case 6:
                return SampleFragment.newInstance(position);
            case 7:
                return SampleFragment.newInstance(position);

        }
        return null;
    }

    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

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



}

SampleFragment.java

public class SampleFragment extends Fragment {

    private GridView mGridView;


    private static final String ARG_POSITION = "position";

    private int position;

    public static SampleFragment newInstance(int position) {
        SampleFragment f = new SampleFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        position = getArguments().getInt(ARG_POSITION);


        switch (position) {

            case 0:
            case 1:

                Log.e("SampleFragment", "0");
                getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.container, GalleryFragment.getInstance(position) ).commit();

                break;


            case 2:

                getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.container, TextTutorialFragment.getInstance(position) ).commit();

                Log.e("SampleFragment", "2");
                break;
            case 3:

                Log.e("SampleFragment", "3");
                break;

            case 4:

                Log.e("SampleFragment", "4");
                break;

            case 5:

                Log.e("SampleFragment", "5");
                break;
        }

        //return rootView;
        return null;
    }
}

MainActivity.java

 ViewPager pager;
 private String titles[] = new String[]{"Tab1", "Tab2", "Tab3", "Tab4"
            , "Tab5", "Tab6", "Tab7", "Tab8"};

SlidingTabLayout slidingTabLayout;
pager = (ViewPager) findViewById(R.id.viewpager);

slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), titles));
slidingTabLayout.setViewPager(pager);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return Color.WHITE;
            }
        });

activity_main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:minHeight="?android:attr/actionBarSize" />



    <com.invensini.UTL.views.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" />



    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white"
        />

     <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.invensini.UTL.views.DrawerNavigationListView
            android:id="@+id/drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"/>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

当我点击tab2后点击Tab1时,我在android监视器中看到了folloiwng日志: -

11-15 10:19:09.751 339-415/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
11-15 10:19:11.141 339-415/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client

我正在使用以下示例中的代码: -

https://github.com/tekinarslan/AndroidMaterialDesignToolbar

请帮忙。

2 个答案:

答案 0 :(得分:1)

pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), titles));
page.setOnPageChangeListener(new OnPageChangeListener(){})
slidingTabLayout.setViewPager(pager);

答案 1 :(得分:0)

感谢微小的阳光工作。你是救世主。我工作了三天来解决这个问题。

不推荐使用setOnPageChangeListener,因此我以下列方式使用了addOnPageChangeListener。

代码段: -

Date