Fragment中的Android TabLayout

时间:2016-01-28 10:28:12

标签: android android-fragments android-recyclerview android-tablayout

我正在使用Navigation Drawer在他们都使用MainActivity工具栏的片段之间导航,但每个片段都有自己的TabLayout,其中有3个标签,并且在每个标签中我使用{{1}显示不同的文字和图像。

我最后一次使用actvities而不是片段,但现在使用抽屉,它使用片段。

我已经到了标签工作的位置,但是只要我将RecyclerView放入并将适配器连接到选项卡,然后我从抽屉打开新的片段,我得到一个空指针可能是什么问题在这里?

MainActivity.java

ViewPager

activity_main.xml中

public class MainActivity extends AppCompatActivity

    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return id == R.id.action_settings || super.onOptionsItemSelected(item);

}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    Fragment fragment;

    if (id == R.id.fragment_one) {
        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    } else if (id == R.id.fragment_two) {

        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    } else if (id == R.id.fragment_three) {

        fragment = new NewFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

NewFragment.java

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.xcandy.guideforfifa17.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

activity_fragment.xml

public class NewFragment extends Fragment {

private RecyclerView mRecyclerView;


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

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("A"));
    tabLayout.addTab(tabLayout.newTab().setText("B"));
    tabLayout.addTab(tabLayout.newTab().setText("C"));

    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);


    viewPager.setAdapter(new PagerAdapter
            (getFragmentManager(), tabLayout.getTabCount()));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });

    return view;
}

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }



    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:

                RecyclerView.Adapter mAdapter = new AdapterOne();
                mRecyclerView.setAdapter(mAdapter);

            case 1:

                mAdapter = new AdapterTwo();
                mRecyclerView.setAdapter(mAdapter);

            case 2:

                mAdapter = new AdapterThree();
                mRecyclerView.setAdapter(mAdapter);

            default:
                return null;
        }
    }

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

错误记录

<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".NewFragment">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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="wrap_content"
    android:background="@android:color/white"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

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

6 个答案:

答案 0 :(得分:22)

在查看了你的代码之后,我发现你忘了在PagefAdapter的 getItem 方法中返回一个片段,你从FragmentStatePagerAdapter中重写,并且每种情况下都没有break语句,因为每次都是返回了一个空值。

为此,您需要为ViewPager中的每个页面创建另一个新片段,或者您可以创建一个可在ViewPager的所有页面中使用的新片段。

您无法在已在父片段中定义的视图寻呼机的选项卡中使用相同的回收站视图。

activity_fragment_one.xml

 <android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".NewFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:tabTextAppearance="?android:attr/textAppearanceMedium"
            app:tabTextColor="#ffffff"
            android:layout_height="?android:actionBarSize"
            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="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="@android:color/white"/>
</android.support.design.widget.CoordinatorLayout>

我修复了你的片段并创建了一个可以在ViewPager Pages中使用的新片段。

public class NewFragment extends Fragment {

private RecyclerView mRecyclerView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_fragment_one, container, false);
    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("A"));
    tabLayout.addTab(tabLayout.newTab().setText("B"));
    tabLayout.addTab(tabLayout.newTab().setText("C"));
    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
   // mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    //mRecyclerView.setLayoutManager(mLayoutManager);
    viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount()));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });

    return view;
}

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }


    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
              return new FragmentTab();
            case 1:
                return new FragmentTab();
            case 2:
                return new FragmentTab();

            default:
                return null;
        }
    }

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

View Viewr中每个页面的FragmentTab(或者您可以根据需要为每个页面创建不同的片段)。

public class FragmentTab extends Fragment {
RecyclerView mRecyclerView;

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

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
    RecyclerView.Adapter mAdapter = new AdapterOne();
    mRecyclerView.setAdapter(mAdapter);
    return view;
}
}

fragment_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</LinearLayout>

答案 1 :(得分:4)

使用getChildFragmentManager()代替getSupportFragmentManager()

答案 2 :(得分:2)

如果没有错误日志,很难说出NullPointerException的位置和原因。

引起我注意的第一件事是您没有关联TabLayoutViewPager。为ViewPager设置适配器后,请使用以下代码:

// setUpWithViewPager doesn't work without using a Runnable interface.
// Support library bug, maybe?
tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

我不确定这是不是问题,但试一试:)

答案 3 :(得分:1)

我建议在

中使用getChildFragmentManager代替getFragmentManager

viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount()));

答案 4 :(得分:1)

嗯,前段时间我实现了完全相同的应用程序设计。我有一个片段,用于保存Tabs和ViewPager。这是一个棘手的任务,我记得我遇到了类似的问题。以下是我解决它的方法:

布局顺序为:

Inventory_ID

您与主要活动无关。所有的东西都是碎片。所以摆脱Activity -> Fragment -> Tab Layout + ViewPager -> FragmentStatePagerAdapter -> Fragment -> RecyclerView中的TabLayout。

您必须处理寻呼机内的嵌套片段。所有页面内容必须在那里,并且TabLayout必须与ViewPager所在的布局相同。

我的项目有点陈旧,如果有意义,我使用了activity_main.xml。我从你不需要的数据中删除了数据源,所以你可能会看到一些孤立的方法。

任务是显示带有类别的价格表,并在自己的页面上呈现每个类别的产品,以便水平滑动可以切换类别和垂直滑动 - 列出产品。

ViewPager和TabLayout的每个片段都有以下android.support.v4.app.Fragment布局:

subfragment_price_list_pager.xml

以下是<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView 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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/icons" app:cardCornerRadius="4dp" app:cardElevation="4dp" app:cardPreventCornerOverlap="true"> <android.support.v7.widget.Toolbar android:id="@+id/priceListToolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="4dp" android:theme="@style/AppTheme.Toolbar.TabLayout" app:navigationIcon="@drawable/ic_tag_multiple_grey600_24dp" tools:ignore="UnusedAttribute"> <android.support.design.widget.TabLayout android:id="@+id/tabBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </android.support.v7.widget.Toolbar> <android.support.v4.view.ViewPager android:id="@+id/priceList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="58dp" android:padding="5dp" android:scrollbars="horizontal" /> </android.support.v7.widget.CardView>

PriceListPagerFragment.class

自定义public class PriceListPagerFragment extends Fragment { private static final String TAG = "PriceListPagerFragment"; private PriceList mPriceList; private ViewPager mCategoriesViewPager; private TabLayout tabLayout; private Loader<PriceList> priceListLoader; private EditText scanerBarcodeText; private OnCartActionListener mListener; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.subfragment_price_list_pager, container, false); // Pager that would be inflated with page fragments. mCategoriesViewPager = (ViewPager) rootView.findViewById(R.id.priceList); tabLayout = (TabLayout) rootView.findViewById(R.id.tabBar); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); // Do all the stuff with pager here: updatePriceListPages(); return rootView; } private void updatePriceListPages() { if (mCategoriesViewPager != null && mPriceList != null) { Log.d(TAG, this + "updatePriceListPages"); if (mCategoriesViewPager.getAdapter() == null) { PriceListCategoriesAdapter adapter = new PriceListCategoriesAdapter( getFragmentManager(), tabLayout ); mCategoriesViewPager.setAdapter( adapter ); // Each page is a content of a single category of one-level catalog // We did not supply catalog to adapter before it is bound to ViewPager for some serious reason which I can't recall now adapter.setPriceList(mPriceList); tabLayout.setupWithViewPager(mCategoriesViewPager); } else { ((PriceListCategoriesAdapter) mCategoriesViewPager.getAdapter()).setPriceList(mPriceList); } } } } 实施,FragmentStatePagerAdapter

PriceListCategoriesAdapter.class

最后public class PriceListCategoriesAdapter extends FragmentStatePagerAdapter { private static final String TAG = "PriceListCategoriesAd"; private final TabLayout mTabLayout; private FragmentManager mFm; private Vector<PriceListFragment> recyclerViewList; private Vector<String> titles; public PriceListCategoriesAdapter(FragmentManager fm, TabLayout tabLayout) { super(fm); mFm = fm; mTabLayout = tabLayout; recyclerViewList = new Vector<>(); titles = new Vector<>(); } // All the magic with nested page fragments is done here public void setPriceList(PriceList priceList) { boolean updateTabs = false; Vector<String> newTitles = new Vector<>(); int position = 0; for (CatalogItem catalogItem : priceList.getCatalogs()) { if (catalogItem.getProducts() == null) continue; boolean newFragment; PriceListFragment fragment; try { fragment = recyclerViewList.get(position); newFragment = false; } catch (ArrayIndexOutOfBoundsException e) { fragment = new PriceListFragment(); newFragment = true; } fragment.setCatalogItem(catalogItem); newTitles.add(catalogItem.getName()); if (newFragment) { recyclerViewList.add(fragment); } position++; } if (titles.size() != newTitles.size()) { updateTabs = true; } else { for (int position = 0; position < titles.size(); position++) { if (!titles.get(position).equals(newTitles.get(position))) { updateTabs = true; break; } } } titles = newTitles; notifyDataSetChanged(); if (updateTabs) mTabLayout.setTabsFromPagerAdapter(this); } @Override public Fragment getItem(int position) { return recyclerViewList.get(position); } @Override public int getItemPosition(Object object) { PriceListFragment fragment = (PriceListFragment) object; String title = (String) fragment.getTitle(); int position = titles.indexOf(title); if (position >= 0) { return position; } else { return POSITION_NONE; } } @Override public int getCount() { return titles.size(); } @Override public CharSequence getPageTitle(int position) { return recyclerViewList.get(position).getTitle(); } // We don't keep states @Override public Parcelable saveState() { return null; } // We don't keep states @Override public void restoreState(Parcelable state, ClassLoader loader) { } } 布局:

PriceListFragment

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView android:id="@+id/categoryList" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> 本身没什么特别的。只需用适当的数据实例化它的RecyclerView即可。

答案 5 :(得分:0)

getItem中的PagerAdapter方法错误,它返回Fragment init 您应该返回Fragment。在片段中,您setView recyclerView到它