在android中使用tablayout和viewpager时动态替换片段

在android中使用tablayout和viewpager时动态替换片段,android,android-fragments,android-viewpager,android-tablayout,Android,Android Fragments,Android Viewpager,Android Tablayout,在过去的几天里,我一直在努力寻找解决这个问题的方法: 我有一个活动,它包含一个选项卡布局和一个查看页面。视图寻呼机使用适配器填充片段。选项卡是使用此viewpager创建的,并且是固定的。我的问题是如何动态更改选项卡的布局(例如,单击按钮后) 这是xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tool

在过去的几天里,我一直在努力寻找解决这个问题的方法: 我有一个活动,它包含一个选项卡布局和一个查看页面。视图寻呼机使用适配器填充片段。选项卡是使用此viewpager创建的,并且是固定的。我的问题是如何动态更改选项卡的布局(例如,单击按钮后)

这是xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:background="@color/white"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>
但在这种情况下,占位符ID应该使用什么?因为在活动布局中,我没有用于片段的占位符,只有一个viewpager。 那么,如何动态地替换片段,同时保留后退按钮的功能呢? 我已经搜索了很多,找到了一些黑客的解决方案,但我认为这是一个非常常见的情况,应该有一个更好的解决方案。
谢谢

您的
查看页面
片段
应包含
占位符ID
。 因此,在
片段
(它是
查看页面
的一部分)中,您可以像往常一样调用添加
片段

childFragmentManager.beginTransaction().add(placeholderId, editPrifileFragment).addToBackStack(null).commit();

谢谢你的回复,但当我尝试时,它不起作用。我还尝试删除片段,然后使用以下代码添加新片段:fragmentTransaction.remove(ProfileFragment.this).addToBackStack(null.commit();它可以工作并删除片段,但添加新片段不起作用:fragmentTransaction.add(R.id.layout,editPrifileFragment,“df”).addToBackStack(null.commit();为什么?你找到解决这个问题的办法了吗?我花了一个星期的时间尝试了所有的事情,但都没有结果。提前谢谢。
public class FragmentPageAdapter extends FragmentStatePagerAdapter {
    private Context mContext;
private List<Fragment> mFragments = new ArrayList<>();
private List<String> mFragmentTitles = new ArrayList<>();
private List<Integer> mFragmentIcons = new ArrayList<>();

public FragmentPageAdapter(Context context, FragmentManager fm) {
    super(fm);
    this.mContext = context;
}

public void addFragment(Fragment fragment, String title, int drawable) {
    mFragments.add(fragment);
    mFragmentTitles.add(title);
    mFragmentIcons.add(drawable);
}



@Override
public Fragment getItem(int position) {

    return mFragments.get(position);
}


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

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

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

public View getTabView(int position) {
    View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
    TextView tabText = (TextView) tab.findViewById(R.id.tabText);
    ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
    tabText.setText(mFragmentTitles.get(position));
    tabImage.setBackgroundResource(mFragmentIcons.get(position));
    if (position == 3) {
        tab.setSelected(true);
    }
    return tab;
}
  fragmentManager.beginTransaction().replace(placeholderId, editPrifileFragment).commit();
childFragmentManager.beginTransaction().add(placeholderId, editPrifileFragment).addToBackStack(null).commit();