如何在FragmentA内部编程FragmentB?

时间:2019-03-24 08:09:55

标签: android android-fragments

我正在作为学校项目进行申请,但在FragmentA内对某些FragmentB进行编程时遇到了麻烦。麻烦的是,更麻烦的是,即使我进行了广泛的搜索,我也不知道该怎么做。

FragmentAMainActivity正常工作(或者这样,因为没有出现任何错误)。我在MainActivity.java中使用了SwitchCase,它可以正常工作。

MainActivity.java


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

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {

                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.navigation_home:
                                selectedFragment = HomeScreenFragment.newInstance();
                                break;
                            case R.id.navigation_profile:
                                selectedFragment = ProfileScreenFragment.newInstance();
                                break;
                            case R.id.navigation_notifications:
                                selectedFragment = NotifScreenFragment.newInstance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, HomeScreenFragment.newInstance());
        transaction.commit();
    }
}

ProfileScreenFragment.java(这是我想要的FragmentB)

public class ProfileScreenFragment extends Fragment {
    public static ProfileScreenFragment newInstance() {
        ProfileScreenFragment fragment = new ProfileScreenFragment ();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_profile_screen2, container, false);
    }
}

fragment_profile_screen2.xml(frame_layout是我要放置FragmentB的位置)

    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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginRight="15dp"
                android:background="@color/colorPrimary"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:padding="7dp"
                    app:srcCompat="@drawable/logo_mdpi" />

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="313dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-black"
                    android:gravity="center"
                    android:text="Profile"
                    android:textColor="@android:color/white"
                    android:textSize="25sp" />

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/icon_more2" />
            </LinearLayout>
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabMode="fixed"
            app:tabTextColor="@android:color/white">

            <android.support.design.widget.TabItem
                android:id="@+id/profile_profile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Profile" />

            <android.support.design.widget.TabItem
                android:id="@+id/profile_posts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Posts" />

            <android.support.design.widget.TabItem
                android:id="@+id/profile_uploads"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Uploads" />

            <android.support.design.widget.TabItem
                android:id="@+id/profile_likes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Likes" />
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@android:drawable/ic_input_add"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>

我应该如何为FragmentB编写什么代码?放在ProfileScreenFragment.java内?

是否还可以在ProfileScreenFragment.java中为选项卡使用SwitchCase?

非常感谢。如果我在使用的代码中需要澄清,请随时询问。我仍在学习所有内容的基础知识,因此我对代码的了解是50/50。

1 个答案:

答案 0 :(得分:0)

在ProfileScreenFragment中添加此代码段。

import { observable, action } from 'mobx';

export interface ISignStore {
  email: string,
  password: string,
  firstName: string,
  lastName: string,
  handleInput(e: any): void,
  handleSubmit(e: any): void
}

export class SignStore implements ISignStore {
  @observable
  UserInformation: {
    [email: string]: '',
    [password: string]: '',
    [firstName: string]: '',
    [lastName: string]: ''
  };

  @action
  handleInput = (e: any): void => {
    [e.target.id] = e.target.value;
  };

  @action
  handleSubmit = (e: any): void => {
    e.preventDefault();
    console.log(this.UserInformation);
  };
}

对于标签布局,您可以添加一个标签更改侦听器

@Override
protected void onViewCreated(View view, Bundle savedInstanceState) {
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, FragmentB.newInstance());
    transaction.commit();
}