将片段添加到活动中而没有实际片段

时间:2018-11-23 20:00:39

标签: java android android-fragments

所以,我这样做是出于学习目的,

我确切想做的是:

创建两个片段, 第一个片段有一个按钮,当按下该按钮时,会将第二个片段替换为主活动上的片段视图,第二个片段仅包含一个文本视图,上面写着“这是第二个片段”,但是当我尝试这样做时, 第一个片段被加载两次并按下按钮,第一个实例停留在第二个实例更改为第二个片段的位置。你们能告诉我我在做什么错:(我在附加代码以及屏幕截图)

MainActivity.java:

package com.femindharamshi.fragmenttrials;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements FirstFragment.OnFragmentInteractionListener {

    FragmentTransaction fragmentTransaction;

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

        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        FirstFragment newFrament = new FirstFragment();
        fragmentTransaction.add(R.id.fragmentView, newFrament);
        fragmentTransaction.commit();

    }

    @Override
    public void onFragmentInteraction() {
        Toast.makeText(this, "Button in fragment pressed!", Toast.LENGTH_SHORT).show();
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        SecondFragment newFragment = new SecondFragment();
        fragmentTransaction.replace(R.id.fragmentView, newFragment);
        fragmentTransaction.commit();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <fragment
        android:id="@+id/fragmentView"
        android:name="com.femindharamshi.fragmenttrials.FirstFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"/>
</RelativeLayout>

FirstFragment.java

package com.femindharamshi.fragmenttrials;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class FirstFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    Button button;

    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        button = view.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonPressed();
            }
        });

        return view;
    }

    public void onButtonPressed() {
        if (mListener != null) {
            mListener.onFragmentInteraction();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction();
    }
}

SecondFragment.java

package com.femindharamshi.fragmenttrials;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class SecondFragment extends Fragment {


    public SecondFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }
}

问题: 另外,在主活动中未提及android:name="com.femindharamshi.fragmenttrials.FirstFragment"时,应用程序崩溃了。如果我不想在活动开始时加载片段怎么办,为什么我必须提一下呢?即使我在其中加载secondFragment也可以吗?即使它的名字说的是FirstFragment?

如果我不希望事先在主活动中为片段类分配,并希望以编程方式进行操作怎么办?

屏幕截图:

Before Clicking the button After Clicking the button

0 个答案:

没有答案
相关问题