【问题标题】:Both Fragment in ActionBar return true for fragment.isVisible()ActionBar 中的 Fragment 都为 fragment.isVisible() 返回 true
【发布时间】:2016-07-31 07:04:48
【问题描述】:

在我的MainActivityActionBar 中,我从DialogFragment 收听一个听众,并根据我所在的ActionBar 的片段,我需要做一些事情。

我正在使用以下代码获取当前片段并检查我在两个ActionBar 片段中的哪一个:

 private Fragment getVisibleFragment() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
    }
    return null;
}

但是fragment.isVisible() 对两个片段都返回true。为什么会这样?我还需要考虑其他标志吗?

以下是我的FragmentPagerAdapter 实现:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context context;
public SectionsPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

@Override
public Fragment getItem(int index) {
    switch (index) {
        case 0:
            return new ReceivedListFragment();
        case 1:
            return new SentListFragment();
    }
    return null;
}

@Override
public int getCount() {
    // Show 2 total pages.
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {
    Locale l = Locale.getDefault();
    switch (position) {
        case 0:
            return context.getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return context.getString(R.string.title_section2).toUpperCase(l);
    }
    return null;
}
}

【问题讨论】:

  • 如何添加片段?你能展示你进行片段交易的部分吗?
  • @jonDoe 将其添加到代码中。

标签: android android-fragments android-actionbar android-actionbar-compat


【解决方案1】:

您似乎正在使用 ViewPager,您需要找出当前选择的视图。在我看来,您的方法中的问题是查看器保留了至少两个片段的实例。似乎您只有两个片段,两个片段都由视图页面保存,这就是为什么您得到两个片段的 isVisible true。

如果你只需要在viewpager上获取当前选中的fragment,请在viewpager类中试试这个方法:

mViewPagerInstance.getCurrentItem();

这将为您提供当前选择的片段索引。

另一种方法是为您的 viewpager 添加一个 PageChangeListener,然后监听页面更改事件:

class MyActivity extends Activity{
  private int curPage;

  private static class MFragChangeListener extends SimpleOnPageChangeListener{
        public void onPageSelected(int position) {
               curPage = position;
  }
} 

  ........... /* Rest of your code */

mViewPagerInstance.setOnPageChangeListener(new MFragChangeListener());
/* Now access curPage to get the current selected page index whenever you need it. */

请参考this SO 帖子。

【讨论】:

  • 行得通。我有一个备份:private Fragment getVisibleFragment(Class&lt;?&gt; fragmentType) {...if(fragment != null &amp;&amp; fragment.isVisible() &amp;&amp; fragmentType == ReceivedListFragment.class) { return fragment; }...} 但我想要一个正确的解决方案。谢谢
  • 是的,我们应该始终尽可能使用最佳解决方案。黑客可能会在意想不到的情况下失败,然后我们就会崩溃。我很高兴这对你有用!
  • 现在如何获取fragment的实例?执行类似mSectionsPagerAdapter.getItem(currentFragmentIndex) 的操作会返回片段的新实例
  • 我相信您可以保留片段的数组/列表,然后使用索引检索它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
相关资源
最近更新 更多