从Activity中打开tabFragment的第二个片段

时间:2017-11-27 12:27:54

标签: android android-fragments android-viewpager

实际上在我的代码中,我实现了一个Tab Fragment,我在Fragment中添加了viewPager代码,并且我从活动中调用了片段。 它运作良好,但我需要打开第二个片段,所以如何从活动加载第二个片段。 这是我的代码:

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
protected FragmentActivity mActivity;
MyAdapter adapter;
View x;
Offers offersFragment;

public TabFragment() {
    // Required empty public constructor
}

public void onBackPressed() {
    // do stuff
    viewPager.setCurrentItem(0, true);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = (FragmentActivity) activity;
}


public void navigateFragment(int position){

    viewPager.setCurrentItem(position, true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    x = inflater.inflate(R.layout.content_main_menu, null);
    tabLayout = x.findViewById(R.id.tabs);
    viewPager = x.findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    //viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    viewPager.setOffscreenPageLimit(1);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}


private void setupViewPager(ViewPager viewPager) {
    adapter = new MyAdapter(getChildFragmentManager());
    adapter.addFragment(new HomeFragment(), "HOME");
    adapter.addFragment(new Offers(), "OFFERS");

    viewPager.setAdapter(adapter);


}


private class MyAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }
}


    }

来自活动的电话:

                    Log.e("lifecycle", "enter");
                    fragobjTab = (TabFragment) getSupportFragmentManager().findFragmentById(R.id.containerView);

                    fragobjTab.navigateFragment();

日志:

on a null object reference and than crash

2 个答案:

答案 0 :(得分:2)

viewPager.setAdapter(adapter);
viewPager.setCurrentItem(2);

现在第二个片段将被打开。

答案 1 :(得分:2)

正如答案中提到的Abhishek Dubey一样,请使用viewPager.setCurrentItem()方法。

使用此代码:

Fragment fragment = new TabFragment();
                Bundle   bundle = new Bundle();
                bundle.putInt("viewpager", 2);
                /*the above value 2 indicates the second fragment*/
                fragment.setArguments(bundle);
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();

                transaction.replace(R.id.containerView, fragment,"SOMETAG").commit();
片段中的

将此代码添加到onCreateView

Bundle bundle = this.getArguments();
    if (bundle != null) {
        int pageNumber=bundle.getInt("viewpager");
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(pageNumber);
    }