tablayout未正确显示BottomNavigation项目导航中的数据

时间:2017-09-26 10:16:39

标签: android android-viewpager fragment bottomnavigationview

我正在开展这个大学项目,我必须在这里创建一个职位门户应用程序 我已经使用了一个底部的导航视图,在其中一个菜单项中,我已经放置了一个标签布局,一旦我启动我的应用程序它工作正常,标签工作正常。但是,一旦我从一个菜单移动到另一个菜单并返回到菜单项,其中包含选项卡,则选项卡无法正常工作。 我在选项卡中使用了回收器视图,并且首先显示列表但是一旦我移动到第三个底部菜单,列表就不再显示 下面是我的标签布局和底部导航视图的代码

  private BottomNavigationView.OnNavigationItemSelectedListener 
  mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = 
   fragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.navigation_home:

                transaction.replace(R.id.content,new 
    HomeFragment()).commit();
                return true;
            case R.id.navigation_JobBoard:

                transaction.replace(R.id.content,new 
   JobBoardFragment()).commit();
                return true;
            case R.id.navigation_notifications:

                transaction.replace(R.id.content,new 
    NotificationBoardFragment()).commit();

                return true;
        }
        return false;
    }

 };

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


    BottomNavigationView navigation = (BottomNavigationView) 
 findViewById(R.id.navigation);





 navigation.setOnNavigationItemSelectedListener
 (mOnNavigationItemSelectedListener);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content,new HomeFragment()).commit();
     }

}

JOBBoardFragment类

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

    TabLayout tabs = (TabLayout) view.findViewById(R.id.result_tabs);
    tabs.addTab(tabs.newTab().setText("saved"));
    tabs.addTab(tabs.newTab().setText("Inbox"));
    tabs.addTab(tabs.newTab().setText("sent"));
    ViewPager pager = (ViewPager) view.findViewById(R.id.viewpager);
    //       SimpleFragmentPagerAdapter adapter = new 
  SimpleFragmentPagerAdapter(this, getSupportFragmentManager());
    //       setContentView(R.layout.activity_main);
    TabsPagerAdapter adapter = new TabsPagerAdapter(getFragmentManager());
    pager.setAdapter(adapter);
    tabs.setupWithViewPager(pager);
    return view;


}

布局文件

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"

 tools:context="com.example.jimmy.workmen.
   DashboardFragment.JobBoardFragment">

<!-- TODO: Update blank fragment layout -->


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout2">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/linearLayout2">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/Theme.Design.NoActionBar"
        app:expanded="true">

        <android.support.design.widget.TabLayout
            android:id="@+id/result_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:drawable/bottom_bar"

           android:theme="@style/Base.DialogWindowTitleBackground.AppCompat"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/holo_orange_dark"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="?attr/colorBackgroundFloating" />
    </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>


</RelativeLayout>

class tabspageradapter

 public class TabsPagerAdapter extends FragmentPagerAdapter {
private Context mContext ;
private String tabTitles[] = new String[]{"saved", "inbox", "sent"};

public TabsPagerAdapter( FragmentManager fm) {

    super(fm);
    //  mContext = context;

}

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.

    if (position == 0) {
        return new InboxFragment();
    } else if (position == 1){
        return new SavedFragment();
    } else if (position == 2){
        return new SentFragment();
    } else {
        return null;
    }


}

@Override

public int getCount() {
    return 3;
}

public CharSequence getPageTitle(int position) {
    return tabTitles[position];
}






}

1 个答案:

答案 0 :(得分:0)

这个问题有两种解决方案。

第一个解决方案是TabsPagerAdapter扩展FragmentStatePagerAdapter。虽然我不确定为什么会这样,但它可能与FragmentStatePagerAdapter的内存有效方法有关,也就是说当你再次回来时它可能会从头开始创建片段。

第二个解决方案是将getChildFragmentManager()传递给TabsPagerAdapter,因为ViewPager会隐藏嵌套的片段结构:

TabsPagerAdapter adapter = new TabsPagerAdapter(getChildFragmentManager());

我认为第二种解决方案是可行的,可以找到更多信息here