2014-05-20 126 views
1

我想根據按鈕單擊更改片段。 (我已經做了兩個片段類稱爲fragmentLeft和fragmentMiddle)在按鈕上更改片段單擊

這是我MainActivity.java

public class MainActivity extends Activity { 

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


    } 
Fragment fr; 
    public void selectFrag(View view) { 


     if(view == findViewById(R.id.bigbutton_left)) { 
      fr = new fragmentLeft(); 



     }else { 
      fr = new fragmentMiddle(); 

     } 

     FragmentManager fm = getFragmentManager(); 

     if (fm != null) { 
      // Perform the FragmentTransaction to load in the list tab content. 
      // Using FragmentTransaction#replace will destroy any Fragments 
      // currently inside R.id.fragment_content and add the new Fragment 
      // in its place. 
      FragmentTransaction ft = fm.beginTransaction(); 
      ft.replace(R.id.fragment_place, fr); 
      ft.commit(); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 


     return true; 
    } 

} 

這是fragmentLeft.java

public class fragmentLeft extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 

     //Inflate the layout for this fragment 

     return inflater.inflate(
       R.layout.fragmentleft, container, false); 
    } 
} 

同樣是fragmenttwo的除外將佈局ID傳遞給infater.inflate()

我製作了兩個佈局文件兩個,並使用了正確的名稱。 這是我的片斷的定義從我activity_main.xml中

但我無法得到的按鈕做任何事情。沒有交易發生,我堅持第一個片段。

有點谷歌搜索後,我才知道,它不建議您在佈局中使用的片段,但一些其他類型的佈局,但我看到這個示例代碼http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ ,似乎工作就好了。

是什麼問題?

::編輯:: 有一些真正奇怪的事情發生。 如果我使用的主要活動此佈局的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Fragment No.1" 
     android:onClick="selectFrag" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="selectFrag" 
     android:text="Fragment No.2" /> 

    <fragment 
     android:name="com.mainpackage.FragmentOne" 
     android:id="@+id/fragment_place" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

片段發生變化,表現爲他們預計。 但是,如果我用這個佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 



    <Button 
     android:id="@+id/button2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignBaseline="@+id/bigbutton_middle" 
     android:layout_alignBottom="@+id/bigbutton_middle" 
     android:layout_marginLeft="7dp" 
     android:layout_toRightOf="@+id/bigbutton_middle" 
     android:background="@drawable/mainbutton_right" /> 

    <Button 
     android:id="@+id/bigbutton_left" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignBaseline="@+id/bigbutton_middle" 
     android:layout_alignBottom="@+id/bigbutton_middle" 
     android:layout_marginRight="7dp" 
     android:layout_toLeftOf="@+id/bigbutton_middle" 
     android:background="@drawable/mainbutton_left" /> 

    <Button 
     android:id="@+id/bigbutton_middle" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="187dp" 
     android:background="@drawable/mainbutton_middle" /> 
     <fragment 
    android:id="@+id/fragment_place" 
    android:name="com.mainpackage.FragmentOne" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:layout_below="@+id/bigbutton_right" /> 


</RelativeLayout> 

片段似乎沒有改變,即。按鈕似乎已經死了。

可能是什麼問題?

我使用Eclipse朱諾

+0

那麼,你發佈的代碼沒有任何引用Button的'OnClickListeners'。我假設你有一些,是嗎? – Wamasa

+0

我不,我期待selectFrag爲我完成這項工作。 –

回答

2

你是不是爲了使FragmentTransaction發生得到Buttons你點擊的參考。您需要在Buttons上設置OnClickListeners,以便它們可以執行相關代碼。

原因之一,第二佈局不顯示是因爲你引用一個inexistant ID:

<fragment 
android:id="@+id/fragment_place" 
android:name="com.mainpackage.FragmentOne" 
android:layout_width="match_parent" 
android:layout_height="80dp" 
android:layout_below="@+id/bigbutton_right" /> 

我沒有看到ViewbigButton_right對XML的ID。

第一個xml工作原理和第二個xml不工作的另一個原因是因爲您沒有在第二個xml上將onClick:屬性設置爲任何Button

+0

但沒有按鈕的工作。 這些片段並不適用。 即使我把切換片段的代碼放在我的onCreate()中,它也不會替換這些片段。 –

+0

您是否嘗試修復錯誤的ID? – Emmanuel

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/54128/discussion-between-harvey-slash-and-emmanuel)。 –