我有一个使用ViewPager和FragmentPagerAdapter的TabLayout。此TabLayout有两个片段。在底部导航视图中连接了TabLayout和其他两个片段。并且在第一次启动时,将加载TabLayout。但是,从底部导航转到另一个片段之后,TabLayout不会加载任何内容。
下面是BottomNavigation活动中的代码
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelected = menuItem -> {
switch (menuItem.getItemId()) {
case R.id.navigation_tv:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
tvShow).commit();
fragment = tvShow;
title = "Tv";
position = 0;
break;
case R.id.navigation_film:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
filmShow).commit();
fragment = filmShow;
title = "Film";
position = 1;
break;
case R.id.navigation_favorit:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
favorits).commit();
fragment = favorits;
title = "Favorit";
position = 2;
break;
}
toolbar.setTitle(title);
return true;
};
下面是TabLayout中的代码
class Favorits : Fragment() {
private var position: Int? = null;
private var adapter: ViewPagerAdapter? = null
private var fragment: Fragment? = Fragment()
private lateinit var tabLayout: TabLayout
private lateinit var viewPager: ViewPager
/* Masukkan adapter untuk ViewPager */
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_favorits, container, false)
val toolbar = view.findViewById<Toolbar>(R.id.toolbar)
// placing toolbar in place of action bar
/*1. Membuat tabLayout*/
tabLayout = view.findViewById(R.id.tablayout)
viewPager = view.findViewById(R.id.viewpager)
adapter = ViewPagerAdapter(fragmentManager)
// 2. Adapter Setrup
viewPager.offscreenPageLimit = 2
viewPager.adapter = adapter
if (savedInstanceState == null) {
tabLayout.setupWithViewPager(viewPager)
} else {
fragment = fragmentManager?.getFragment(savedInstanceState, "fragment")
tabLayout.setupWithViewPager(viewPager)
}
tabLayout.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
position = tab.position
viewPager.currentItem = position as Int
fragment = adapter!!.getItem(position!!)
}
override fun onTabUnselected(tab: TabLayout.Tab) {
}
override fun onTabReselected(tab: TabLayout.Tab) {
}
})
return view
}
override fun onSaveInstanceState(outState: Bundle) {
if (fragment?.isAdded!!) {
fragmentManager?.putFragment(outState, "fragment", fragment!!)
}
super.onSaveInstanceState(outState)
}
第一个片段
public class FavoritShow extends Fragment {
private FavoritRecycler favoritRecycler;
private TvHelper tvHelper;
private ArrayList<Favorit> listFavorit = new ArrayList<>();
private static ProgressBar progressBar;
public FavoritShow() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_favorit, container, false);
RecyclerView recyclerView = view.findViewById(R.id.favorit_tv_recycler);
progressBar = view.findViewById(R.id.progress_bar);
recyclerView.setVisibility(View.INVISIBLE);
progressBar.setVisibility(VISIBLE);
LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
favoritRecycler = new FavoritRecycler(getContext());
tvHelper = new TvHelper(getContext());
tvHelper = TvHelper.getInstance(getContext());
tvHelper.open();
listFavorit = tvHelper.getAllFavorits();
favoritRecycler.setListFavorits(listFavorit);
recyclerView.setAdapter(favoritRecycler);
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(VISIBLE);
return view;
}
代码是否有问题?谢谢您的回答。
答案 0 :(得分:0)
检查FragmentVariable是否初始化为这样:
tvShow = new TvShow(); filmShow = new FilmShow(); favorits = new Favoirts();
或者您可以这样做
switch (menuItem.getItemId()) {
case R.id.navigation_tv:
TvShow tvShow = new TvShow();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
tvShow).commit();
title = "Tv";
position = 0;
break;
case R.id.navigation_film:
FilmShow filmShow = new FilmShow();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
filmShow).commit();
title = "Film";
position = 1;
break;
case R.id.navigation_favorit:
Favorits fovorits = new Favorits();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container_bottomnav,
favorits).commit();
fragment = favorits;
title = "Favorit";
position = 2;
break;
}