从所有片段实例中的活动调用方法

时间:2015-10-06 23:25:22

标签: android android-fragments

我有 PengaduanHomeActivity.class ,其中3个标签使用相同的片段 PengaduanFragment.class ,如下所示:

   public class PengaduanHomeActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

    ....

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

            appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);

            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            actionbar = getSupportActionBar();
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);


            tabLayout = (TabLayout) findViewById(R.id.tabs);
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setOffscreenPageLimit(7);

            mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
            mViewPager.setAdapter(mPagerAdapter);
            tabLayout.setupWithViewPager(mViewPager);

            appBarLayout.addOnOffsetChangedListener(this);

        }

int index = 0;

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
    index = i;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (index == 0) {
                PengaduanFragment.enableRefreshLayout(true);
            } else {
                PengaduanFragment.enableRefreshLayout(false);
            }

            break;
    }
    return super.dispatchTouchEvent(ev);
}


public class PagerAdapter extends FragmentPagerAdapter {


    private final String[] TITLES =
            {"News Task", "Task On Progress", "Completed Task"};
    private Bundle bundle;

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return PengaduanFragment.newInstance("0");
            case 1:
                return PengaduanFragment.newInstance("1");

            case 2:
                return PengaduanFragment.newInstance("2");
            default:
                return null;
        }
    }

}

在上面的代码中,我在PengaduanFragment中调用方法

public static void enableRefreshLayout(boolean b) {

        mSwipeRefreshLayout.setEnabled(b);
    }

但是最后 PengaduanFragment 在位置2中调用的方法。其他PengaduanFragment,位置0或1,未调用。那么如何在所有片段中调用方法 enableRefreshLayout ? thaks

1 个答案:

答案 0 :(得分:0)

您应该创建一个声明要使用的方法的接口:

public interface IMyInterface {
    public void myMethod();
}

然后,您拥有的每个片段都应该实现此方法:

public class MyFragment extends Fragment implements IMyInterface {
  //Fragment code

  public void myMethod() {
    //Method code
  }
}

最后,让你的适配器拥有这个接口的集合,并在活动调用

时循环它
public class MyAdapter extends FragmentPagerAdapter {
  //Collection of fragments
  Fragment[] fragments;

  //Adapter code

  public void invokeMe(){
    //Loop through each fragment
    if (fragment instanceof IMyInterface) {
      fragment.myMethod();
    }
  }
}

然后,您应该能够调用invokeMe并让适配器委派给正确的片段正确的行为。请注意片段实例是否仍然根据ViewPager当前屏幕外限制的设置仍然有效(可通过调用setOffscreenPageLimit()进行调整)