首页 > 解决方案 > Android片段替换

问题描述

我有一个名为 ProjectsFragment 的片段,它有一个 constraintLayout、recyclerView 和一个 FloatingActionButton。

当我单击按钮转到另一个片段时,我正在尝试做到这一点。

新片段称为 NewProjectFragment。

到目前为止我得到的代码在下面,但它没有被替换。我知道在打印日志时正在运行代码

FloatingActionButton fab = view.findViewById(R.id.floatingActionButton);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NewProjectFragment npf = new NewProjectFragment();
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.constraintLayout, npf);
            transaction.addToBackStack(null);
            transaction.commit();

            Log.d("swap", "swap");
        }
    });

使用 MainActivity 上的以下设置选项卡

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);
}

SimpleFragmentPageAdapter 如下

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;

    public SimpleFragmentPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    // This determines the fragment for each tab
    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new ProjectsFragment();
        } else if (position == 1) {
            return new ColoursFragment();
        } else if (position == 2) {
            return new WishListFragment();
        } else if (position == 3) {
            return new AboutFragment();
        } else {
            return new ProjectsFragment();
        }
    }

    // This determines the number of tabs
    @Override
    public int getCount() {
        return 4;
    }

    // This determines the title for each tab
    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        switch (position) {
            case 0:
                return mContext.getString(R.string.project_tab);
            case 1:
                return mContext.getString(R.string.colours_tab);
            case 2:
                return mContext.getString(R.string.wish_list_tab);
            case 3:
                return mContext.getString(R.string.about_tab);
            default:
                return null;
        }
    }

}

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" />

</LinearLayout>

和 fragment_projects.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.ProjectsFragment"
    android:id="@+id/constraintLayout" >

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        android:src="@drawable/ic_add_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

标签: androidandroid-fragments

解决方案


transaction.replace(R.id.constraintLayout, npf);

看起来您正在尝试替换为约束布局,这对我来说很有趣。通常你有一个 FrameLayout,它是片段的简单容器。因此,我的猜测是您正在将新片段移动到现有片段的布局中,没有约束,因此它没有显示并且第一个片段保持在原位。

确保您在包含第一个片段的布局中使用视图的 ID(同样,通常是拥有的 Activity 布局中的 FrameLayout)。

如果这不是您的问题,请发布您的 XML 文件。


更新。

是的,正如我猜想的那样,您正在替换为 ConstraintLayout ,这不是一个好主意,正如您所见。您的设置有点奇怪,因为您有一个 ViewPager 正在管理一组片段,并且您想在所有内容上推送一个新片段?

我建议您改为启动一个新的活动。


推荐阅读