我的活动中有一个tabLayout
。单击选项卡后,将显示包含片段的viewpager
。我试图引用viewpager的片段,但每次都得到一个null pointer
。如何从活动中获取viewPager片段的实例?
方法一:Fragment fragment = new Fragment();
方法二:Fragment fragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem());
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private int no_of_tabs;
public ViewPagerAdapter(FragmentManager fm, int no_of_tabs) {
super(fm);
this.no_of_tabs = no_of_tabs;
}
@Override
public Fragment getItem(int position) {
return new Fragment();
}
@Override
public int getCount() {
return no_of_tabs;
}
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseFirestore = FirebaseFirestore.getInstance();
final ViewPager viewPager = findViewById(R.id.viewPager);
final TabLayout tabLayout = findViewById(R.id.tabLayout);
firebaseFirestore.collection("Categories")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getString("title") + " => " + document.getData());
tabLayout.addTab(tabLayout.newTab().setText(document.getString("title")));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem());
viewPager.setOffscreenPageLimit(4);
fragment.setTextViewText();
viewPager.setAdapter(categoriesPagerAdapter);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(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) {
}
});
}
}
答案 0 :(得分:0)
这样做
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + viewPager.getCurrentItem());
用您的viewpager ID替换R.id.viewpager
由于尚未创建片段UI,因此无法在onCreate
方法内使用此代码。
在这种情况下,您可以这样做
viewPager.setOffscreenPageLimit(4); // this forces to load 4
setOffscreenPageLimit
用于强制适配器在创建第一个页面后再创建4个页面。默认值为1。这仅对访问未选择的页面有用。
如果在创建页面时需要更新片段UI,则可以在onCreate
...
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(x);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
// here you can set you UI values from page 'i'.
// i = page index
}
@Override
public void onPageSelected(int i) {
// here you can set you UI values from the selected page
// i = page index
// for the first selected page (before scrolling), use onPageScrolled
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
第一次显示viewpager
片段时,不会触发onPageSelected
,而会触发onPageScrolled
。因此,您可以使用这两种方法来更新片段UI。