Java 无法替换/导航片段

Java 无法替换/导航片段,java,android,eclipse,user-interface,android-fragments,Java,Android,Eclipse,User Interface,Android Fragments,我试图从一个片段导航到另一个片段。这两个片段是100%不同的,没有ViewPager或类似的东西来连接它们。这是代码 fragment_view1.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" a

我试图从一个片段导航到另一个片段。这两个片段是100%不同的,没有
ViewPager
或类似的东西来连接它们。这是代码

fragment_view1.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/firstView">

    <TextView
        android:id="@+id/viewOneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="92dp"
        android:layout_marginTop="182dp"
        android:text="First View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/viewOneBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/viewOneText"
        android:layout_below="@+id/viewOneText"
        android:layout_marginTop="17dp"
        android:text="Click Here" />

  <include layout = "@layout/drop_down"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_alignParentBottom="true"/>


   <FrameLayout
                android:id="@+id/fragment_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/customFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="174dp"
        android:text="Custom Fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
java(使用custom_view.xml)

FirstView.java
中,当我单击按钮时,我需要移动到片段
CustomView
。但是,它只是将
CustomView
中的所有内容替换到
FirstView
之上,而不是移动。下面的图片将更好地解释它

FirstView
单独

CustomView
单独

当按钮被单击时,
FirstView


为什么会发生这种情况?

如果您需要用另一个片段替换一个片段,请参阅下面提到的休耕:

TalkDetail fragment = new TalkDetail(); 
// TalkDetail is name of fragment which you need to put while replacing older one.
Bundle bundle = new Bundle();

                    bundle.putString("title", m_ArrayList.get(arg2).title);
                    bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg);
                    bundle.putString("excert", m_ArrayList.get(arg2).excert);
                    bundle.putString("description", m_ArrayList.get(arg2).description);
                    bundle.putString("cat", m_ArrayList.get(arg2).cat);
                    bundle.putString("header_title", "Talk");
                    //bundle.putInt("postid", m_ArrayList.get(arg2).postid);

                    fragment.setArguments(bundle);                  ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
这是您的BaseContainerFragment.java类,它将为您管理很多东西

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import app.drugs.talksooner.R;

public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment);
        transaction.commit();
        getChildFragmentManager().executePendingTransactions();
    }

    public boolean popFragment() {
        Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

}
要了解更多具体信息,请在这里找到我的完整帖子


您正试图用新片段替换
firstView
,但
firstView
不是片段,而是“a
RelativeLayout

不能替换布局文件中静态定义的片段。您只能替换通过
FragmentTransaction
动态添加的片段

布局中没有一个包含片段

<fragment
        android:id="@+id/fragment1"
        android:layout_width="march_parent"
        android:layout_height="match_parent"></fragment>

新片段将替换先前添加到容器中的现有片段


请看。

谢谢您的回复。是的,我看了看,但不明白。那我该怎么办?代码示例?可能是对我的演示代码的修改吗?虽然你知道,但请修改片段的工作方式,()更改你正在寻找的内容。我编辑了代码。请查看FirstView.java和fragment_view1.xml。问题仍然存在。您已经在Layout.xml文件中添加了
FrameLayout
,您需要添加
fragment
。还有一件更重要的事情——首先需要在片段上附加第一个视图,然后单击按钮onClick,需要用自定义片段替换它。希望你明白我的意思。你需要在碎片活动中交换碎片。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import app.drugs.talksooner.R;

public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment);
        transaction.commit();
        getChildFragmentManager().executePendingTransactions();
    }

    public boolean popFragment() {
        Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

}
<fragment
        android:id="@+id/fragment1"
        android:layout_width="march_parent"
        android:layout_height="match_parent"></fragment>