使用PagerAdapter的SlidingTabLayout不会滑动

时间:2015-02-11 09:04:17

标签: android android-fragments android-viewpager navigation-drawer pagerslidingtabstrip

我正在实施一些来自Google的代码(SlidingTabLayoutSlidingTabStrip,可以在线轻松找到),我尝试将其与ViewPager一起使用以获取我的滑动标签Fragment。问题是,我在我的主标签片段类中使用PagerAdapter作为扩展,用于我的适配器以显示标签中的内容的视图。我能够获得片段CuteCollectionFragment.java中工作标签的基本视图,但是当我点击它们时,它们通常会点击2次以突出显示新标签,而且您无法滑动以获得新标签标签。我怀疑是因为基本代码(通常用于主要活动)必须放在Fragment中(因为我的Navigation Drawer),它与我的Navigation Drawer冲突,已经滑动了。但这只是猜测。

基本上,我必须使用Fragments,而不是活动。但它们如何相互冲突,我该如何解决?我发现在线的所有示例都希望您使用FragmentPagerAdapter代替PagerAdapter作为我的适配器的扩展名,但我不能这样做,因为它要求您使用FragmentManager是支持v.4。我无法在我的CuteMainActivity(具有我的导航抽屉代码的那个)中使用它,因为我无法点击每个导航抽屉菜单项并转到Fragment,我在Fragment fragment部分的switch语句中出现红色错误,表示我的片段必须全部扩展FragmentActivity,但他们不能使用导航抽屉,他们必须扩展Fragment

无论如何,天哪,这太令人困惑了。主要是我只想知道两件事:

  1. 为什么我的标签没有滑动(为什么它们需要2次点击)

  2. 如何连接内部TabsAdapter类以显示我的选项卡类(全部为Fragments)。我的标签片段类/ xmls都已准备就绪(例如PhotoTab.java,VideoTab.java等),但如何将它们挂钩到标签中的视图?

  3. 如果您想跑步并亲眼看看,这是我的整个项目。 ICollectCute app

    我只会在Fragment下面包含我试图将滑块标签代码放入其中,以及它们从中绘制的xmls。如果您需要更多文件,请与我们联系。您可能需要(但可能不是)的唯一其他内容是名为SlidingTabLayoutSlidingTabStrip的Google类,或者我的CuteMainActivity,其中我放置了所有导航抽屉代码。但是查找添加/错误的主要位置是我的CuteCollectionFragment类,因为滑动标签需要在那里实现。

    我帮助从this S.O. post获取代码。

    感谢您的帮助!

    CuteCollectionFragment.java

    package org.azurespot.cutecollection;
    
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.os.Bundle;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import org.azurespot.R;
    
    /**
     * Created by mizu on 1/26/15.
     */
    public class CuteCollectionFragment extends Fragment {
    
        static final String LOG_TAG = "SlidingTabsBasicFragment";
        private SlidingTabLayout mSlidingTabLayout;
        private ViewPager mViewPager;
        FragmentManager fragmentManager;
    
        View photoView;
        View videoView;
        View audioView;
        View textView;
    
        private static final int PHOTO_TAB = 0;
        private static final int VIDEO_TAB = 1;
        private static final int AUDIO_TAB = 2;
        private static final int TEXT_TAB = 3;
    
    
    
        public CuteCollectionFragment(){}
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.fragment_cute_collection,
                                                                container, false);
    
            photoView = inflater.inflate(R.layout.photo_tab,
                    container, false);
    
            videoView = inflater.inflate(R.layout.video_tab,
                    container, false);
            audioView = inflater.inflate(R.layout.audio_tab,
                    container, false);
            textView = inflater.inflate(R.layout.text_tab,
                    container, false);
    
    //      Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
    
            fragmentManager = getActivity().getFragmentManager();
    
            mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
            mViewPager.setAdapter(new TabsAdapter());
            mSlidingTabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);
            mSlidingTabLayout.setViewPager(mViewPager);
    
    
            return rootView;
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
    //
        }
    
        class TabsAdapter extends PagerAdapter {
    
            /**
             * @return the number of pages (tabs) to display
             */
            @Override
            public int getCount() {
                return 5;
            }
    
            /**
             * @return true if the value returned from
             *         {@link #instantiateItem(ViewGroup, int)} is the same object
             *         as the {@link View} added to the {@link ViewPager}.
             */
            @Override
            public boolean isViewFromObject(View view, Object o) {
                return o == view;
            }
    
            // BEGIN_INCLUDE (pageradapter_getpagetitle)
            /**
             * Return the title of the item at {@code position}. This is important
             * as what this method returns is what is displayed in the
             * {@link SlidingTabLayout}.
             * <p>
             * Here we construct one using the position value, but for real
             * application the title should refer to the item's contents.
             */
            @Override
            public CharSequence getPageTitle(int position) {
                return "Item " + (position + 1);
    
            }
    
            // END_INCLUDE (pageradapter_getpagetitle)
    
            /**
             * Instantiate the {@link View} which should be displayed at
             * {@code position}. Here we inflate a layout from the apps resources
             * and then change the text view to signify the position.
             */
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // Inflate a new layout from our resources
    
                View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                        container, false);
                // Add the newly created View to the ViewPager
                container.addView(view);
    
                // Retrieve a TextView from the inflated View, and update it's text
                TextView title = (TextView) view.findViewById(R.id.item_title);
                title.setText(String.valueOf(position + 1));
    
    //            Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]");
    
                // Return the View
                return view;
            }
    
            /**
             * Destroy the item from the {@link ViewPager}. In our case this is
             * simply removing the {@link View}.
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
    //            Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
            }
    
        }
    
    }
    

    fragment_cute_collection.xml

    <?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"
        android:background="#2198bb" >
    
        <!--<android.support.v7.widget.Toolbar-->
            <!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
            <!--android:id="@+id/toolbar"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:minHeight="?attr/actionBarSize"-->
            <!--app:theme="@style/ThemeOverlay.AppCompat.ActionBar">-->
    
        <org.azurespot.cutecollection.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <!--</android.support.v7.widget.Toolbar>-->
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:background="@android:color/white" />
    
    
    </RelativeLayout>
    

    pager_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
    
        <TextView
            android:id="@+id/item_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Page:"/>
    
        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="80sp" />
    
    </LinearLayout>
    

    这是我的一个适配器类。每个选项卡都有自己的类和适配器类。这是一个(虽然还没有与CuteCollectionFragment.java联系,因为我不知道如何):

    PhotoTab.java

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.GridView;
    
    import org.azurespot.R;
    
    /**
     * Created by mizu on 2/8/15.
     */
    public class PhotoTab extends Fragment{
    
        private GridView gridView;
        private GridViewPhotoAdapter gvPhotoAdapter;
    
        public PhotoTab(){}
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.photo_tab, container, false);
    
            // with fragments, make sure you include the rootView when finding id
            gridView = (GridView) v.findViewById(R.id.photo_grid);
            // Create the Custom Adapter Object
            gvPhotoAdapter = new GridViewPhotoAdapter(getActivity());
            // Set the Adapter to GridView
            gridView.setAdapter(gvPhotoAdapter);
    
            return v;
        }
    }
    

    PhotoTab.java的适配器, GridViewPhotoAdapter.java

    package org.azurespot.cutecollection;
    
    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    
    import org.azurespot.R;
    
    /**
     * Created by mizu on 2/5/15.
     */
    public class GridViewPhotoAdapter extends ArrayAdapter {
    
        public Context context;
        public ImageView mediaPhoto;
    
    
        public GridViewPhotoAdapter(Context context) {
            super(context, 0);
            this.context = context;
    
        }
    
        public int getCount() {
            return 24;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View row = convertView;
    
            if (row == null)
            {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.photo_grid_row, parent, false);
    
                mediaPhoto = (ImageView) row.findViewById(R.id.photo_view);
    
                mediaPhoto.setImageResource(R.drawable.ic_collection_add);
    
            }
    
            return row;
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我也遇到过这个问题。我通过使用SlidingTabLayou和SlidingTabTrip从这个https://github.com/google/iosched/tree/0a90bf8e6b90e9226f8c15b34eb7b1e4bf6d632e/android/src/main/java/com/google/samples/apps/iosched/ui/widget开始工作。 感谢您进行Google I / O更新。