如何知道哪个片段是可见的?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (45)

我有一个带有fragments slide的viewpager,我需要知道屏幕上是哪一个,才能将一些结果返回到正确的字段。如何知道屏幕上有什么?

以下是代码:

//Activity
public class ActivityAtendimentoTab extends AppCompatActivity{

    private ResultFragmentsCondutas resultFragmentsCondutas;

    //método para que possa ser passado os valores falados para os fragments.
    public interface ResultFragmentsCondutas {
        void showCondutaResults(ArrayList<String> results);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atendimento_tab);

......

        vpPager = (ViewPager) findViewById(R.id.vpPager);
        adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
        vpPager.setAdapter(adapterViewPager);
        vpPager.addOnPageChangeListener(viewPagerPageChangeListener);
..
   }
}

下面是ViewPager,它是一个内部类:

    //ViewPager inner class ActivityAtendimentoTab
    public static class MyPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 2;

        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return new FragmentAvaliacao();
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return new FragmentReceita()
                default:
                    return null;
            }
        }

        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            return "Page " + position;
        }

    }

我需要知道其中哪些是在屏幕上,按照以下代码执行:

@Override
public void onResults(Bundle results) {

    //inicializando as variáveis de fragments
    av = (FragmentAvaliacao) getSupportFragmentManager().findFragmentByTag(getString(R.string.tag_fragment_avaliacao));//NULL

    //pegando a lista de comandos
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

    //Verifica qual fragment está chamando a interface
    //e passa o valor da string para que seja implementada nos respectivos campos.
    try {
        if (vpPager.getCurrentItem() == 0) {//TRUE
            resultFragmentsCondutas = av;
            resultFragmentsCondutas.showCondutaResults(matches);
}

如何初始化av,始终为空。

提问于
用户回答回答于

如果我理解正确,您需要知道在滑动时可见的片段:

第一种选择:

viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {

}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

public void onPageSelected(int position) {

    // Check if this is the page you want by position

  if{position==0){
   //is FragmentAvaliacao() do something
  } else{
   //is FragmentReceita()  do something
  }

}

});

检查int onPageSelected(int position)是否为0或者1,0表示“FragmentAvaliacao(),1表示FragmentReceita()

第二种选择

 @Override
 public void onResults(Bundle results) {

   Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.of_your_viewpager + ":" + viewPager.getCurrentItem());

 if (ViewPager.getCurrentItem() == 0 && page != null) {
      ((FragmentAvaliacao)page).showCondutaResults(matches);     
 } 
}

所属标签

可能回答问题的人

  • 天使的炫翼

    17 粉丝531 提问36 回答
  • 富有想象力的人

    3 粉丝0 提问30 回答
  • 学生

    3 粉丝476 提问28 回答
  • o o

    4 粉丝494 提问27 回答

扫码关注云+社区

领取腾讯云代金券