How to determine which Fragment is currently in ViewPager

As shown in the figure, it is a very common interface:

We put a SlidingTabLayout at the top of an Activity At the bottom is a ViewPager to switch the corresponding Fragment. For example, we need a button to create a new contact. The simplest way is to put a button directly under the list in Fragment. But if our code has been highly encapsulated and has been referenced in multiple places, we can't write in the encapsulation, or directly change another encapsulation with a button at the bottom Second time. In fact, the best way is to add a button directly at the bottom of the Activity, and then judge which Fragment it is currently in. The keyword instanceof naturally comes to our mind first, which is used to judge which Fragment it belongs to. However, there is no such method as getCurrentFragment that can be directly used to judge Fragment pager adapter.
A method is introduced here. The method setPrimaryItem (ViewGroup container, int position, Object object object) corresponding to the FragmentPagerAdapter class can meet our current needs.

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;
    private List<String> mTabs;
    private Fragment mCurrentFragment;

    public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> tabs) {
        super(fm);
        this.fragments = fragments;
        this.mTabs = tabs;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabs.get(position);
    }

    @Override
    public int getCount() {
        if (fragments != null) {
            return fragments.size();
        } else {
            return 0;
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    //Used to distinguish which fragment it belongs to
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentFragment = (Fragment) object;
        super.setPrimaryItem(container, position, object);
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }
}

What we need to do is to call this getCurrentFragment() in the corresponding Activity and combine it with instanceof to make a judgment.

/**
 * Address management
 */
public class AddressManagerActivity extends BaseActivity {

    @BindView(R.id.tablayout)
    SlidingTabLayout mTablayout;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;

    @BindView(R.id.tv_contacts)
    TextView contacts;  //New contact

    private ViewPagerAdapter mAdapter;

    @Override
    protected void getBundleExtras(Intent intent) {

    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected int getContentViewLayoutId() {
        return R.layout.activity_address_manager;
    }

    @Override
    protected void initViewAndData() {
        setToolBarTitle(getString(R.string.address));
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new RecipientFragment());
        fragments.add(new ConsignorFragment());
        List<String> tabs = new ArrayList<>();
        tabs.add(getString(R.string.recipient));
        tabs.add(getString(R.string.consignor));

        mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, tabs);
        mViewpager.setAdapter(mAdapter);
        mTablayout.setViewPager(mViewpager);
    }

    @OnClick({R.id.tv_contacts})
    void click(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.tv_contacts:
                if (mAdapter.getCurrentFragment() instanceof RecipientFragment) {
                    intent = new Intent(this, BuildRecipientActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(this, BuildConsignorActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }
}

RecipientFragment is the recipient list. Because there are only two fragments here, you can directly use else to judge. If there are more than two fragments, you need to judge separately.

Tags: Fragment

Posted on Fri, 31 Jan 2020 10:10:25 -0500 by tecmeister