我使用viewpager创建了一个标签布局。 一切都很好,除了我需要在特定的时刻运行一个方法。所以我需要获取片段实例并运行他们的方法。 我这样创造:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
activateToolbarWithNavigationView(HomeActivity.this);
// Tabs Setup
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.home_pager);
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.favorites_label_fragment)).setTag(getString(R.string.fragment_favorite_tag)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.air_today_label_fragment)).setTag(getString(R.string.fragment_airing_today_tag)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final HomePageAdapter adapter = new HomePageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
if (viewPager != null) {
viewPager.setAdapter(adapter);
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) {
}
});
}
}
public void refreshFavorites(){
FavoritesFragment favoritesFragment = (FavoritesFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_favorite_tag));
if(favoritesFragment != null) favoritesFragment.executeFavoriteList();
}
我不知道我是以错误的方式做错,还是有一些错误,他们从findFragmentByTag返回null ...我无法弄明白。如果我已经检查了其他一些答案,但我无法理解我真正需要做的事情。
viewpager适配器:
public class HomePageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public HomePageAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FavoritesFragment favoritesFragment = new FavoritesFragment();
return favoritesFragment;
case 1:
AirTodayFragment airTodayFragment = new AirTodayFragment();
return airTodayFragment;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
我的xml:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ad_view_home">
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?actionBarSize"
android:theme="@style/ActionBarThemeOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:logo="@mipmap/ic_launcher_initials"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/ActionBarThemeOverlay"
app:titleTextAppearance="@style/ActionBar.TitleText">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/app_bar_layout"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/home_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
我如何解决:
public void refreshFavorites(){
List<Fragment> allFragments = getSupportFragmentManager().getFragments();
for (Fragment fragmento: allFragments) {
if (fragmento instanceof FavoritesFragment){
((FavoritesFragment) fragmento).executeFavoriteList();
}
}
}
答案 0 :(得分:9)
您没有为片段设置标记,这是您获得null
而不是使用,
FavoritesFragment favoritesFragment = (FavoritesFragment) getSupportFragmentManager()
.findFragmentByTag(getString(R.string.fragment_favorite_tag));
使用此,
FavoritesFragment favoritesFragment = (FavoritesFragment) getSupportFragmentManager()
.getFragments()
.get(0);
获取FavoritesFragment的实例。
我已将get(0)
置于FavoritesFragment
实例的位置为零。
您可以在位置1调用AirTodayFragment
get(1)
个实例