Android片段替换

时间:2019-06-15 22:19:37

标签: android android-fragments

我有一个名为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>

1 个答案:

答案 0 :(得分:0)

  

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

看起来您正在尝试替换为约束布局,这对我来说很有趣。通常,您有一个FrameLayout,它是片段的简单容器。因此,我的猜测是,您正在将新片段无限制地移至现有片段的布局中,因此它不会出现,并且第一个片段仍然存在。

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

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


更新。

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

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