多个片段中的活动调用界面

时间:2019-04-30 08:01:51

标签: java android android-fragments android-viewpager

我正在使用viewpager进行活动,该viewpager包含3个片段

1. A barcode scanner camera fragment, 2. A map fragment 3. and a recyclerview fragment

应用程序启动时,我导航至片段2(地图片段),但就像我们知道的那样,ViewPager一次加载了多个片段,因此过渡过程更加平滑,Barcode scanner on fragment 1也是初始化并在后台运行,我的解决方案是在我的活动中创建一个interface,例如:

public class ControllerActivity extends AppCompatActivity{

    private PageNumberListener pageNumberListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_controller);
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      BarcodeScannerFragment barcodeScannerFragment = new BarcodeScannerFragment();
      pageNumberListener = barcodeScannerFragment;
      adapter.addFragment(barcodeScannerFragment);

      viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            pageNumberListener.onCurrentPageChanged(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    }

    public interface PageNumberListener {
      void onCurrentPageChanged(int pageNumber);
    }
}

并在我的BarcodeFragment中实现它

  public class BarcodeScannerFragment extends Fragment implements ControllerActivity.PageNumberListener {


    @Override
    public void onCurrentPageChanged(int pageNumber) {


    }


  }

然后在onCurrentPageChanged(in pageNumber)

中开始/停止扫描

我现在的问题是,我需要在我所有其他片段中实现此接口,并且当活动调用onCurrentPageChanged(in pageNumber)时,我希望所有3个片段都接收信号,并且每个都按照预期执行操作例如,当用户从BarcodeScannerFragment滚动到MapFragment时,我希望触发该方法,以使BarcodeScannerFragment可以停止扫描并且MapFragment恢复位置更新,相同适用于RecyclerViewFragment,当用户从MapFragment滚动到RecyclerViewFragment时,我希望MapFragment停止位置更新,并且希望RecyclerViewFragment提取数据名单。有人会说我使用了onResume(),但是我觉得它不能正常工作。有人知道我如何做到这一点吗?我尝试创建类似{p>的BaseFragment

  public class BaseFragment extends Fragment implements ControllerActivity.PageNumberListener  {


      @Override
      public void onCurrentPageChanged(int pageNumber) {

      }
  }

每个片段都像

 public class MapFragment extends BaseFragment  {
          @Override
          public void onCurrentPageChanged(int pageNumber) {
            super.onCurrentPageChanged(pageNumber)
          }
}

public class BarCodeScannerFragment extends BaseFragment  {
          @Override
          public void onCurrentPageChanged(int pageNumber) {
            super.onCurrentPageChanged(pageNumber)
          }
}


public class RecyclerViewFragment extends BaseFragment  {
          @Override
          public void onCurrentPageChanged(int pageNumber) {
            super.onCurrentPageChanged(pageNumber)
          }
}

在我的活动中

  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            pageNumberListener = (PageNumberListener) adapter.getItem(position);
            pageNumberListener.onCurrentPageChanged(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

这有效,但是很浅,它仅针对当前片段触发,我不知道一个调用如何触发所有片段中的onCurrentPageChanged(position)

1 个答案:

答案 0 :(得分:0)

在Marcin的评论下,我成功了,我在EventBus中注册了BaseFragment,在Override中将onPageChanged(Integer currentPage)注册了Fragments

public class BaseFragment extends Fragment  {


@Subscribe(threadMode = ThreadMode.MAIN)
public void onPageChanged(Integer currentPage) {

}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}

}

然后,在我的活动中,每次更改页面时,都需要触发EventBus

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            EventBus.getDefault().post(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

我的问题解决了! :)