如何更改Android应用程序开发的片段,每个片段中都有按钮?

如何更改Android应用程序开发的片段,每个片段中都有按钮?,android,button,android-fragments,android-fragmentactivity,Android,Button,Android Fragments,Android Fragmentactivity,我有一个Android活动,里面有两个片段,比如A和B。我想点击A里面的按钮,这样B就会切换到C、D或E 每个布局都引用了一个.xml,而所有布局都有不同的按钮来调用其他片段或执行其他操作 现在在main.java中: public void stackAFragment(int page) { //depending on which button on the list is clicked, the corresponding fragment is replaced if (npage

我有一个Android活动,里面有两个片段,比如A和B。我想点击A里面的按钮,这样B就会切换到C、D或E

每个布局都引用了一个.xml,而所有布局都有不同的按钮来调用其他片段或执行其他操作

现在在main.java中:

public void stackAFragment(int page) {

//depending on which button on the list is clicked, the corresponding fragment is replaced
if (npage = 1){
Fragment f = new FragmentB();}

if (npage = 2){
Fragment f = new FragmentC();}

if (npage = 3){
Fragment f = new FragmentD();}

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.replace(R.id.frag_content, f); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
ft.addToBackStack(null); 
ft.commit();}
然后在A.java中,有一个列表视图,用于侦听器

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Main main = (Main) getActivity();
main.stackAFragment(arg2 + 1); }
在C.java中:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
View c = inflater.inflate(R.layout.frag_c, container, false);
return c;}
等等

现在,在第一页,它显示了片段A和B

但当我点击更改为片段C时,片段B并没有消失,相反,片段C出现在片段B下

当我点击更改为片段D时,片段C消失了,但B仍然留在

我怎样才能解决我的问题

非常感谢!!!如果有人能为我指出正确的道路,我将不胜感激

其他信息: main.xml的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

<fragment
    android:id="@+id/frag_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.seapointcorp.enque01.ContentFragment" />

<fragment
    android:id="@+id/frag_title"
    android:layout_width="@dimen/titles_size"
    android:layout_height="match_parent"
    class="com.seapointcorp.enque01.TitleFragment" />

</FrameLayout>

在您的主机
活动中

public static FragmentListener sFragmentListener;    

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Do normal work.
    sFragmentListener = new FragmentListener();
    // Do more work.
}

public interface FragmentListener {
        void onNextFragment();
    }

    public final class FragmentListenerListener {

        @Override
        public void onShowNextFragment() {
            // Do Fragment Transition
            mFragmentTransition.replace(R.id.awesomeContainer, new A.class);
        }

    }
Fragment
OnClick
方法中,只需调用:
HostActivity.sfFragmentListener.onShowNextFragment()
即可执行该方法


您可以根据自己的需要进行修改。

代码的主要问题是,您正在使用布局文件中的
片段
项的id进行替换
片段事务
(因此您基本上是在id为
片段内容
的片段中进行替换(我假设是片段B))。相反,您应该有一个包装布局(一个简单的
FrameLayout
RelativeLayout
等),作为将添加/删除/替换内容片段的区域:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

    <FrameLayout android:id="@+id/content"     android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <fragment
           android:id="@+id/frag_content"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           class="com.seapointcorp.enque01.ContentFragment" />
    </FrameLayout>
    <fragment
        android:id="@+id/frag_title"
        android:layout_width="@dimen/titles_size"
        android:layout_height="match_parent"
        class="com.seapointcorp.enque01.TitleFragment" />

    </FrameLayout>
</FrameLayout>

了解id为
R.id.frag_内容的视图位于何处会有帮助(可能还会发布布局文件)。您需要在宿主活动中创建一个侦听器,您可以从片段a调用该侦听器并切换到所需的片段。R.id.frag_内容位于main.xml@Leandros:::是否有关于该内容的链接/教程??我想我不明白……谢谢@user1702061将发布一些代码,wait.ft.replace(R.id.content,f);什么是“f”?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

    <FrameLayout android:id="@+id/content"     android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <fragment
           android:id="@+id/frag_content"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           class="com.seapointcorp.enque01.ContentFragment" />
    </FrameLayout>
    <fragment
        android:id="@+id/frag_title"
        android:layout_width="@dimen/titles_size"
        android:layout_height="match_parent"
        class="com.seapointcorp.enque01.TitleFragment" />

    </FrameLayout>
</FrameLayout>
FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.replace(R.id.content, new tagetFragment()); 
 ft.addToBackStack(null);

            // Commit the transaction
            ft.commit();
//...