我正在這個大學項目中,我必須使一個工作門戶應用 我已經使用了一個底部的導航視圖,並在我已經放置了一個選項卡布局一旦我開始我的應用程序它工作正常,並且標籤工作正常。但是,一旦我從一個菜單移動到另一個菜單項並返回到帶有標籤的菜單項,標籤就無法正常工作。 我已經在標籤中使用的回收視圖,並在第一個顯示的列表中,但一旦我移動到第三個底部菜單列表中不再顯示 下面是我的標籤佈局的代碼和底部的導航視圖tablayout沒有正確顯示數據導航的BottomNavigation項目
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>
類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];
}
}
你可以分享'TabsPagerAdapter'嗎? – Mehmed
我已經添加了TabsPagerAdapter類 – depp
順便說一句,您不需要在'onCreateView()'中調用這三個'addTab()'調用,它們已經由您的'TabsPagerAdapter'創建。 – Mehmed