如何实现与NavigationDrawer和多个片段重复使用相同的TabLayout AppCompatActivity? [英] How do I implement AppCompatActivity with NavigationDrawer and multiple fragments reusing same TabLayout?
问题描述
我已经成功地使用 TabLayout 与AppCompatActivity与一个CoordinatorLayout看起来接近这个片段:
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:FAB =http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:layout_scrollFlags =啪
应用:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/> < android.support.design.widget.TabLayout
机器人:ID =@ + ID /标签
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
应用:tabGravity =补
应用:tabMode =固定/> < /android.support.design.widget.AppBarLayout> <包括布局=@布局/内容/>< /android.support.design.widget.CoordinatorLayout>
现在我实现了一个NavigationDrawer和我奋力实现选项卡中被显示我AppCompatActivity里面的片段中的一个。我希望能够几个孩子之间的片段与TabLayout切换这个片段里面。
- 如何从我的片段中的一个访问TabLayout?
- 如何正确设置PagerAdapter每个片段?
- 在哪里打电话addOnPageChangeListener?
- 如何隐藏TabLayout当我的片段中的一个并不需要
显示标签?
1。一级片段之间切换
假设布局 content.xml中
为:
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / fragment_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent
应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior
工具:上下文=。ui.MyActivity
工具:舒=@布局/ my_activity/>
然后,为了能够在片段之间进行切换,实现这个功能:
私人无效makeTransition(INT fragmentId){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 开关(fragmentId){
//片段与标签
案例FRAGMENT_TABS:
fragmentTransaction.replace(R.id.fragment_container,新TabsFragment());
//这说明TabLayout
findViewById(R.id.tabs).setVisibility(View.VISIBLE);
。getSupportActionBar()的setTitle(R.string.fragment_tabs_title);
打破;
//片段没有标签
案例FRAGMENT_NO_TABS:
fragmentTransaction.replace(R.id.fragment_container,新NoTabsFragment());
//这个隐藏TabLayout
findViewById(R.id.tabs).setVisibility(View.GONE);
。getSupportActionBar()的setTitle(R.string.fragment_no_tabs_title);
打破;
默认:
抛出新的RuntimeException(+ fragmentId +发现,ID为无分段);
}
fragmentTransaction.commit();
}
2。访问和从一级片段设置TabLayout
在 TabsFragment
类,添加一个私有类 TabAdapter
:
私有类TabAdapter扩展FragmentPagerAdapter { 公共TabAdapter(FragmentManager FM){
超(FM);
} @覆盖
公共CharSequence的getPageTitle(INT位置){
开关(位置){
情况下0:
返回TAB1
情况1:
返回TAB2
// ...
}
返回null;
} @覆盖
公共片段的getItem(INT位置){
开关(位置){
情况下0:
返回Tab1Fragment.getInstance();
情况1:
返回Tab2Fragment.getInstance();
// ...
}
返回null;
} @覆盖
公众诠释的getCount(){
返回2;
}
}
另外,可选实施 ViewPager.OnPageChangeListener
:
私有类FragmentPageChangeListener实现ViewPager.OnPageChangeListener { @覆盖
公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){
Log.d(的getClass()的getName(),onPageScrolled);
} @覆盖
公共无效使用onPageSelected(INT位置){
Log.d(的getClass()的getName(),使用onPageSelected);
} @覆盖
公共无效onPageScrollStateChanged(INT状态){
Log.d(的getClass()的getName(),onPageScrollStateChanged);
}
}
假设你的布局与标签片段是这样的:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < android.support.v4.view.ViewPager
机器人:ID =@ + ID / viewpager
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_alignParentTop =真/>< / RelativeLayout的>
覆盖 onCreateView
来是这样的:
@Nullable
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.viewpager_fragment,NULL); fragmentPagerAdapter =新TabAdapter(getChildFragmentManager());
fragmentPageChangeListener =新FragmentPageChangeListener(); ViewPager寻呼机=(ViewPager)view.findViewById(R.id.viewpager);
pager.setAdapter(fragmentPagerAdapter);
pager.addOnPageChangeListener(fragmentPageChangeListener); TabLayout tabLayout =(TabLayout)MyAcvtivity.getInstance()findViewById(R.id.tabs)。
tabLayout.setupWithViewPager(寻呼机); 返回视图。
}
注意:
使用 getChildFragmentManager()
,而不是 getFragmentManager()实例化一个ViewPager时
在一级片段。
I have successfully used TabLayout with AppCompatActivity with a CoordinatorLayout that looks close to this snippet:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/tools"
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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content" />
</android.support.design.widget.CoordinatorLayout>
Now I have implemented a NavigationDrawer and I am struggling to implement tabs within one of the Fragments being shown inside my AppCompatActivity. I want to be able to switch with TabLayout between few child Fragments inside of this Fragment.
- How do I access TabLayout from one of my Fragments?
- How do I set PagerAdapter for each of the Fragments correctly?
- Where do I call addOnPageChangeListener?
- How do I hide TabLayout when one of my Fragments does not need to display tabs?
1. Switching between first-level Fragments
Suppose layout content.xml
stands for:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.MyActivity"
tools:showIn="@layout/my_activity" />
Then, to be able to switch between the Fragments, implement this function:
private void makeTransition(int fragmentId) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (fragmentId) {
// Fragment with tabs
case FRAGMENT_TABS:
fragmentTransaction.replace(R.id.fragment_container, new TabsFragment());
// This shows TabLayout
findViewById(R.id.tabs).setVisibility(View.VISIBLE);
getSupportActionBar().setTitle(R.string.fragment_tabs_title);
break;
// Fragment with no tabs
case FRAGMENT_NO_TABS:
fragmentTransaction.replace(R.id.fragment_container, new NoTabsFragment());
// This hides TabLayout
findViewById(R.id.tabs).setVisibility(View.GONE);
getSupportActionBar().setTitle(R.string.fragment_no_tabs_title);
break;
default:
throw new RuntimeException("No fragment with ID " + fragmentId + " found");
}
fragmentTransaction.commit();
}
2. Accessing and setting up TabLayout from first-level Fragment
In TabsFragment
class, add a private class TabAdapter
:
private class TabAdapter extends FragmentPagerAdapter {
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "TAB1";
case 1:
return "TAB2";
// ...
}
return null;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Tab1Fragment.getInstance();
case 1:
return Tab2Fragment.getInstance();
// ...
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
Also, optionally, implement a ViewPager.OnPageChangeListener
:
private class FragmentPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d(getClass().getName(), "onPageScrolled");
}
@Override
public void onPageSelected(int position) {
Log.d(getClass().getName(), "onPageSelected");
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d(getClass().getName(), "onPageScrollStateChanged");
}
}
Suppose your layout for fragment with tabs is like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
Override onCreateView
to look like this:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_fragment, null);
fragmentPagerAdapter = new TabAdapter(getChildFragmentManager());
fragmentPageChangeListener = new FragmentPageChangeListener();
ViewPager pager = (ViewPager) view.findViewById(R.id.viewpager);
pager.setAdapter(fragmentPagerAdapter);
pager.addOnPageChangeListener(fragmentPageChangeListener);
TabLayout tabLayout = (TabLayout) MyAcvtivity.getInstance().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
return view;
}
NB:
Use getChildFragmentManager()
and not getFragmentManager()
in first-level Fragments when instantiating a ViewPager.
这篇关于如何实现与NavigationDrawer和多个片段重复使用相同的TabLayout AppCompatActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!