MainActivity包含导航抽屉。单击导航抽屉项,使用viewpager打开一个片段。单击选项卡上的片段项目,另一个片段即将到来,但是当按下返回按钮时,viewpager片段变成空白。如何维护后退堆栈?
ViewPagerAdapter
public class ViewPagerAdapterProfile extends FragmentStatePagerAdapter{
int mNumOfTabs;
public ViewPagerAdapterProfile(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
if (position ==0) {
return new PersonalDetailsFragment();
} else if (position == 1)
{
return new ProfessionalDetailsFragment();
} else return new EmploymentDetailsFragment();
}
@Override
public int getCount() {
return mNumOfTabs;
}
@Override
public int getItemPosition(@NonNull Object object)
{
return PagerAdapter.POSITION_NONE;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void setListViewHeight(ExpandableListView listView, int group) {
if ((listAdapter.getChildrenCount(i) == 0) && (i == group)) {
Fragment fragment = null;
switch (group) {
case 0:
fragment = new DashboardFragment();
tv_name.clearComposingText();
tv_name.setText("Dashboard");
break;
case 1:
fragment = new MyProfileFrament();
tv_name.clearComposingText();
tv_name.setText("My Profile");
break;
default:
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
for (Fragment frag : fm.getFragments()) {
if (frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
childFm.popBackStack();
return;
}
}
}
super.onBackPressed();
}
}
MyProfileFrament
public class MyProfileFrament extends Fragment{
private ViewPager viewPager;
private TabLayout tabLayout;
private String[] pageTitle = {"Personal Details", "Education/Professional Details", "Employement Details"};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_myprofile, container, false);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
for (int i = 0; i < 3; i++) {
tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
ViewPagerAdapterProfile pagerAdapter = new ViewPagerAdapterProfile(requireFragmentManager(), pageTitle.length);
viewPager.setAdapter(pagerAdapter);
//change Tab selection when swipe ViewPager
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//change Tab selection when swipe ViewPager
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//change ViewPager page when tab selected
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
}
答案 0 :(得分:0)
您可以使用:维护片段的后堆栈:
Fragment fragment=new YourFragmentClass();
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.hide(SourceFragment.this);
ft.addToBackStack(SourceFragment.class.getName());
ft.commit();