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

时间:2013-01-22 08:03:44

标签: 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 = 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); }

和B.java:

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

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

2 个答案:

答案 0 :(得分:1)

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

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> 方法中执行:

stackAFragment()

答案 1 :(得分:0)

在您的主人activity

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的{​​{1}}方法中,只需调用:OnClick即可执行该方法。

您可以根据需要对其进行修改。