嵌入在片段viewpager中的TabLayout会创建空间

时间:2016-02-16 10:12:22

标签: android android-layout android-tablayout

下面的片段创建空间中的Tablayout检查图像enter image description here 环境:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
build
compileSdkVersion 22
buildToolsVersion "22.0.1"
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'

XML布局

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:visibility="gone"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:visibility="visible"
            android:background="@color/clr_black" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            style="@style/MyCustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:padding="0dp" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:visibility="visible"
            android:background="@color/clr_black" />

        <android.support.v4.view.ViewPager
            android:id="@+id/my_orders_page_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

片段类

  public class MyOrderFragment extends Fragment {

        private ViewPager pager;
        private MyOrderPagerAdapter adapter;
        private Context context;
        private TabLayout tabLayout;

        private static MyOrderFragment instance;

        public static MyOrderFragment getInstance() {
            if (instance == null) {
                instance = new MyOrderFragment();
            }
            return instance;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            this.context = context;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View myOdersView = inflater.inflate(R.layout.fragment_tab_layout1, container, false);

            pager = (ViewPager) myOdersView.findViewById(R.id.my_orders_page_viewpager);
            adapter = new MyOrderPagerAdapter(getChildFragmentManager(), context);
            pager.setAdapter(adapter);

            tabLayout = (TabLayout) myOdersView.findViewById(R.id.tab_layout);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab selectedTab) {
                }

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

                }

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

                }
            });
            pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

            tabLayout.post(new Runnable() {
                @Override
                public void run() {
                    tabLayout.setupWithViewPager(pager);
                }
            });

            return myOdersView;
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            if ( this.instance != null && getFragmentManager().findFragmentById(this.instance.getId()) != null) {
                getFragmentManager().beginTransaction().remove(this.instance).commit();
                this.instance = null;
            }
        }

        @Override
        public void onDetach() {
            super.onDetach();

        }

    }

适配器

public class MyOrderPagerAdapter extends FragmentStatePagerAdapter {

    private CharSequence[] tabs;
    private Context context;

    public MyOrderPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
        tabs = context.getResources().getStringArray(R.array.my_orders_tabs);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return InTransitFragment.getInstance();
            case 1:
                return UpComingTripFragment.getInstance();
            case 2:
                return DeliveredFragment.getInstance();
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

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

}

问题:

在图片中,您可以看到渲染过程中创建的空白区域,上面的片段被FrameLayout容器替换。我无能为力我改变了主题,它在棒棒糖版本中工作得很好,但在下面它创造了空间。建议非常感谢。

1 个答案:

答案 0 :(得分:0)

ViewPager必须在AppBarLayout之外。

<android.support.design.widget.CoordinatorLayout>

   <android.support.design.widget.AppBarLayout>
    .....
   </android.support.design.widget.AppBarLayout>

   <android.support.v4.view.ViewPager
            android:id="@+id/my_orders_page_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>