从viewpager片段打开一个片段

时间:2016-03-08 09:24:21

标签: android android-viewpager android-tablayout

我有一个带有viewpager的TabLayout。 TabLayout有3个选项卡,这意味着有3个片段。我想从一个片段中打开一个新片段,使新片段的布局也覆盖Tabs(即整个屏幕)。现在我们没有覆盖标签,因为我们正在从viewpager打开片段。



Activity:

public class ContainerActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private FragmentPagerAdapter pagerAdapter;

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

        TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.pagerFragments);

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabs.setupWithViewPager(viewPager);
        tabs.setTabTextColors(ContextCompat.getColorStateList(getApplicationContext(), R.drawable.tab_text_color));
        tabs.setSelectedTabIndicatorColor(ContextCompat.getColor(getApplicationContext(), R.color.strip_ind));
    }

    @Override
    public void onBackPressed() {
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
            return;
        } else {
            Fragment currFragment = fm.findFragmentByTag("android:switcher:" + viewPager.getId() +
                    ":" + pagerAdapter.getItemId(viewPager.getCurrentItem()));
            if (currFragment != null) {
                FragmentManager cfm = currFragment.getChildFragmentManager();
                if (cfm.getBackStackEntryCount() > 0) {
                    cfm.popBackStack();
                    return;
                }
            }
        }

        super.onBackPressed();
    }

    private  class MyPagerAdapter extends FragmentPagerAdapter {

        private  String[] pages;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            pages = getResources().getStringArray(R.array.nav_tabs);
        }

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

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new GalleryFragment();
                case 1:
                    return new PhotoFragment();
                case 2:
                    return new VideoFragment();
            }
            return null;
        }

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

    public void returnPhotoUri(Uri uri) {
        Intent data = new Intent();
        data.setData(uri);

        if (getParent() == null) {
            setResult(RESULT_OK, data);
        } else {
            getParent().setResult(RESULT_OK, data);
        }

        finish();
    }

    public void onCancel(View view) {
        getSupportFragmentManager().popBackStack();
    }


}

Fragment of ViewPager:

public class VideoFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.videofrag, container, false);

        Button record = (Button)v.findViewById(R.id.record);
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Fragment fragment = new FilterFragment();
            getChildFragmentManager().beginTransaction()
                    .replace(R.id.filter_fragment, fragment)
                    .addToBackStack(null)
                    .commit(); 
            }
        });
        return v;
    }
}




1 个答案:

答案 0 :(得分:0)

Fragment fragment = new NewFragment();
                getChildFragmentManager().beginTransaction()
                        .replace(R.id.framelayout_old_fragment, fragment)
                        .addToBackStack(null)
                        .commit();