2013-04-13 23 views
0

enter image description here更換片段:沒有工作

我有一個FragmentButton。如果點擊了Button,則必須將Fragment替換爲另一個Fragment。 activity_main.xml中的Fragments是靜態創建的。

<LinearLayout 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" 
android:background="#efefef" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <Button 
     android:id="@+id/bHome" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:background="#000000" 
     android:padding="3dp" 
     android:text="Home" 
     android:textColor="#ffffff" /> 

    <Button 
     android:id="@+id/bEvents" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:background="#f8bd0e" 
     android:padding="3dp" 
     android:text="Events" /> 

    <Button 
     android:id="@+id/bNearby" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:background="#f8bd0e" 
     android:padding="3dp" 
     android:text="Nearby" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/llFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="horizontal" > 

    <fragment 
     android:id="@+id/fHome" 
     android:name="com.example.fragmentstack.Home" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" /> 

    <fragment 
     android:id="@+id/fEvents" 
     android:name="com.example.fragmentstack.Events" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" /> 

    <fragment 
     android:id="@+id/fNear" 
     android:name="com.example.fragmentstack.NearBy" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" /> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </FrameLayout> 
</LinearLayout> 

activity_main.xml中的輸出是

enter image description here

主要活動:

public class MainActivity extends FragmentActivity implements OnClickListener { 
Button home, events, near; 
Fragment f1, f2, f3, f4; 
FragmentManager manager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    home = (Button) findViewById(R.id.bHome); 
    events = (Button) findViewById(R.id.bEvents); 
    near = (Button) findViewById(R.id.bNearby); 

    home.setOnClickListener(this); 
    events.setOnClickListener(this); 
    near.setOnClickListener(this); 

    manager = getSupportFragmentManager(); 
    f1 = manager.findFragmentById(R.id.fHome); 
    f2 = manager.findFragmentById(R.id.fEvents); 
    f3 = manager.findFragmentById(R.id.fNear); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.bHome: 
     if (home.isPressed()) { 
      home.setBackgroundColor(Color.BLACK); 
      home.setTextColor(Color.WHITE); 
      events.setBackgroundResource(R.color.Yellow); 
      near.setBackgroundResource(R.color.Yellow); 
      events.setTextColor(Color.BLACK); 
      near.setTextColor(Color.BLACK); 
      FragmentTransaction transaction = getSupportFragmentManager() 
        .beginTransaction(); 

      transaction.hide(f2); 
      transaction.hide(f3); 
      transaction.show(f1); 
      transaction.commit(); 
     } 
     break; 
    case R.id.bEvents: 
     if (events.isPressed()) { 
      events.setBackgroundColor(Color.BLACK); 
      events.setTextColor(Color.WHITE); 
      home.setBackgroundResource(R.color.Yellow); 
      near.setBackgroundResource(R.color.Yellow); 
      home.setTextColor(Color.BLACK); 
      near.setTextColor(Color.BLACK); 
      FragmentTransaction transaction1 = getSupportFragmentManager() 
        .beginTransaction(); 
      transaction1.hide(f1); 
      transaction1.hide(f3); 
      transaction1.show(f2); 
      transaction1.commit(); 
     } 
     break; 
    case R.id.bNearby: 
     if (near.isPressed()) { 
      near.setBackgroundColor(Color.BLACK); 
      near.setTextColor(Color.WHITE); 
      home.setBackgroundResource(R.color.Yellow); 
      events.setBackgroundResource(R.color.Yellow); 
      home.setTextColor(Color.BLACK); 
      events.setTextColor(Color.BLACK); 
      FragmentTransaction transaction2 = getSupportFragmentManager() 
        .beginTransaction(); 
      transaction2.hide(f1); 
      transaction2.hide(f2); 
      transaction2.show(f3); 
      transaction2.commit(); 
     } 
     break; 
    } 
} 

    } 

現在Home延伸Fragment。家的xml佈局有一個Button和一個TextView。在onClicking Button我用Update Profile Fragment替換Home Fragment

public class Home extends Fragment { 
Button btn; 
View view; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    view = inflater.inflate(R.layout.home, container, false); 
    init(); 

    return view; 
} 

private void init() { 
    btn = (Button) view.findViewById(R.id.bUpdate); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Fragment fg = new UpdateProfile(); 
      FragmentTransaction fragmentTransaction = getFragmentManager() 
        .beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment_container, fg); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 
     } 
    }); 

} 

    } 

R.id.fragment_container存在於activity_main.xml中。現在我面臨的問題是點擊按鈕時,它不會用新的片段替換片段。

回答

1

現在我面臨的問題是,當點擊按鈕,它不是 用新的片段替換片段。

本次交易不會發生,但你該片段添加到將被推出屏幕的容器是不可見的(所有佈局的片段具有寬度設置爲match_parent所以他們將填補LinearLayout的寬度並且FrameLayout將被推出屏幕)。 另外,您還可以對不包含以前片段的容器執行替換事務,這意味着舊片段仍然可以在屏幕上顯示(加上新添加的片段)。

您設置的系統不是處理您的方案的正確方法。從圖像看來,您似乎有一個類似佈局的選項卡,在這種情況下,您應該只有一個容器,其中的所有片段都將駐留(以避免顯示/隱藏正確片段的額外工作,並且還提供體面的BACK按鈕體驗)。

其次,如果你要與這些片段一起工作(做交易),你應該真的以編程方式添加它們,因爲替換不會很好地處理靜態片段。有很多關於如何製作帶碎片的佈局的選項卡的教程。