2015-01-07 39 views
0

我是Fragments的新手。我正在尝试开发滑动选项卡。从Activity调用Fragment方法,findFragmentById返回null

我有以下适配器:

public class HomeTabsPagerAdapter extends FragmentPagerAdapter { 

    public HomeTabsPagerAdapter(android.support.v4.app.FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public android.support.v4.app.Fragment getItem(int idx) { 
     switch (idx) { 
     case CONSTS_MAIN.TAB_WATCH: 
      return new WatchFragment(); 
     case CONSTS_MAIN.TAB_DISCOVER: 
      return new DiscoverFragment(); 
     case CONSTS_MAIN.TAB_LOG: 
      return new LogFragment(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return CONSTS_MAIN.HOME_NO_OF_TABS; 
    } 

} 

和MainActivity.java

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 
} 

,当然,分片类:

public class WatchFragment extends Fragment { 
public class DiscoverFragment extends Fragment { 
public class LogFragment extends Fragment { 

,基本上一切似乎做工精细图形。

但是,我试图从活动中调用碎片的方法。我这样做:

FragmentManager fm = getSupportFragmentManager(); 
WatchFragment watchFragment = (WatchFragment) fm.findFragmentById(R.id.watch_fragment); 
watchFragment.refresh(); 

和这里fm.findFragmentById总是返回null。

我一直在使用android.support.v4.app

如何获得片段类?

编辑:

这里是片段的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id ="@+id/watch_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:label="@string/title_activity_watch" 
    android:orientation="vertical" > 

    <com.x.y.custom_controls.CustomWatchChronologicalListView 
     android:id="@+id/lvWatchChronologicalDiscussions" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

,这里是活动的布局

<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"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </android.support.v4.view.ViewPager> 

</RelativeLayout> 
+0

也许你正在使用'WatchFragment watchFragment =(WatchFragment)fm.findFragmentById(R.id.watch_fragment)ID;'不正确。你确定'WatchFragment'被加载到'R.id.watch_fragment'?请检查。 – Rohit5k2

+0

你如何处理碎片?你是动态还是静态地将它们添加到XML布局中? –

+0

我添加了片段和活动的布局。 @Rohit请看看Id是否不正确。 – dsb

回答

0

使用此:

在适配器中添加一个SparseArray到继续参考添加的片段

private final SparseArray<Fragment> mPageReferences = new SparseArray<Fragment>(); 

在你的getItem方法之后实例化的片段将它添加到阵列:

@Override 
public android.support.v4.app.Fragment getItem(int idx) 
{ 
    Fragment fragment = null; 

    switch (idx) 
    { 
     case CONSTS_MAIN.TAB_WATCH: 
      fragment = new WatchFragment(); 
     case CONSTS_MAIN.TAB_DISCOVER: 
      fragment = new DiscoverFragment(); 
     case CONSTS_MAIN.TAB_LOG: 
      fragment = new LogFragment(); 
    } 
    if(fragment != null) { 
     mPageReferences.put(position, fragment); 
    } 
    return fragment; 
} 

并添加以下方法:

@Override 
    public void destroyItem(ViewGroup container, int position, Object object) 
    { 
     super.destroyItem(container, position, object); 
     mPageReferences.remove(position); 
    } 

    public SherlockFragment getFragment(int key) { 
     return mPageReferences.get(key); 
    } 

最后你可以在索引中的片段,你想要这样:

Fragment fragment = (Fragment)adapter.getFragment(index); 

如果你想用键来识别碎片而不是int索引,你可以使用一个HashMap

祝你好运!

+0

工作!谢谢。 – dsb

0

如果您未在xml中使用片段标记定义片段,则无法执行findFragmentById。 在这里,您将相对布局的id传递给方法findFragmentById。 的方法是使用你得到的片段例如从fragmentMnager-

WatchFragment watchFragment=(WatchFragment)getFragment(0); 
    watchFragment.refresh(); 


    //getFragment Method 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    List<Fragment> fragmentList=fragmentManager.getFragments(); 
public Fragment getFragment(int index){ 
     for (Fragment fragment:fragmentList){ 
      if (fragment.getId()==index) 
       return fragment; 
     } 
    } 
相关问题