ActionBar中的两个Fragment都对fragment.isVisible()返回true [英] Both Fragment in ActionBar return true for fragment.isVisible()

查看:202
本文介绍了ActionBar中的两个Fragment都对fragment.isVisible()返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有ActionBarMainActivity中,我从DialogFragment侦听一个侦听器,并根据我所在的ActionBar片段,我需要做一些事情.

In my MainActivity with ActionBar I listen to a listener from a DialogFragment and based on which Fragment of ActionBar i am on, I need to do some things.

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

I am using below code to get the current fragment and check which of the two ActionBar fragment i am on:

 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.为什么会这样呢?我还需要考虑另一个标志吗?

but fragment.isVisible() returns true for both the fragments. Why would that be? Is there another flag I need to consider?

以下是我的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;
}
}

推荐答案

似乎您使用的是ViewPager,您需要找出哪个是当前选定的视图.我认为您的方法存在的问题是,viewpager保留至少两个片段的实例.看来您只有两个片段,两个片段都由viewpager保留,这就是为什么两个片段都为 isVisible true.

It seems you are using a ViewPager and you need to find out which is the currently selected view. The problem in your approach, in my opinion, is that the viewpager keeps instance of at least two fragments. It seems you have two fragments only, both of the fragments are kept by the viewpager and which is why you get the isVisible true for both the fragments.

如果仅需要在viewpager上获取当前选定的片段,请尝试从viewpager类中尝试以下方法:

If your only need is to get the currently selected fragment on the viewpager, please try this method from the viewpager class:

mViewPagerInstance.getCurrentItem();

,它将为您提供当前选择的片段索引.

which will give you the currently selected fragment index.

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

Another way would be to add a PageChangeListener for your viewpager and then listen to page change events:

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. */

请参考 这样的帖子.

这篇关于ActionBar中的两个Fragment都对fragment.isVisible()返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆