Android系统。 Tablayout位于另一个tablayout的标签内

时间:2016-08-18 14:46:13

标签: android android-fragments android-viewpager slide android-tablayout

是否可以在另一个tablayout的标签内设置tablayout?我创建了以下图像以获得更好的解释。所需的滑动运动如图所示。

enter image description here

我的主要活动

XML

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

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

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

代码

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;

    private final static int[] tabIcons = {
            R.drawable.ic_tab_1,
            R.drawable.ic_tab_2,
            R.drawable.ic_tab_3,
            R.drawable.ic_tab_4
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Fragment1());
        adapter.addFrag(new Fragment2());
        adapter.addFrag(new Fragment3());
        adapter.addFrag(new Fragment4());
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragmentList = new ArrayList<>();

        ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);
        }
    }
}

片段

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"
    android:background="@android:color/holo_blue_light"
    android:id="@+id/fragment_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="myContext">

    <TextView
        android:text="It is just a text."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

代码

public class Fragment1 extends Fragment {

    public Fragment1() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment.
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

Fragment2_A和Fragment2_B应该如何使用XML和Java代码?

3 个答案:

答案 0 :(得分:5)

好的,所以你的片段中只有TextView。现在为fragment_2添加了另一个TabLayoutViewPager(您不需要AppBarCoordinator,它们将来自{{}的父布局1}})

frag2.xml

Activity

您必须创建<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/frag2_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill"/> <android.support.v4.view.ViewPager android:id="@+id/frag2_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> Frag2,在Fragment内使用上述布局初始化,然后为OnCreateView frag2_viewpager类创建ViewPager的另一个适配器}和frag2a.xml frag2b.xml,他们可能只允许假设Fragments或其他。 TextView将放置在TabLayout内部(Frag2中的一个,如图所示)

答案 1 :(得分:3)

我知道这不是问题的直接答案,我可能会因此而受到惩罚,但你不应该在标签中嵌套标签。 Google使用称为材料设计的设计概念。制表符的一个规则是不嵌套它们。以下是该页面的链接:https://material.google.com/components/tabs.html#tabs-usage。我认为我们作为开发人员遵循这一点非常重要,以使Android平台具有普及性和惊人性。

谢谢, 橡木

答案 2 :(得分:0)

简单

所有都将保持相同,就像您在“活动”中设置 ViewPager 一样。只是您需要在Fragment中使用getChildFragmentManager()而不是getFragmentManager()

来自getChildFragmentManager() documentation

  

返回一个私有的FragmentManager来放置和管理Fragments   在该片段的内部。

来自getFragmentManager() documentation

  

返回FragmentManager以与关联的片段进行交互   这个片段的活动。

您可以看到@this answer以获得完整的代码。

相关问题