如何知道哪个片段可见?

时间:2018-06-04 17:52:22

标签: android android-viewpager

我有一个带有片段幻灯片的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(示例),始终为空。

1 个答案:

答案 0 :(得分:2)

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

第一个选项:

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表示“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);     
 } 
}